✨Feat: 使用Redis优化点赞查询性能
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
package cn.hezhaohui.thumb.constent;
|
||||
|
||||
|
||||
/**
|
||||
* 点赞常量接口
|
||||
* 用于定义与点赞功能相关的常量
|
||||
*/
|
||||
public interface ThumbConstant {
|
||||
|
||||
/**
|
||||
* 用户点赞前缀常量
|
||||
* 用于作为Redis中存储用户点赞信息的key前缀
|
||||
*/
|
||||
String USER_THUMB_KEY_PREFIX = "thumb:";
|
||||
}
|
||||
@@ -16,4 +16,6 @@ public interface ThumbService extends IService<Thumb> {
|
||||
Boolean doThumb(DoThumbRequest doThumbRequest, HttpServletRequest request);
|
||||
|
||||
Boolean undoThumb(DoThumbRequest doThumbRequest, HttpServletRequest request);
|
||||
|
||||
Boolean hasThumb(Long blogId, Long userId);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package cn.hezhaohui.thumb.service.impl;
|
||||
|
||||
import cn.hezhaohui.thumb.model.entity.Thumb;
|
||||
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;
|
||||
@@ -14,12 +14,12 @@ import cn.hezhaohui.thumb.mapper.BlogMapper;
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@@ -38,9 +38,8 @@ public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog>
|
||||
@Lazy
|
||||
private ThumbService thumbService;
|
||||
|
||||
public BlogServiceImpl(UserService userService) {
|
||||
this.userService = userService;
|
||||
}
|
||||
@Resource
|
||||
private RedisTemplate<String, Object> redisTemplate;
|
||||
|
||||
@Override
|
||||
public BlogVO getBlogVOById(long blogId, HttpServletRequest request) {
|
||||
@@ -55,13 +54,15 @@ public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog>
|
||||
Map<Long, Boolean> blogIdHasThumbMap = new HashMap<>();
|
||||
if (ObjUtil.isNotEmpty(loginUser)) {
|
||||
// Blog Id
|
||||
Set<Long> blogIdSet = blogList.stream().map(Blog::getId).collect(Collectors.toSet());
|
||||
List<Object> blogIdList = blogList.stream().map(blog -> blog.getId().toString()).collect(Collectors.toList());
|
||||
// Get Thumb-up Data
|
||||
List<Thumb> thumbList = thumbService.lambdaQuery()
|
||||
.eq(Thumb::getUserid, loginUser.getId())
|
||||
.in(Thumb::getBlogId, blogIdSet)
|
||||
.list();
|
||||
thumbList.forEach(blogThumb -> blogIdHasThumbMap.put(blogThumb.getBlogId(), true));
|
||||
List<Object> thumbList = redisTemplate.opsForHash().multiGet(ThumbConstant.USER_THUMB_KEY_PREFIX + loginUser.getId(), blogIdList);
|
||||
for (int i = 0; i < thumbList.size(); i++) {
|
||||
if (thumbList.get(i) == null) {
|
||||
continue;
|
||||
}
|
||||
blogIdHasThumbMap.put(Long.valueOf(blogIdList.get(i).toString()), true);
|
||||
}
|
||||
}
|
||||
return blogList.stream()
|
||||
.map(blog -> {
|
||||
@@ -80,11 +81,8 @@ public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog>
|
||||
return blogVO;
|
||||
}
|
||||
|
||||
Thumb thumb = thumbService.lambdaQuery()
|
||||
.eq(Thumb::getUserid, loginUser.getId())
|
||||
.eq(Thumb::getBlogId, blog.getId())
|
||||
.one();
|
||||
blogVO.setHasThumb(thumb != null);
|
||||
Boolean exist = thumbService.hasThumb(blog.getId(), loginUser.getId());
|
||||
blogVO.setHasThumb(exist);
|
||||
|
||||
return blogVO;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
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;
|
||||
@@ -13,6 +14,7 @@ import cn.hezhaohui.thumb.service.ThumbService;
|
||||
import cn.hezhaohui.thumb.mapper.ThumbMapper;
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
|
||||
@@ -34,6 +36,9 @@ public class ThumbServiceImpl extends ServiceImpl<ThumbMapper, Thumb>
|
||||
@Resource
|
||||
private TransactionTemplate transactionTemplate;
|
||||
|
||||
@Resource
|
||||
private RedisTemplate<String, Object> redisTemplate;
|
||||
|
||||
@Override
|
||||
public Boolean doThumb(DoThumbRequest doThumbRequest, HttpServletRequest request) {
|
||||
if (doThumbRequest == null || doThumbRequest.getBlogId() == null) {
|
||||
@@ -46,10 +51,7 @@ public class ThumbServiceImpl extends ServiceImpl<ThumbMapper, Thumb>
|
||||
return transactionTemplate.execute(status -> {
|
||||
Long blogId = doThumbRequest.getBlogId();
|
||||
// 判断是否已点过赞
|
||||
boolean exists = this.lambdaQuery()
|
||||
.eq(Thumb::getUserid, loginUser.getId())
|
||||
.eq(Thumb::getBlogId, blogId)
|
||||
.exists();
|
||||
Boolean exists = this.hasThumb(blogId, loginUser.getId());
|
||||
if (exists) {
|
||||
throw new BusinessException(ErrorCode.OPERATION_ERROR, "用户已点赞");
|
||||
}
|
||||
@@ -63,7 +65,12 @@ public class ThumbServiceImpl extends ServiceImpl<ThumbMapper, Thumb>
|
||||
thumb.setUserid(loginUser.getId());
|
||||
thumb.setBlogId(blogId);
|
||||
// 两者一起执行
|
||||
return update && this.save(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());
|
||||
}
|
||||
return success;
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -80,11 +87,9 @@ public class ThumbServiceImpl extends ServiceImpl<ThumbMapper, Thumb>
|
||||
return transactionTemplate.execute(status -> {
|
||||
Long blogId = doThumbRequest.getBlogId();
|
||||
// 判断是否已点过赞
|
||||
Thumb thumb = this.lambdaQuery()
|
||||
.eq(Thumb::getUserid, loginUser.getId())
|
||||
.eq(Thumb::getBlogId, blogId)
|
||||
.one();
|
||||
if (thumb == null) {
|
||||
Object thumbIdObj = redisTemplate.opsForHash().get(ThumbConstant.USER_THUMB_KEY_PREFIX + loginUser.getId().toString(), blogId.toString());
|
||||
Long thumbId = thumbIdObj == null ? null : ((Number) thumbIdObj).longValue();
|
||||
if (thumbId == null) {
|
||||
throw new BusinessException(ErrorCode.OPERATION_ERROR, "用户未点赞");
|
||||
}
|
||||
// 更新帖子点赞计数器
|
||||
@@ -94,10 +99,19 @@ public class ThumbServiceImpl extends ServiceImpl<ThumbMapper, Thumb>
|
||||
.update();
|
||||
// 更新点赞表数据
|
||||
// 两者一起执行
|
||||
return update && this.removeById(thumb.getId());
|
||||
boolean success = update && this.removeById(thumbId);
|
||||
if (success) {
|
||||
redisTemplate.opsForHash().delete(ThumbConstant.USER_THUMB_KEY_PREFIX + loginUser.getId(), blogId.toString());
|
||||
}
|
||||
return success;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean hasThumb(Long blogId, Long userId) {
|
||||
return redisTemplate.opsForHash().hasKey(ThumbConstant.USER_THUMB_KEY_PREFIX + userId, blogId.toString());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user