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即可),做增删改查。


  
  1. package com.now.community.community.service;
  2. import com.now.community.community.entity.User;
  3. import com.now.community.community.util.CommunityConstant;
  4. import com.now.community.community.util.RedisKeyUtil;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.dao.DataAccessException;
  7. import org.springframework.data.redis.core.RedisOperations;
  8. import org.springframework.data.redis.core.RedisTemplate;
  9. import org.springframework.data.redis.core.SessionCallback;
  10. import org.springframework.stereotype.Service;
  11. import java.util.*;
  12. @Service
  13. public class FollowService implements CommunityConstant {
  14. @Autowired
  15. private RedisTemplate redisTemplate;
  16. @Autowired
  17. private UserService userService;
  18. public void follow(int userId, int entityType, int entityId) {
  19. redisTemplate.execute(new SessionCallback() {
  20. @Override
  21. public Object execute(RedisOperations operations) throws DataAccessException {
  22. String followeeKey = RedisKeyUtil.getFolloweeKey(userId, entityType);
  23. String followerKey = RedisKeyUtil.getFollowerKey(entityType, entityId);
  24. operations.multi();
  25. operations.opsForZSet().add(followeeKey, entityId, System.currentTimeMillis());
  26. operations.opsForZSet().add(followerKey, userId, System.currentTimeMillis());
  27. return operations.exec();
  28. }
  29. });
  30. }
  31. public void unfollow(int userId, int entityType, int entityId) {
  32. redisTemplate.execute(new SessionCallback() {
  33. @Override
  34. public Object execute(RedisOperations operations) throws DataAccessException {
  35. String followeeKey = RedisKeyUtil.getFolloweeKey(userId, entityType);
  36. String followerKey = RedisKeyUtil.getFollowerKey(entityType, entityId);
  37. operations.multi();
  38. operations.opsForZSet().remove(followeeKey, entityId);
  39. operations.opsForZSet().remove(followerKey, userId);
  40. return operations.exec();
  41. }
  42. });
  43. }
  44. // 查询关注的实体的数量
  45. public long findFolloweeCount(int userId, int entityType) {
  46. String followeeKey = RedisKeyUtil.getFolloweeKey(userId, entityType);
  47. return redisTemplate.opsForZSet().zCard(followeeKey);
  48. }
  49. // 查询实体的粉丝的数量
  50. public long findFollowerCount(int entityType, int entityId) {
  51. String followerKey = RedisKeyUtil.getFollowerKey(entityType, entityId);
  52. return redisTemplate.opsForZSet().zCard(followerKey);
  53. }
  54. // 查询当前用户是否已关注该实体
  55. public boolean hasFollowed(int userId, int entityType, int entityId) {
  56. String followeeKey = RedisKeyUtil.getFolloweeKey(userId, entityType);
  57. return redisTemplate.opsForZSet().score(followeeKey, entityId) != null;
  58. }
  59. // 查询某用户关注的人
  60. public List<Map<String, Object>> findFollowees(int userId, int offset, int limit) {
  61. String followeeKey = RedisKeyUtil.getFolloweeKey(userId, ENTITY_TYPE_USER);
  62. Set<Integer> targetIds = redisTemplate.opsForZSet().reverseRange(followeeKey, offset, offset + limit - 1);
  63. if (targetIds == null) {
  64. return null;
  65. }
  66. List<Map<String, Object>> list = new ArrayList<>();
  67. for (Integer targetId : targetIds) {
  68. Map<String, Object> map = new HashMap<>();
  69. User user = userService.findUserById(targetId);
  70. map.put("user", user);
  71. Double score = redisTemplate.opsForZSet().score(followeeKey, targetId);
  72. map.put("followTime", new Date(score.longValue()));
  73. list.add(map);
  74. }
  75. return list;
  76. }
  77. // 查询某用户的粉丝
  78. public List<Map<String, Object>> findFollowers(int userId, int offset, int limit) {
  79. String followerKey = RedisKeyUtil.getFollowerKey(ENTITY_TYPE_USER, userId);
  80. Set<Integer> targetIds = redisTemplate.opsForZSet().reverseRange(followerKey, offset, offset + limit - 1);
  81. if (targetIds == null) {
  82. return null;
  83. }
  84. List<Map<String, Object>> list = new ArrayList<>();
  85. for (Integer targetId : targetIds) {
  86. Map<String, Object> map = new HashMap<>();
  87. User user = userService.findUserById(targetId);
  88. map.put("user", user);
  89. Double score = redisTemplate.opsForZSet().score(followerKey, targetId);
  90. map.put("followTime", new Date(score.longValue()));
  91. list.add(map);
  92. }
  93. return list;
  94. }
  95. }

 

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

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

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

评论(0

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

全部回复

上滑加载中

设置昵称

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

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

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