|
| 1 | +# 利用 Redis Set 实现点赞点踩功能 |
| 2 | + |
| 3 | +## 代码实现 |
| 4 | +| [Python](#Python-版本) | [Java](#Java-版本) | |
| 5 | +|---|---| |
| 6 | + |
| 7 | +### Python 版本 |
| 8 | +```python |
| 9 | +from redis import Redis |
| 10 | + |
| 11 | + |
| 12 | +def get_like_key(entity_id): |
| 13 | + return 'like:{}'.format(entity_id) |
| 14 | + |
| 15 | + |
| 16 | +def get_dislike_key(entity_id): |
| 17 | + return 'dislike:{}'.format(entity_id) |
| 18 | + |
| 19 | + |
| 20 | +class Like: |
| 21 | + def __init__(self, client: Redis, entity_id: str): |
| 22 | + self.client = client |
| 23 | + self.entity_id = entity_id |
| 24 | + |
| 25 | + def like(self, user_id) -> int: |
| 26 | + """某用户点赞""" |
| 27 | + self.client.sadd(get_like_key(self.entity_id), user_id) |
| 28 | + self.client.srem(get_dislike_key(self.entity_id), user_id) |
| 29 | + return self.client.scard(get_like_key(self.entity_id)) |
| 30 | + |
| 31 | + def dislike(self, user_id) -> int: |
| 32 | + """某用户点踩""" |
| 33 | + self.client.sadd(get_dislike_key(self.entity_id), user_id) |
| 34 | + self.client.srem(get_like_key(self.entity_id), user_id) |
| 35 | + return self.client.scard(get_like_key(self.entity_id)) |
| 36 | + |
| 37 | + def get_like_status(self, user_id) -> int: |
| 38 | + """判断用户是否点赞或点踩了,点赞返回1,点踩返回-1,不赞不踩返回0""" |
| 39 | + if self.client.sismember(get_like_key(self.entity_id), user_id): |
| 40 | + return 1 |
| 41 | + if self.client.sismember(get_dislike_key(self.entity_id), user_id): |
| 42 | + return -1 |
| 43 | + return 0 |
| 44 | + |
| 45 | + def get_like_count(self) -> int: |
| 46 | + """获取当前点赞数""" |
| 47 | + return self.client.scard(get_like_key(self.entity_id)) |
| 48 | + |
| 49 | + |
| 50 | +if __name__ == '__main__': |
| 51 | + redis = Redis(decode_responses=True) |
| 52 | + redis.flushall() |
| 53 | + like_entity = Like(redis, 'user1') |
| 54 | + print(like_entity.get_like_count()) # 0 |
| 55 | + like_entity.like('user2') |
| 56 | + like_entity.like('user3') |
| 57 | + |
| 58 | + like_entity.dislike('user4') |
| 59 | + |
| 60 | + print(like_entity.get_like_count()) # 2 |
| 61 | + print(like_entity.get_like_status('user2')) # 1 |
| 62 | + print(like_entity.get_like_status('user4')) # -1 |
| 63 | + |
| 64 | + |
| 65 | +``` |
| 66 | + |
| 67 | +### Java 版本 |
| 68 | +```java |
| 69 | +import redis.clients.jedis.Jedis; |
| 70 | +import utils.JedisUtils; |
| 71 | + |
| 72 | +public class LikeService { |
| 73 | + private Jedis client = JedisUtils.getClient(); |
| 74 | + |
| 75 | + public LikeService() { |
| 76 | + |
| 77 | + } |
| 78 | + |
| 79 | + private String getLikeKey(String entityId) { |
| 80 | + return "like:" + entityId; |
| 81 | + } |
| 82 | + |
| 83 | + private String getDislikeKey(String entityId) { |
| 84 | + return "dislike:" + entityId; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * 用户点赞了某个实体 |
| 89 | + * |
| 90 | + * @param userId 用户id |
| 91 | + * @param entityId 实体id |
| 92 | + * @return 实体的点赞人数 |
| 93 | + */ |
| 94 | + public Long like(String userId, String entityId) { |
| 95 | + client.sadd(getLikeKey(entityId), userId); |
| 96 | + client.srem(getDislikeKey(entityId), userId); |
| 97 | + return client.scard(getLikeKey(entityId)); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * 用户点踩了某个实体 |
| 102 | + * |
| 103 | + * @param userId 用户id |
| 104 | + * @param entityId 实体id |
| 105 | + * @return 实体的点赞人数 |
| 106 | + */ |
| 107 | + public Long dislike(String userId, String entityId) { |
| 108 | + client.sadd(getDislikeKey(entityId), userId); |
| 109 | + client.srem(getLikeKey(entityId), userId); |
| 110 | + return client.scard(getLikeKey(entityId)); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * 用户对某个实体的赞踩状态,点赞1,点踩-1,不赞不踩0 |
| 115 | + * |
| 116 | + * @param userId 用户id |
| 117 | + * @param entityId 实体id |
| 118 | + * @return 赞踩状态 |
| 119 | + */ |
| 120 | + public int getLikeStatus(String userId, String entityId) { |
| 121 | + if (client.sismember(getLikeKey(entityId), userId)) { |
| 122 | + return 1; |
| 123 | + } |
| 124 | + if (client.sismember(getDislikeKey(entityId), userId)) { |
| 125 | + return -1; |
| 126 | + } |
| 127 | + return 0; |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * 获取某实体的点赞人数 |
| 132 | + * |
| 133 | + * @param entityId 实体id |
| 134 | + * @return 点赞人数 |
| 135 | + */ |
| 136 | + public Long getLikeCount(String entityId) { |
| 137 | + return client.scard(getLikeKey(entityId)); |
| 138 | + } |
| 139 | + |
| 140 | + public static void main(String[] args){ |
| 141 | + LikeService likeService = new LikeService(); |
| 142 | + |
| 143 | + String entityId = "user1"; |
| 144 | + System.out.println(likeService.getLikeCount(entityId)); // 0 |
| 145 | + |
| 146 | + likeService.like("user2", entityId); |
| 147 | + likeService.like("user3", entityId); |
| 148 | + |
| 149 | + likeService.dislike("user4", entityId); |
| 150 | + |
| 151 | + System.out.println(likeService.getLikeCount(entityId)); // 2 |
| 152 | + System.out.println(likeService.getLikeStatus("user2", entityId)); // 1 |
| 153 | + |
| 154 | + System.out.println(likeService.getLikeStatus("user4", entityId)); // -1 |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +``` |
0 commit comments