最近做了一個(gè)帖子的收藏、點(diǎn)贊數(shù)量的功能,其實(shí)之前也做過(guò)類(lèi)似的功能,因?yàn)橹耙恢笔褂玫膍ysql 總是感覺(jué)對(duì)于這種頻繁需要改變的值,不應(yīng)該給予Mysql過(guò)大的壓力,本文章采用的是redis 做了持久化。下面貼出關(guān)鍵代碼:DataResponse是項(xiàng)目中使用的結(jié)果封裝實(shí)體類(lèi);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
BoundHashOperationsString, 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)) {
//無(wú)則set
post.put(key, DEFAULT_VALUE);
//再取出來(lái)賦值給 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 直接通過(guò) 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";
ListMqMessage> 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();
BoundHashOperationsString, Object, Object> comment = redisTemplate.boundHashOps("post:comment");
if (null == comment.get(ckey)) {
//無(wú)則set
comment.put(ckey, "0");
//再取出來(lái)賦值給 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 直接通過(guò) 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);
}
/**
* 功能描述: 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();
}
到此這篇關(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)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!