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

更新时间:2024-07-06 20:52: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();

49. System.out.println(\); 50. while(it22.hasNext()){ 51. System.out.print(it22.next()+\); //1 3 52. } 53. 54. Iterator it33 = user3.followedBy().iterator(); 55. System.out.println(\); 56. while(it33.hasNext()){ 57. System.out.print(it33.next()+\); //1 2 58. } 59. 60. } 61. 62. }

Java代码 1. 2. 3. 4. 5. 6. 7. 8. 9. package com.redis.test; import java.util.Iterator; public class IsFollowTestMain { //简单测试、没有采用单元测试Junit public static void main(String[] args) { UserService user1 = new UserServiceImpl(\); 10. UserService user2 = new UserServiceImpl(\); 11. UserService user3 = new UserServiceImpl(\); 12. user1.follow(\); 13. 14. user2.follow(\); 15. user2.follow(\); 16. 17. user3.follow(\); 18. user3.follow(\); 19. 20. boolean isfollowing = user1.isfollowing(\); 21. System.out.println(\+isfollowing); //true 22. 23. boolean isfollowed = user1.isfollowedBy(\); 24. System.out.println(\+isfollowed); //true 25. 26. Long followingCount = user1.followingCount(); 27. System.out.println(\+followingCount); //1 28. 29. Long followedCount = user2.followerCount(); 30. System.out.println(\+followedCount); //2 31. 32. Iterator commonFollowing = user1.commonfollowing(\).iterator(); 33. System.out.println(\); 34. while(commonFollowing.hasNext()){ 35. System.out.println(commonFollowing.next()); //2 36. } 37. 38. Iterator commonFans = user3.commonfollowedBy(\).iterator(); 39. System.out.println(\); 40. while(commonFans.hasNext()){ 41. System.out.println(commonFans.next()); //2 42. } 43. 44. } 45. 46. }

本文来源:https://www.bwwdw.com/article/tjm.html

Top