夜间模式
点赞与取消点赞
使用redis的集合set来完成
点赞:sadd key value
取消点赞:srem key value
点赞人数:scard key
点赞的人: SMEMBERS key
是否点过赞:SISMEMBER key value
代码实现
java
public Result queryHotBlog(Integer current) {
// 根据用户查询
Page<Blog> page = query()
.orderByDesc("liked")
.page(new Page<>(current, SystemConstants.MAX_PAGE_SIZE));
// 获取当前页数据
List<Blog> records = page.getRecords();
records.forEach(blog ->{
// 查询用户
blog = getUser(blog);
blog = isBolgLike(blog);
});
//也能这样写
//records.forEach(this::getUser);
return Result.ok(records);
}
// 是否点赞
private Blog isBolgLike(Blog blog) {
Long id = UserHolder.getUser().getId();
String key = RedisConstants.BLOG_LIKED_KEY+blog.getId();
Boolean sussess = stringRedisTemplate.opsForSet().isMember(key, id.toString());
blog.setIsLike(BooleanUtil.isTrue(sussess));
return blog;
}
@Override
public Result likeBlog(Long id) {
//1. 获取用户
Long userId = UserHolder.getUser().getId();
String key = RedisConstants.BLOG_LIKED_KEY+id;
//判断用户是否点赞
Boolean islike = stringRedisTemplate.opsForSet().isMember(key, userId.toString());
if (BooleanUtil.isFalse(islike)){
//未点赞
//数据库点赞+1
boolean sussess = update().setSql("liked = liked + 1").eq("id", id).update();
if (sussess) {
stringRedisTemplate.opsForSet().add(key, String.valueOf(userId));
}
} else {
//已点赞
boolean sussess = update().setSql("liked = liked - 1").eq("id", id).update();
if (sussess) {
stringRedisTemplate.opsForSet().remove(key, String.valueOf(userId));
}
}
return Result.ok();
}
@Override
public Result queryBlogByid(Long id) {
Blog blog = getById(id);
if (blog == null) {
return Result.fail("博客不存在");
}
blog = getUser(blog);
blog = isBolgLike(blog);
return Result.ok(blog);
}
private Blog getUser(Blog blog){
Long userId = blog.getUserId();
User user = userService.getById(userId);
blog.setName(user.getNickName());
blog.setIcon(user.getIcon());
return blog;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76