From 51cebf62156837e0755061d8196f24bf8ea589ac Mon Sep 17 00:00:00 2001 From: Hanserwei <2628273921@qq.com> Date: Sat, 8 Nov 2025 22:06:26 +0800 Subject: [PATCH] =?UTF-8?q?feat(count):=20=E6=96=B0=E5=A2=9E=E7=AC=94?= =?UTF-8?q?=E8=AE=B0=E8=AF=84=E8=AE=BA=E6=95=B0=E7=BC=93=E5=AD=98=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 CountNoteCommentConsumer 中引入 RedisTemplate依赖 - 消费评论消息时,更新 Redis 中笔记评论总数 - 新增 RedisKeyConstants.FIELD_COMMENT_TOTAL 常量定义 - 实现基于 Hash 的评论数累加更新机制 - 优化评论数更新流程,支持批量处理与缓存同步 --- .../count/biz/constant/RedisKeyConstants.java | 5 +++++ .../biz/consumer/CountNoteCommentConsumer.java | 17 +++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/han-note-count/han-note-count-biz/src/main/java/com/hanserwei/hannote/count/biz/constant/RedisKeyConstants.java b/han-note-count/han-note-count-biz/src/main/java/com/hanserwei/hannote/count/biz/constant/RedisKeyConstants.java index 1e64417..4c12a0e 100644 --- a/han-note-count/han-note-count-biz/src/main/java/com/hanserwei/hannote/count/biz/constant/RedisKeyConstants.java +++ b/han-note-count/han-note-count-biz/src/main/java/com/hanserwei/hannote/count/biz/constant/RedisKeyConstants.java @@ -31,6 +31,11 @@ public class RedisKeyConstants { */ private static final String COUNT_NOTE_KEY_PREFIX = "count:note:"; + /** + * Hash Field: 笔记评论总数 + */ + public static final String FIELD_COMMENT_TOTAL = "commentTotal"; + /** * Hash Field: 笔记收藏总数 */ diff --git a/han-note-count/han-note-count-biz/src/main/java/com/hanserwei/hannote/count/biz/consumer/CountNoteCommentConsumer.java b/han-note-count/han-note-count-biz/src/main/java/com/hanserwei/hannote/count/biz/consumer/CountNoteCommentConsumer.java index 406fe8d..57dc6e9 100644 --- a/han-note-count/han-note-count-biz/src/main/java/com/hanserwei/hannote/count/biz/consumer/CountNoteCommentConsumer.java +++ b/han-note-count/han-note-count-biz/src/main/java/com/hanserwei/hannote/count/biz/consumer/CountNoteCommentConsumer.java @@ -5,12 +5,14 @@ import com.github.phantomthief.collection.BufferTrigger; import com.google.common.collect.Lists; import com.hanserwei.framework.common.utils.JsonUtils; import com.hanserwei.hannote.count.biz.constant.MQConstants; +import com.hanserwei.hannote.count.biz.constant.RedisKeyConstants; import com.hanserwei.hannote.count.biz.domain.mapper.NoteCountDOMapper; import com.hanserwei.hannote.count.biz.model.dto.CountPublishCommentMqDTO; import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; import org.apache.rocketmq.spring.annotation.RocketMQMessageListener; import org.apache.rocketmq.spring.core.RocketMQListener; +import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; import java.time.Duration; @@ -27,6 +29,8 @@ public class CountNoteCommentConsumer implements RocketMQListener { @Resource private NoteCountDOMapper noteCountDOMapper; + @Resource + private RedisTemplate redisTemplate; private final BufferTrigger bufferTrigger = BufferTrigger.batchBlocking() .bufferSize(50000) // 缓存队列的最大容量 @@ -67,6 +71,19 @@ public class CountNoteCommentConsumer implements RocketMQListener { // 评论数 int count = CollUtil.size(entry.getValue()); + // 更新 Redis 缓存中的笔记评论总数 + // 构建 Key + String noteCountHashKey = RedisKeyConstants.buildCountNoteKey(noteId); + // 判断 Hash 是否存在 + boolean hasKey = redisTemplate.hasKey(noteCountHashKey); + + // 若 Hash 存在 + if (hasKey) { + // 累加更新 + redisTemplate.opsForHash() + .increment(noteCountHashKey, RedisKeyConstants.FIELD_COMMENT_TOTAL, count); + } + // 若评论数大于零,则执行更新操作:累加评论总数 if (count > 0) { noteCountDOMapper.insertOrUpdateCommentTotalByNoteId(count, noteId);