diff --git a/han-note-comment/han-note-comment-biz/pom.xml b/han-note-comment/han-note-comment-biz/pom.xml
index 9281cc6..9ccd397 100644
--- a/han-note-comment/han-note-comment-biz/pom.xml
+++ b/han-note-comment/han-note-comment-biz/pom.xml
@@ -130,6 +130,12 @@
han-note-user-api
+
+
+ com.github.ben-manes.caffeine
+ caffeine
+
+
diff --git a/han-note-comment/han-note-comment-biz/src/main/java/com/hanserwei/hannote/comment/biz/service/impl/CommentServiceImpl.java b/han-note-comment/han-note-comment-biz/src/main/java/com/hanserwei/hannote/comment/biz/service/impl/CommentServiceImpl.java
index 3abda15..35fa263 100644
--- a/han-note-comment/han-note-comment-biz/src/main/java/com/hanserwei/hannote/comment/biz/service/impl/CommentServiceImpl.java
+++ b/han-note-comment/han-note-comment-biz/src/main/java/com/hanserwei/hannote/comment/biz/service/impl/CommentServiceImpl.java
@@ -3,6 +3,8 @@ package com.hanserwei.hannote.comment.biz.service.impl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
@@ -34,7 +36,11 @@ import com.hanserwei.hannote.user.dto.resp.FindUserByIdRspDTO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
-import org.springframework.data.redis.core.*;
+import org.apache.logging.log4j.util.Strings;
+import org.jspecify.annotations.NonNull;
+import org.springframework.data.redis.core.RedisCallback;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
@@ -64,6 +70,15 @@ public class CommentServiceImpl extends ServiceImpl
@Resource(name = "taskExecutor")
private ThreadPoolTaskExecutor threadPoolTaskExecutor;
+ /**
+ * 评论详情本地缓存
+ */
+ private static final Cache LOCAL_CACHE = Caffeine.newBuilder()
+ .initialCapacity(10000) // 设置初始容量为 10000 个条目
+ .maximumSize(10000) // 设置缓存的最大容量为 10000 个条目
+ .expireAfterWrite(1, TimeUnit.HOURS) // 设置缓存条目在写入后 1 小时过期
+ .build();
+
@Override
public Response> publishComment(PublishCommentReqVO publishCommentReqVO) {
// 评论正文
@@ -167,47 +182,83 @@ public class CommentServiceImpl extends ServiceImpl
}
// 分页返回参数
- List commentRspVOS = null;
+ List commentRspVOS;
- // 若评论总数大于0
- if (count > 0) {
- commentRspVOS = Lists.newArrayList();
- // 计算分页查询的offset
- long offset = PageResponse.getOffset(pageNo, pageSize);
- // 评论分页缓存使用 ZSET + STRING 实现
- // 构建评论 ZSET Key
- String commentZSetKey = RedisKeyConstants.buildCommentListKey(noteId);
- // 先判断 ZSET 是否存在
- boolean hasKey = redisTemplate.hasKey(commentZSetKey);
+ commentRspVOS = Lists.newArrayList();
+ // 计算分页查询的offset
+ long offset = PageResponse.getOffset(pageNo, pageSize);
+ // 评论分页缓存使用 ZSET + STRING 实现
+ // 构建评论 ZSET Key
+ String commentZSetKey = RedisKeyConstants.buildCommentListKey(noteId);
+ // 先判断 ZSET 是否存在
+ boolean hasKey = redisTemplate.hasKey(commentZSetKey);
- // 若不存在
- if (!hasKey) {
- // 异步将热点评论同步到 redis 中(最多同步 500 条)
- threadPoolTaskExecutor.execute(() ->
- syncHeatComments2Redis(commentZSetKey, noteId));
- }
+ // 若不存在
+ if (!hasKey) {
+ // 异步将热点评论同步到 redis 中(最多同步 500 条)
+ threadPoolTaskExecutor.execute(() ->
+ syncHeatComments2Redis(commentZSetKey, noteId));
+ }
- // 若 ZSET 缓存存在, 并且查询的是前 50 页的评论
- if (hasKey && offset < 500) {
- // 使用 ZRevRange 获取某篇笔记下,按热度降序排序的一级评论 ID
- Set