Skip to content

Commit 06a2edc

Browse files
committed
feat: add redis-set-like-and-dislike service
利用 Redis Set 实现点赞点踩功能
1 parent 4d150e5 commit 06a2edc

File tree

6 files changed

+311
-0
lines changed

6 files changed

+311
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
- [用 Redis 如何实现页面数据分页的效果?](/docs/redis-list-paginate.md)
2525

2626
### [Set 集合](/docs/redis-set-introduction.md)
27+
- [如何用 Redis 实现论坛帖子的点赞点踩功能?](/docs/redis-set-like-and-dislike.md)
2728

2829
### [Sorted Sets 有序集合](/docs/redis-sorted-set-introduction.md)
2930
- [社交网站通常会有粉丝关注的功能,用 Redis 怎么实现?](/docs/redis-sorted-set-sns-follow.md)

docs/redis-set-like-and-dislike.md

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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+
```
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import redis.clients.jedis.Jedis;
2+
import utils.JedisUtils;
3+
4+
public class LikeService {
5+
private Jedis client = JedisUtils.getClient();
6+
7+
public LikeService() {
8+
9+
}
10+
11+
private String getLikeKey(String entityId) {
12+
return "like:" + entityId;
13+
}
14+
15+
private String getDislikeKey(String entityId) {
16+
return "dislike:" + entityId;
17+
}
18+
19+
/**
20+
* 用户点赞了某个实体
21+
*
22+
* @param userId 用户id
23+
* @param entityId 实体id
24+
* @return 实体的点赞人数
25+
*/
26+
public Long like(String userId, String entityId) {
27+
client.sadd(getLikeKey(entityId), userId);
28+
client.srem(getDislikeKey(entityId), userId);
29+
return client.scard(getLikeKey(entityId));
30+
}
31+
32+
/**
33+
* 用户点踩了某个实体
34+
*
35+
* @param userId 用户id
36+
* @param entityId 实体id
37+
* @return 实体的点赞人数
38+
*/
39+
public Long dislike(String userId, String entityId) {
40+
client.sadd(getDislikeKey(entityId), userId);
41+
client.srem(getLikeKey(entityId), userId);
42+
return client.scard(getLikeKey(entityId));
43+
}
44+
45+
/**
46+
* 用户对某个实体的赞踩状态,点赞1,点踩-1,不赞不踩0
47+
*
48+
* @param userId 用户id
49+
* @param entityId 实体id
50+
* @return 赞踩状态
51+
*/
52+
public int getLikeStatus(String userId, String entityId) {
53+
if (client.sismember(getLikeKey(entityId), userId)) {
54+
return 1;
55+
}
56+
if (client.sismember(getDislikeKey(entityId), userId)) {
57+
return -1;
58+
}
59+
return 0;
60+
}
61+
62+
/**
63+
* 获取某实体的点赞人数
64+
*
65+
* @param entityId 实体id
66+
* @return 点赞人数
67+
*/
68+
public Long getLikeCount(String entityId) {
69+
return client.scard(getLikeKey(entityId));
70+
}
71+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import org.junit.jupiter.api.Test;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
public class LikeServiceTest {
6+
7+
@Test
8+
public void testLikeService() {
9+
LikeService likeService = new LikeService();
10+
String entityId = "user1";
11+
assertEquals(0, likeService.getLikeCount(entityId));
12+
13+
likeService.like("user2", entityId);
14+
likeService.like("user3", entityId);
15+
16+
likeService.dislike("user4", entityId);
17+
18+
assertEquals(2, likeService.getLikeCount(entityId));
19+
assertEquals(1, likeService.getLikeStatus("user2", entityId));
20+
21+
assertEquals(-1, likeService.getLikeStatus("user4", entityId));
22+
}
23+
}

python/src/main/like_dislike.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from redis import Redis
2+
3+
4+
def get_like_key(entity_id):
5+
return 'like:{}'.format(entity_id)
6+
7+
8+
def get_dislike_key(entity_id):
9+
return 'dislike:{}'.format(entity_id)
10+
11+
12+
class Like:
13+
def __init__(self, client: Redis, entity_id: str):
14+
self.client = client
15+
self.entity_id = entity_id
16+
17+
def like(self, user_id) -> int:
18+
"""某用户点赞"""
19+
self.client.sadd(get_like_key(self.entity_id), user_id)
20+
self.client.srem(get_dislike_key(self.entity_id), user_id)
21+
return self.client.scard(get_like_key(self.entity_id))
22+
23+
def dislike(self, user_id) -> int:
24+
"""某用户点踩"""
25+
self.client.sadd(get_dislike_key(self.entity_id), user_id)
26+
self.client.srem(get_like_key(self.entity_id), user_id)
27+
return self.client.scard(get_like_key(self.entity_id))
28+
29+
def get_like_status(self, user_id) -> int:
30+
"""判断用户是否点赞或点踩了,点赞返回1,点踩返回-1,不赞不踩返回0"""
31+
if self.client.sismember(get_like_key(self.entity_id), user_id):
32+
return 1
33+
if self.client.sismember(get_dislike_key(self.entity_id), user_id):
34+
return -1
35+
return 0
36+
37+
def get_like_count(self) -> int:
38+
"""获取当前点赞数"""
39+
return self.client.scard(get_like_key(self.entity_id))
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from redis import Redis
2+
3+
from main.like_dislike import Like
4+
5+
6+
def test_like_dislike():
7+
redis = Redis(decode_responses=True)
8+
redis.flushall()
9+
like_entity = Like(redis, 'user1')
10+
assert like_entity.get_like_count() == 0
11+
like_entity.like('user2')
12+
like_entity.like('user3')
13+
14+
like_entity.dislike('user4')
15+
16+
assert like_entity.get_like_count() == 2
17+
assert like_entity.get_like_status('user2') == 1
18+
assert like_entity.get_like_status('user4') == -1
19+

0 commit comments

Comments
 (0)