redis——实战关注

举报
兔老大 发表于 2021/04/23 02:08:33 2021/04/23
【摘要】 效果:  思路:很好想,把自己的粉丝和自己关注的人都存起来(set即可),做增删改查。 package com.now.community.community.service; import com.now.community.community.entity.User;import com.now.community.community.util.Co...

效果: 

思路:很好想,把自己的粉丝和自己关注的人都存起来(set即可),做增删改查。


      package com.now.community.community.service;
      import com.now.community.community.entity.User;
      import com.now.community.community.util.CommunityConstant;
      import com.now.community.community.util.RedisKeyUtil;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.dao.DataAccessException;
      import org.springframework.data.redis.core.RedisOperations;
      import org.springframework.data.redis.core.RedisTemplate;
      import org.springframework.data.redis.core.SessionCallback;
      import org.springframework.stereotype.Service;
      import java.util.*;
      @Service
      public class FollowService implements CommunityConstant {
      @Autowired
      private RedisTemplate redisTemplate;
      @Autowired
      private UserService userService;
      public void follow(int userId, int entityType, int entityId) {
       redisTemplate.execute(new SessionCallback() {
      @Override
      public Object execute(RedisOperations operations) throws DataAccessException {
       String followeeKey = RedisKeyUtil.getFolloweeKey(userId, entityType);
       String followerKey = RedisKeyUtil.getFollowerKey(entityType, entityId);
       operations.multi();
       operations.opsForZSet().add(followeeKey, entityId, System.currentTimeMillis());
       operations.opsForZSet().add(followerKey, userId, System.currentTimeMillis());
      return operations.exec();
       }
       });
       }
      public void unfollow(int userId, int entityType, int entityId) {
       redisTemplate.execute(new SessionCallback() {
      @Override
      public Object execute(RedisOperations operations) throws DataAccessException {
       String followeeKey = RedisKeyUtil.getFolloweeKey(userId, entityType);
       String followerKey = RedisKeyUtil.getFollowerKey(entityType, entityId);
       operations.multi();
       operations.opsForZSet().remove(followeeKey, entityId);
       operations.opsForZSet().remove(followerKey, userId);
      return operations.exec();
       }
       });
       }
      // 查询关注的实体的数量
      public long findFolloweeCount(int userId, int entityType) {
       String followeeKey = RedisKeyUtil.getFolloweeKey(userId, entityType);
      return redisTemplate.opsForZSet().zCard(followeeKey);
       }
      // 查询实体的粉丝的数量
      public long findFollowerCount(int entityType, int entityId) {
       String followerKey = RedisKeyUtil.getFollowerKey(entityType, entityId);
      return redisTemplate.opsForZSet().zCard(followerKey);
       }
      // 查询当前用户是否已关注该实体
      public boolean hasFollowed(int userId, int entityType, int entityId) {
       String followeeKey = RedisKeyUtil.getFolloweeKey(userId, entityType);
      return redisTemplate.opsForZSet().score(followeeKey, entityId) != null;
       }
      // 查询某用户关注的人
      public List<Map<String, Object>> findFollowees(int userId, int offset, int limit) {
       String followeeKey = RedisKeyUtil.getFolloweeKey(userId, ENTITY_TYPE_USER);
       Set<Integer> targetIds = redisTemplate.opsForZSet().reverseRange(followeeKey, offset, offset + limit - 1);
      if (targetIds == null) {
      return null;
       }
       List<Map<String, Object>> list = new ArrayList<>();
      for (Integer targetId : targetIds) {
       Map<String, Object> map = new HashMap<>();
       User user = userService.findUserById(targetId);
       map.put("user", user);
       Double score = redisTemplate.opsForZSet().score(followeeKey, targetId);
       map.put("followTime", new Date(score.longValue()));
       list.add(map);
       }
      return list;
       }
      // 查询某用户的粉丝
      public List<Map<String, Object>> findFollowers(int userId, int offset, int limit) {
       String followerKey = RedisKeyUtil.getFollowerKey(ENTITY_TYPE_USER, userId);
       Set<Integer> targetIds = redisTemplate.opsForZSet().reverseRange(followerKey, offset, offset + limit - 1);
      if (targetIds == null) {
      return null;
       }
       List<Map<String, Object>> list = new ArrayList<>();
      for (Integer targetId : targetIds) {
       Map<String, Object> map = new HashMap<>();
       User user = userService.findUserById(targetId);
       map.put("user", user);
       Double score = redisTemplate.opsForZSet().score(followerKey, targetId);
       map.put("followTime", new Date(score.longValue()));
       list.add(map);
       }
      return list;
       }
      }
  
 

 

文章来源: fantianzuo.blog.csdn.net,作者:兔老大RabbitMQ,版权归原作者所有,如需转载,请联系作者。

原文链接:fantianzuo.blog.csdn.net/article/details/102798814

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。