av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術(shù)文章
文章詳情頁

springboot +redis 實(shí)現(xiàn)點(diǎn)贊、瀏覽、收藏、評(píng)論等數(shù)量的增減操作

瀏覽:5日期:2023-04-23 08:25:58

springboot +redis 實(shí)現(xiàn)點(diǎn)贊、瀏覽、收藏、評(píng)論等數(shù)量的增減操作

前言

第一次寫博客,記錄一下:

springboot +redis 實(shí)現(xiàn)點(diǎn)贊、瀏覽、收藏、評(píng)論等數(shù)量的增減操作

最近做了一個(gè)帖子的收藏、點(diǎn)贊數(shù)量的功能,其實(shí)之前也做過類似的功能,因?yàn)橹耙恢笔褂玫膍ysql 總是感覺對(duì)于這種頻繁需要改變的值,不應(yīng)該給予Mysql過大的壓力,本文章采用的是redis 做了持久化。下面貼出關(guān)鍵代碼:DataResponse是項(xiàng)目中使用的結(jié)果封裝實(shí)體類;forumDTO是此功能的參數(shù)實(shí)體,如果有需要請(qǐng)留言。

常量如下:

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;

@Override public DataResponse keepNum(ForumDTO forumDTO) { //將帖子id 設(shè)置為 key String key = forumDTO.getPostId().toString(); //get 用戶id String userId = forumDTO.getUserId(); String count, newCount; //綁定數(shù)據(jù)集key BoundHashOperations<String, Object, Object> post = redisTemplate.boundHashOps('post:'); //獲取hKey // count: 0論壇-點(diǎn)贊量 1評(píng)論量 2收藏量 3瀏覽 4評(píng)論-點(diǎn)贊量 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 帖子點(diǎn)贊 3 收藏 4評(píng)論-點(diǎn)贊 String prefix; switch (forumDTO.getOperationType()) { case 1://記錄瀏覽次數(shù) OPERATIONTYPE 1 : 記錄瀏覽次數(shù)newCount = resetValue(count, BYTE_THREE, true);post.put(key, newCount);break; case 2://記錄帖子-點(diǎn)贊prefix = 'thumbs:post';switch (forumDTO.getClickType()) { case 0: /** * OPERATIONTYPE 2: + CLICKTYPE 0 = 給帖子點(diǎn)贊 * 0點(diǎn)贊 * 從redis中獲取數(shù)量 帖子d 例如:177488r88t78r78r7 * count: 0論壇-點(diǎn)贊量 1評(píng)論量 2收藏量 3瀏覽 4評(píng)論-點(diǎn)贊量 * 避免每種數(shù)量都去查詢r(jià)edis 直接通過 redis value 記錄所有的數(shù)量 * 獲取加 +1 后的值 */ if (redisTemplate.opsForSet().isMember(prefix + ':' + key, prefix + ':' + userId)) { return DataResponse.fail('不能重復(fù)點(diǎn)贊哦'); } 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 = 取消帖子點(diǎn)贊 //1取消帖子點(diǎn)贊 if (!redisTemplate.opsForSet().isMember(prefix + ':' + key, prefix + ':' + userId)) { //重復(fù)處理 return DataResponse.fail('不能重復(fù)取消哦'); } 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: //數(shù)量+1 //根據(jù)用戶id + 帖子id 查詢r(jià)edis 數(shù)據(jù) if (redisTemplate.opsForSet().isMember(prefix + ':' + key, prefix + ':' + userId)) { //重復(fù)處理 return DataResponse.fail('不能重復(fù)收藏哦'); } //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取出當(dāng)前用戶是否已經(jīng)收藏 if (!redisTemplate.opsForSet().isMember(prefix + ':' + key, prefix + ':' + userId)) { //重復(fù)處理 return DataResponse.fail('不能重復(fù)取消哦'); } //刪除 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://記錄評(píng)論-點(diǎn)贊// OPERATIONTYPE 4: + CLICKTYPE 0 = 給評(píng)論點(diǎn)贊if (null == forumDTO.getCommentId()) { return DataResponse.fail('評(píng)論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();}//贊評(píng)論prefix = 'thumbs:comment';switch (forumDTO.getClickType()) { case 0: /** * 0點(diǎn)贊 * 從redis中獲取數(shù)量 帖子d 例如:177488r88t78r78r7 * count: 0論壇-點(diǎn)贊量 1評(píng)論量 2收藏量 3瀏覽 4評(píng)論-點(diǎn)贊量 * 避免每種數(shù)量都去查詢r(jià)edis 直接通過 redis value 記錄所有的數(shù)量 * 獲取加 + 后的值 */ if (redisTemplate.opsForSet().isMember(prefix + ':' + ckey, prefix + ':' + userId)) { return DataResponse.fail('不能重復(fù)點(diǎn)贊哦'); } else { redisTemplate.opsForSet().add(prefix + ':' + ckey, prefix + ':' + userId); } //set to redis comment.put(ckey, cResetValue(commentNum, true)); break; case 1: //1取消評(píng)論點(diǎn)贊 if (!redisTemplate.opsForSet().isMember(prefix + ':' + ckey, prefix + ':' + userId)) { //重復(fù)處理 return DataResponse.fail('不能重復(fù)取消哦'); } 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代碼:

/** * 功能描述: <br> * 〈點(diǎn)贊數(shù)、收藏?cái)?shù)等數(shù)量重置〉 * @param val 數(shù)組 * @param type 0帖子點(diǎn)贊量 1評(píng)論量 2收藏量 3瀏覽 4評(píng)論點(diǎn)贊量 * @param isPlus 是否增加數(shù)量 true + false - * @Return: java.lang.String * @Author:王震 * @Date: 2020/8/5 10:27 * StringUtils包:import org.apache.commons.lang3.StringUtils; * 可以使用jdk的包替代split方法;但jdk的包需要驗(yàn)證正則,效率較低。 */ 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(); }

總結(jié)

到此這篇關(guān)于springboot +redis 實(shí)現(xiàn)點(diǎn)贊、瀏覽、收藏、評(píng)論等數(shù)量的增減操作的文章就介紹到這了,更多相關(guān)springboot +redis實(shí)現(xiàn)點(diǎn)贊收藏評(píng)論內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 国产精品高清在线 | 国产黄色一级电影 | 日韩一二区 | 欧美日韩精品一区 | 欧美在线一级 | 日韩一区二区在线观看 | 黄色大片免费看 | 欧美日韩在线免费观看 | 大象一区 | 一本久久a久久精品亚洲 | 欧美在线观看一区二区 | 国产高清视频一区 | 毛片入口 | www.天天干.com | 精品国产伦一区二区三区观看方式 | 欧美精品99 | 国产视频一区二区 | 国产免费拔擦拔擦8x高清 | 久久亚洲一区二区三区四区 | 一区二区亚洲 | www.亚洲一区二区三区 | 99亚洲国产精品 | 亚洲视频一区二区三区四区 | 一区二区三区免费观看 | 天天干狠狠操 | 91在线观看视频 | hdfreexxxx中国妞| 欧美区在线 | 午夜精品影院 | 欧美日韩在线免费 | 成年免费大片黄在线观看一级 | 亚洲字幕在线观看 | 狠狠色综合久久丁香婷婷 | 国产精品综合视频 | 国产91观看 | 日本中出视频 | 毛片a级毛片免费播放100 | 狠狠干2020| 蜜桃视频一区二区三区 | 欧美在线观看免费观看视频 | 国产欧美一区二区三区在线看 |