🔄Update: 封装 RedisKey 的构造获取方法
This commit is contained in:
@@ -12,4 +12,10 @@ public interface ThumbConstant {
|
|||||||
* 用于作为Redis中存储用户点赞信息的key前缀
|
* 用于作为Redis中存储用户点赞信息的key前缀
|
||||||
*/
|
*/
|
||||||
String USER_THUMB_KEY_PREFIX = "thumb:";
|
String USER_THUMB_KEY_PREFIX = "thumb:";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 临时点赞前缀常量
|
||||||
|
* 用于作为Redis中存储临时点赞信息的key前缀
|
||||||
|
*/
|
||||||
|
String TEMP_THUMB_KEY_PREFIX = "temp_thumb:%s";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import cn.hezhaohui.thumb.model.entity.User;
|
|||||||
import cn.hezhaohui.thumb.model.vo.BlogVO;
|
import cn.hezhaohui.thumb.model.vo.BlogVO;
|
||||||
import cn.hezhaohui.thumb.service.ThumbService;
|
import cn.hezhaohui.thumb.service.ThumbService;
|
||||||
import cn.hezhaohui.thumb.service.UserService;
|
import cn.hezhaohui.thumb.service.UserService;
|
||||||
|
import cn.hezhaohui.thumb.util.RedisKeyUtil;
|
||||||
import cn.hutool.core.bean.BeanUtil;
|
import cn.hutool.core.bean.BeanUtil;
|
||||||
import cn.hutool.core.util.ObjUtil;
|
import cn.hutool.core.util.ObjUtil;
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
@@ -56,7 +57,7 @@ public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog>
|
|||||||
// Blog Id
|
// Blog Id
|
||||||
List<Object> blogIdList = blogList.stream().map(blog -> blog.getId().toString()).collect(Collectors.toList());
|
List<Object> blogIdList = blogList.stream().map(blog -> blog.getId().toString()).collect(Collectors.toList());
|
||||||
// Get Thumb-up Data
|
// Get Thumb-up Data
|
||||||
List<Object> thumbList = redisTemplate.opsForHash().multiGet(ThumbConstant.USER_THUMB_KEY_PREFIX + loginUser.getId(), blogIdList);
|
List<Object> thumbList = redisTemplate.opsForHash().multiGet(RedisKeyUtil.getUserThumbKey(loginUser.getId()), blogIdList);
|
||||||
for (int i = 0; i < thumbList.size(); i++) {
|
for (int i = 0; i < thumbList.size(); i++) {
|
||||||
if (thumbList.get(i) == null) {
|
if (thumbList.get(i) == null) {
|
||||||
continue;
|
continue;
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import cn.hezhaohui.thumb.model.entity.Blog;
|
|||||||
import cn.hezhaohui.thumb.model.entity.User;
|
import cn.hezhaohui.thumb.model.entity.User;
|
||||||
import cn.hezhaohui.thumb.service.BlogService;
|
import cn.hezhaohui.thumb.service.BlogService;
|
||||||
import cn.hezhaohui.thumb.service.UserService;
|
import cn.hezhaohui.thumb.service.UserService;
|
||||||
|
import cn.hezhaohui.thumb.util.RedisKeyUtil;
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
import cn.hezhaohui.thumb.model.entity.Thumb;
|
import cn.hezhaohui.thumb.model.entity.Thumb;
|
||||||
import cn.hezhaohui.thumb.service.ThumbService;
|
import cn.hezhaohui.thumb.service.ThumbService;
|
||||||
@@ -68,7 +69,7 @@ public class ThumbServiceImpl extends ServiceImpl<ThumbMapper, Thumb>
|
|||||||
boolean success = update && this.save(thumb);
|
boolean success = update && this.save(thumb);
|
||||||
// 点赞记录存入 Redis
|
// 点赞记录存入 Redis
|
||||||
if (success) {
|
if (success) {
|
||||||
redisTemplate.opsForHash().put(ThumbConstant.USER_THUMB_KEY_PREFIX + loginUser.getId().toString(), blogId.toString(), thumb.getId());
|
redisTemplate.opsForHash().put(RedisKeyUtil.getUserThumbKey(loginUser.getId()), blogId.toString(), thumb.getId());
|
||||||
}
|
}
|
||||||
return success;
|
return success;
|
||||||
});
|
});
|
||||||
@@ -87,7 +88,7 @@ public class ThumbServiceImpl extends ServiceImpl<ThumbMapper, Thumb>
|
|||||||
return transactionTemplate.execute(status -> {
|
return transactionTemplate.execute(status -> {
|
||||||
Long blogId = doThumbRequest.getBlogId();
|
Long blogId = doThumbRequest.getBlogId();
|
||||||
// 判断是否已点过赞
|
// 判断是否已点过赞
|
||||||
Object thumbIdObj = redisTemplate.opsForHash().get(ThumbConstant.USER_THUMB_KEY_PREFIX + loginUser.getId().toString(), blogId.toString());
|
Object thumbIdObj = redisTemplate.opsForHash().get(RedisKeyUtil.getUserThumbKey(loginUser.getId()), blogId.toString());
|
||||||
if (thumbIdObj == null) {
|
if (thumbIdObj == null) {
|
||||||
throw new BusinessException(ErrorCode.OPERATION_ERROR, "用户未点赞");
|
throw new BusinessException(ErrorCode.OPERATION_ERROR, "用户未点赞");
|
||||||
}
|
}
|
||||||
@@ -101,7 +102,7 @@ public class ThumbServiceImpl extends ServiceImpl<ThumbMapper, Thumb>
|
|||||||
// 两者一起执行
|
// 两者一起执行
|
||||||
boolean success = update && this.removeById(thumbId);
|
boolean success = update && this.removeById(thumbId);
|
||||||
if (success) {
|
if (success) {
|
||||||
redisTemplate.opsForHash().delete(ThumbConstant.USER_THUMB_KEY_PREFIX + loginUser.getId(), blogId.toString());
|
redisTemplate.opsForHash().delete(RedisKeyUtil.getUserThumbKey(loginUser.getId()), blogId.toString());
|
||||||
}
|
}
|
||||||
return success;
|
return success;
|
||||||
});
|
});
|
||||||
@@ -110,7 +111,7 @@ public class ThumbServiceImpl extends ServiceImpl<ThumbMapper, Thumb>
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Boolean hasThumb(Long blogId, Long userId) {
|
public Boolean hasThumb(Long blogId, Long userId) {
|
||||||
return redisTemplate.opsForHash().hasKey(ThumbConstant.USER_THUMB_KEY_PREFIX + userId, blogId.toString());
|
return redisTemplate.opsForHash().hasKey(RedisKeyUtil.getUserThumbKey(userId), blogId.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package cn.hezhaohui.thumb.util;
|
||||||
|
|
||||||
|
import cn.hezhaohui.thumb.constent.ThumbConstant;
|
||||||
|
|
||||||
|
public class RedisKeyUtil {
|
||||||
|
|
||||||
|
public static String getUserThumbKey(Long userId) {
|
||||||
|
return ThumbConstant.USER_THUMB_KEY_PREFIX + userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getTempThumbKey(String time) {
|
||||||
|
return ThumbConstant.TEMP_THUMB_KEY_PREFIX.formatted(time);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user