Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
81e845f2ef | ||
|
|
db4138896d | ||
|
|
248b14dc96 |
+7
-1
@@ -1,4 +1,4 @@
|
||||
package cn.hezhaohui.thumb.constent;
|
||||
package cn.hezhaohui.thumb.constant;
|
||||
|
||||
|
||||
/**
|
||||
@@ -12,4 +12,10 @@ public interface ThumbConstant {
|
||||
* 用于作为Redis中存储用户点赞信息的key前缀
|
||||
*/
|
||||
String USER_THUMB_KEY_PREFIX = "thumb:";
|
||||
|
||||
/**
|
||||
* 临时点赞前缀常量
|
||||
* 用于作为Redis中存储临时点赞信息的key前缀
|
||||
*/
|
||||
String TEMP_THUMB_KEY_PREFIX = "temp_thumb:%s";
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package cn.hezhaohui.thumb.constent;
|
||||
package cn.hezhaohui.thumb.constant;
|
||||
|
||||
public class UserConstant {
|
||||
|
||||
@@ -2,7 +2,7 @@ package cn.hezhaohui.thumb.controller;
|
||||
|
||||
import cn.hezhaohui.thumb.common.BaseResponse;
|
||||
import cn.hezhaohui.thumb.common.ResultUtils;
|
||||
import cn.hezhaohui.thumb.constent.UserConstant;
|
||||
import cn.hezhaohui.thumb.constant.UserConstant;
|
||||
import cn.hezhaohui.thumb.model.entity.User;
|
||||
import cn.hezhaohui.thumb.service.UserService;
|
||||
import jakarta.annotation.Resource;
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package cn.hezhaohui.thumb.model.enums;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public enum LuaStatusEnum {
|
||||
// 成功
|
||||
SUCCESS(1L),
|
||||
// 失败
|
||||
FAIL(0L);
|
||||
|
||||
private final long value;
|
||||
|
||||
LuaStatusEnum(long value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package cn.hezhaohui.thumb.model.enums;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public enum ThumbTypeEnum {
|
||||
// 点赞
|
||||
INCR(1),
|
||||
// 取消点赞
|
||||
DECR(-1),
|
||||
// 不发生改变
|
||||
NON(0);
|
||||
|
||||
private final int value;
|
||||
|
||||
ThumbTypeEnum(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,10 @@
|
||||
package cn.hezhaohui.thumb.service.impl;
|
||||
|
||||
import cn.hezhaohui.thumb.constent.ThumbConstant;
|
||||
import cn.hezhaohui.thumb.model.entity.User;
|
||||
import cn.hezhaohui.thumb.model.vo.BlogVO;
|
||||
import cn.hezhaohui.thumb.service.ThumbService;
|
||||
import cn.hezhaohui.thumb.service.UserService;
|
||||
import cn.hezhaohui.thumb.util.RedisKeyUtil;
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.util.ObjUtil;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
@@ -56,7 +56,7 @@ public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog>
|
||||
// Blog Id
|
||||
List<Object> blogIdList = blogList.stream().map(blog -> blog.getId().toString()).collect(Collectors.toList());
|
||||
// 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++) {
|
||||
if (thumbList.get(i) == null) {
|
||||
continue;
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package cn.hezhaohui.thumb.service.impl;
|
||||
|
||||
import cn.hezhaohui.thumb.constent.ThumbConstant;
|
||||
import cn.hezhaohui.thumb.exception.BusinessException;
|
||||
import cn.hezhaohui.thumb.exception.ErrorCode;
|
||||
import cn.hezhaohui.thumb.model.dto.DoThumbRequest;
|
||||
@@ -8,6 +7,7 @@ import cn.hezhaohui.thumb.model.entity.Blog;
|
||||
import cn.hezhaohui.thumb.model.entity.User;
|
||||
import cn.hezhaohui.thumb.service.BlogService;
|
||||
import cn.hezhaohui.thumb.service.UserService;
|
||||
import cn.hezhaohui.thumb.util.RedisKeyUtil;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import cn.hezhaohui.thumb.model.entity.Thumb;
|
||||
import cn.hezhaohui.thumb.service.ThumbService;
|
||||
@@ -68,7 +68,7 @@ public class ThumbServiceImpl extends ServiceImpl<ThumbMapper, Thumb>
|
||||
boolean success = update && this.save(thumb);
|
||||
// 点赞记录存入 Redis
|
||||
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;
|
||||
});
|
||||
@@ -87,7 +87,7 @@ public class ThumbServiceImpl extends ServiceImpl<ThumbMapper, Thumb>
|
||||
return transactionTemplate.execute(status -> {
|
||||
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) {
|
||||
throw new BusinessException(ErrorCode.OPERATION_ERROR, "用户未点赞");
|
||||
}
|
||||
@@ -101,7 +101,7 @@ public class ThumbServiceImpl extends ServiceImpl<ThumbMapper, Thumb>
|
||||
// 两者一起执行
|
||||
boolean success = update && this.removeById(thumbId);
|
||||
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;
|
||||
});
|
||||
@@ -110,7 +110,7 @@ public class ThumbServiceImpl extends ServiceImpl<ThumbMapper, Thumb>
|
||||
|
||||
@Override
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package cn.hezhaohui.thumb.service.impl;
|
||||
|
||||
import cn.hezhaohui.thumb.constent.UserConstant;
|
||||
import cn.hezhaohui.thumb.constant.UserConstant;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import cn.hezhaohui.thumb.model.entity.User;
|
||||
import cn.hezhaohui.thumb.service.UserService;
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package cn.hezhaohui.thumb.util;
|
||||
|
||||
import cn.hezhaohui.thumb.constant.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