java实现用Redis来存储关注关系

2019-03-09 14:01

用Redis来存储关注关系(java实现)

?

Redis Dev redis Java代码 1. 2. 3. //一个接口UserService.java //一个接口的实现UserServiceImpl.java //两个采用Jedis的客户端测试类FollowTestMain.java,IsFollowTestMain.java

Java代码 1. 2. 3. 4. 5. 6. 7. 8. 9. //UserService.java接口如下 package com.redis.test; import java.util.Set; public interface UserService { public void follow(String userId); public void unfollow(String userId); 10. public Set following(); 11. public Set followedBy(); 12. public boolean isfollowing(String userId); 13. public boolean isfollowedBy(String userId); 14. public Long followingCount(); 15. public Long followerCount(); 16. public Set commonfollowing(String userId); 17. public Set commonfollowedBy(String userId); 18. }

Java代码 1. 2. 3. 4. 5. 6. 7. 8. 9. //接口实现方法如下: package com.redis.test; import java.util.Set; import redis.clients.jedis.Jedis; public class UserServiceImpl implements UserService { 10. 11. private String userId; 12. private Jedis redisService; 13. 14. public UserServiceImpl(String userId) { 15. this.userId = userId; 16. this.redisService = new Jedis(\); 17. } 18. 19. /** 20. *@description 关注对应用户编号方法实现 21. *@date 2012-4-1 22. *@parameter 23. *@see com.redis.test.UserService#follow(java.lang.String) 24. */ 25. @Override 26. public void follow(String userId) { 27. this.redisService.sadd(\ + this.userId + \,userId); //add 28. this.redisService.sadd(\ + userId + \,this.userId); //add 29. } 30. 31. /** 32. *@description 获取当前用户所有关注对象的用户编号集合 33. *@date 2012-4-1 34. *@parameter 35. *@see com.redis.test.UserService#following() 36. */ 37. @Override 38. public Set following() { 39. return this.redisService.smembers(\ + this.userId + \); //member 40. } 41. 42. /** 43. *@description 获取当前用户被哪些人关注的用户编号集合 44. *@date 2012-4-1 45. *@parameter 46. *@see com.redis.test.UserService#followedBy() 47. */ 48. @Override 49. public Set followedBy() { 50. return this.redisService.smembers(\ + this.userId + \); //member 51. } 52. 53. /** 54. *@description 取消关注某人(传入的用户编号标识操作)方法实现 55. *@date 2012-4-1 56. *@parameter 57. *@see com.redis.test.UserService#unfollow(java.lang.String) 58. */ 59. @Override 60. public void unfollow(String userId) { 61. this.redisService.srem(\ + this.userId + \,userId); //remove 62. this.redisService.srem(\ + userId + \,this.userId); //remove 63. } 64. 65. /** 66. *@description 判断当前用户是否关注了对应用户编号的用户方法实现 67. *@date 2012-4-1 68. *@parameter 69. *@see com.redis.test.UserService#isfollowing(java.lang.String) 70. */ 71. @Override 72. public boolean isfollowing(String userId) { 73. return this.redisService.sismember(\+this.userId+\, userId); //is member 74. } 75. 76. /** 77. *@description 判断是否存在对应用户编号的粉丝方法实现 78. *@date 2012-4-1 79. *@parameter 80. *@see com.redis.test.UserService#isfollowedBy(java.lang.String) 81. */ 82. @Override 83. public boolean isfollowedBy(String userId) { 84. return this.redisService.sismember(\+this.userId+\, userId);//is member 85. } 86. 87. /** 88. *@description 统计当前用户关注的人数总和 89. *@date 2012-4-1 90. *@parameter 91. *@see com.redis.test.UserService#followingCount() 92. */ 93. @Override 94. public Long followingCount() { 95. return this.redisService.scard(\+this.userId+\); //card 96. } 97. 98. /** 99. *@description 统计当前用户有多少粉丝数目方法实现 100. *@date 2012-4-1 101. *@parameter 102. *@see com.redis.test.UserService#followerCount() 103. */ 104. @Override 105. public Long followerCount() { 106. return this.redisService.scard(\+this.userId+\); //card 107. } 108. 109. /** 110. *@description 获取当前用户和传入用户编号对应的用户共同关注的用户编号集合方法实现 111. *@date 2012-4-1 112. *@parameter 113. *@see com.redis.test.UserService#commonfollowing(java.lang.String) 114. */ 115. @Override 116. public Set commonfollowing(String userId) { 117. String s1 = \ + this.userId + \; 118. String s2 = \ + userId + \; 119. return this.redisService.sinter(new String[] { s1, s2 }); 120. } 121. 122. /** 123. *@description 获取当前用户和传入用户编号对应的用户共同粉丝的用户编号集合方法实现 124. *@date 2012-4-1 125. *@parameter 126. *@see com.redis.test.UserService#commonfollowedBy(java.lang.String) 127. */ 128. @Override 129. public Set commonfollowedBy(String userId) { 130. String s1 = \ + this.userId + \; 131. String s2 = \ + userId + \; 132. return this.redisService.sinter(new String[] { s1, s2 }); 133. } 134. }

Java代码 1. 2. 3. 4. //两个测试类如下: package com.redis.test; 5. 6. 7. 8. 9. import java.util.Iterator; public class FollowTestMain { //简单测试、没有采用单元测试Junit 10. public static void main(String[] args) { 11. UserService user1 = new UserServiceImpl(\); 12. UserService user2 = new UserServiceImpl(\); 13. UserService user3 = new UserServiceImpl(\); 14. 15. user1.follow(\); 16. user1.follow(\); 17. 18. user2.follow(\); 19. user2.follow(\); 20. 21. user3.follow(\); 22. user3.follow(\); 23. 24. Iterator it1 = user1.following().iterator(); 25. System.out.println(\); 26. while(it1.hasNext()){ 27. System.out.print(it1.next()+\); //2 3 28. } 29. 30. Iterator it2 = user2.following().iterator(); 31. System.out.println(\); 32. while(it2.hasNext()){ 33. System.out.print(it2.next()+\); //1 3 34. } 35. 36. Iterator it3 = user3.following().iterator(); 37. System.out.println(\); 38. while(it3.hasNext()){ 39. System.out.print(it3.next()+\); //1 2 40. } 41. 42. Iterator it11 = user1.followedBy().iterator(); 43. System.out.println(\); 44. while(it11.hasNext()){ 45. System.out.print(it11.next()+\); //2 3 46. } 47. 48. Iterator it22 = user2.followedBy().iterator();


java实现用Redis来存储关注关系.doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:施工员题库5-2(多选题)

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: