springboot +redis 实现点赞、浏览、收藏、评论等数量的增减操作

时间:2022-06-26 05:50:32

springboot +redis 实现点赞、浏览、收藏、评论等数量的增减操作

前言

第一次写博客,记录一下:

springboot +redis 实现点赞、浏览、收藏、评论等数量的增减操作

最近做了一个帖子的收藏、点赞数量的功能,其实之前也做过类似的功能,因为之前一直使用的mysql 总是感觉对于这种频繁需要改变的值,不应该给予Mysql过大的压力,本文章采用的是redis 做了持久化。下面贴出关键代码:DataResponse是项目中使用的结果封装实体类;forumDTO是此功能的参数实体,如果有需要请留言。

常量如下:

?
1
2
3
4
5
6
7
8
private static final String DEFAULT_VALUE = "0:0:0:0:0:0";
 public static final Byte BYTE_ZERO = 0;
 public static final Byte BYTE_ONE = 1;
 public static final Byte BYTE_TWO = 2;
 public static final Byte BYTE_THREE = 3;
 public static final Byte BYTE_FOUR = 4;
 public static final Byte BYTE_FIVE = 5;
 public static final Byte BYTE_SIX = 6;
?
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
@Override
 public DataResponse keepNum(ForumDTO forumDTO) {
   //将帖子id 设置为 key
   String key = forumDTO.getPostId().toString();
   //get 用户id
   String userId = forumDTO.getUserId();
   String count, newCount;
   //绑定数据集key
   BoundHashOperations<String, Object, Object> post = redisTemplate.boundHashOps("post:");
   //获取hKey
   // count: 0论坛-点赞量 1评论量 2收藏量 3浏览 4评论-点赞量
   if (null == post.get(key)) {
     //无则set
     post.put(key, DEFAULT_VALUE);
     //再取出来赋值给 count
     count = post.get(key).toString();
   } else {
     //有直接赋值 count
     count = post.get(key).toString();
   }
   // operationType 1 浏览 2 帖子点赞 3 收藏 4评论-点赞
   String prefix;
   switch (forumDTO.getOperationType()) {
     case 1:
       //记录浏览次数 OPERATIONTYPE 1 : 记录浏览次数
       newCount = resetValue(count, BYTE_THREE, true);
       post.put(key, newCount);
       break;
     case 2:
       //记录帖子-点赞
       prefix = "thumbs:post";
       switch (forumDTO.getClickType()) {
         case 0:
           /**
            * OPERATIONTYPE 2: + CLICKTYPE 0 = 给帖子点赞
            * 0点赞
            * 从redis中获取数量 帖子d 例如:177488r88t78r78r7
            * count: 0论坛-点赞量 1评论量 2收藏量 3浏览 4评论-点赞量
            * 避免每种数量都去查询redis 直接通过 redis value 记录所有的数量
            * 获取加 +1 后的值
            */
           if (redisTemplate.opsForSet().isMember(prefix + ":" + key, prefix + ":" + userId)) {
             return DataResponse.fail("不能重复点赞哦");
           } else {
             redisTemplate.opsForSet().add(prefix + ":" + key, prefix + ":" + userId);
           }
           newCount = resetValue(count, BYTE_ZERO, true);
           //set to redis
           post.put(key, newCount);
           break;
         case 1:
           //OPERATIONTYPE 2: + CLICKTYPE 1 = 取消帖子点赞
           //1取消帖子点赞
           if (!redisTemplate.opsForSet().isMember(prefix + ":" + key, prefix + ":" + userId)) {
             //重复处理
             return DataResponse.fail("不能重复取消哦");
           } else {
             //删除
             redisTemplate.opsForSet().remove(prefix + ":" + key, prefix + ":" + userId);
           }
           newCount = resetValue(count, BYTE_ZERO, false);
           post.put(key, newCount);
           break;
       }
       break;
     case 3:
       prefix = "collection:post";
       List<MqMessage> sendList = new LinkedList<>();
       MqMessage mqMessage = new MqMessage();
       switch (forumDTO.getClickType()) {
         //OPERATIONTYPE 3 + CLICKTYPE 0 = 记录收藏
         case 0:
           //数量+1
           //根据用户id + 帖子id 查询redis 数据
           if (redisTemplate.opsForSet().isMember(prefix + ":" + key, prefix + ":" + userId)) {
             //重复处理
             return DataResponse.fail("不能重复收藏哦");
           }
           //add
           redisTemplate.opsForSet().add(prefix + ":" + key, prefix + ":" + userId);
           //set to redis
           newCount = resetValue(count, BYTE_TWO, true);
           post.put(key, newCount);
           mqMessage.setType(new Byte("9"));
           mqMessage.setSenderId(userId);
           mqMessage.setPostId(forumDTO.getPostId());
           sendList.add(mqMessage);
           this.sendMq.send(sendList);
           break;
         //OPERATIONTYPE 3 + CLICKTYPE 1 = 取消收藏
         case 1:
           //取消收藏
           //尝试从redis取出当前用户是否已经收藏
           if (!redisTemplate.opsForSet().isMember(prefix + ":" + key, prefix + ":" + userId)) {
             //重复处理
             return DataResponse.fail("不能重复取消哦");
           }
           //删除
           redisTemplate.opsForSet().remove(prefix + ":" + key, prefix + ":" + userId);
           newCount = resetValue(count, BYTE_TWO, false);
           post.put(key, newCount);
           mqMessage.setType(new Byte("10"));
           mqMessage.setSenderId(userId);
           mqMessage.setPostId(forumDTO.getPostId());
           sendList.add(mqMessage);
           this.sendMq.send(sendList);
           break;
       }
       break;
     case 4:
       //记录评论-点赞
       // OPERATIONTYPE 4: + CLICKTYPE 0 = 给评论点赞
       if (null == forumDTO.getCommentId()) {
         return DataResponse.fail("评论id不能为空");
       }
       String commentNum, ckey = forumDTO.getCommentId().toString();
       BoundHashOperations<String, Object, Object> comment = redisTemplate.boundHashOps("post:comment");
       if (null == comment.get(ckey)) {
         //无则set
         comment.put(ckey, "0");
         //再取出来赋值给 count
         commentNum = comment.get(ckey).toString();
       } else {
         //有直接赋值 count
         commentNum = comment.get(ckey).toString();
       }
       //赞评论
       prefix = "thumbs:comment";
       switch (forumDTO.getClickType()) {
         case 0:
           /**
            * 0点赞
            * 从redis中获取数量 帖子d 例如:177488r88t78r78r7
            * count: 0论坛-点赞量 1评论量 2收藏量 3浏览 4评论-点赞量
            * 避免每种数量都去查询redis 直接通过 redis value 记录所有的数量
            * 获取加 + 后的值
            */
           if (redisTemplate.opsForSet().isMember(prefix + ":" + ckey, prefix + ":" + userId)) {
             return DataResponse.fail("不能重复点赞哦");
           } else {
             redisTemplate.opsForSet().add(prefix + ":" + ckey, prefix + ":" + userId);
           }
           //set to redis
           comment.put(ckey, cResetValue(commentNum, true));
           break;
         case 1:
           //1取消评论点赞
           if (!redisTemplate.opsForSet().isMember(prefix + ":" + ckey, prefix + ":" + userId)) {
             //重复处理
             return DataResponse.fail("不能重复取消哦");
           } else {
             //删除
             redisTemplate.opsForSet().remove(prefix + ":" + ckey, prefix + ":" + userId);
           }
           newCount = cResetValue(commentNum, false);
           comment.put(ckey, newCount);
           break;
       }
       break;
     default:
       DataResponse.fail(ResponseEnum.FAILED);
   }
   return DataResponse.success(ResponseEnum.SUCCESS);
 }

 

resetValue代码:

?
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
/**
  * 功能描述: <br>
  * 〈点赞数、收藏数等数量重置〉
  * @param val  数组
  * @param type  0帖子点赞量 1评论量 2收藏量 3浏览 4评论点赞量
  * @param isPlus 是否增加数量 true +  false -
  * @Return: java.lang.String
  * @Author:王震
  * @Date: 2020/8/5 10:27
  * StringUtils包:import org.apache.commons.lang3.StringUtils;
  * 可以使用jdk的包替代split方法;但jdk的包需要验证正则,效率较低。
  */
 private String resetValue(String val, int j, boolean isPlus) {
   String[] value = StringUtils.split(val, ":");
   Long temp = Long.valueOf(value[j]);
   StringBuffer sb = new StringBuffer(16);
   if (isPlus) {
     temp += 1;
   } else {
     temp -= 1;
   }
   value[j] = temp.toString();
   for (int i = 0, len = value.length; i < len; i++) {
     if (i != len - 1) {
       sb.append(value[i]).append(":");
     }else {
       sb.append(value[i]);
     }
   }
   return sb.toString();
 }

总结

到此这篇关于springboot +redis 实现点赞、浏览、收藏、评论等数量的增减操作的文章就介绍到这了,更多相关springboot +redis实现点赞收藏评论内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/roseandjeck/article/details/108615292