feat(comment): 一级评论:子评论总数更新与查询

- 新增批量查询评论计数的数据库接口及SQL实现
- 优化本地缓存中评论ID失效判断逻辑,修正变量命名
- 增加从Redis中获取评论计数数据的功能,并支持缺失时回源数据库
- 实现评论计数数据异步同步至Redis的逻辑,包括子评论总数和点赞数
- 在消费端增加更新Redis中评论子评论总数的逻辑
- 添加评论计数相关的Redis Key和Field常量定义
- 更新HTTP测试用例中的评论内容和回复ID,验证计数同步功能
This commit is contained in:
2025-11-08 21:01:15 +08:00
parent 6f22c2b50d
commit e3f9b6a5b5
7 changed files with 230 additions and 10 deletions

View File

@@ -36,6 +36,25 @@ public class RedisKeyConstants {
*/
public static final String FIELD_COLLECT_TOTAL = "collectTotal";
/**
* Hash Field: 子评论总数
*/
public static final String FIELD_CHILD_COMMENT_TOTAL = "childCommentTotal";
/**
* 评论维度计数 Key 前缀
*/
private static final String COUNT_COMMENT_KEY_PREFIX = "count:comment:";
/**
* 构建评论维度计数 Key
*
* @param commentId 评论ID
* @return 评论维度计数 Key
*/
public static String buildCountCommentKey(Long commentId) {
return COUNT_COMMENT_KEY_PREFIX + commentId;
}
/**
* 构建用户维度计数 Key
*

View File

@@ -5,6 +5,7 @@ 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.CommentDOMapper;
import com.hanserwei.hannote.count.biz.enums.CommentLevelEnum;
import com.hanserwei.hannote.count.biz.model.dto.CountPublishCommentMqDTO;
@@ -15,6 +16,7 @@ import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;
@@ -39,6 +41,9 @@ public class CountNoteChildCommentConsumer implements RocketMQListener<String> {
@Resource
private CommentDOMapper commentDOMapper;
@Resource
private RedisTemplate<String, Object> redisTemplate;
private final BufferTrigger<String> bufferTrigger = BufferTrigger.<String>batchBlocking()
.bufferSize(50000) // 缓存队列的最大容量
.batchSize(1000) // 一批次最多聚合 1000 条
@@ -82,6 +87,19 @@ public class CountNoteChildCommentConsumer implements RocketMQListener<String> {
// 评论数
int count = CollUtil.size(entry.getValue());
// 更新 Redis 缓存中的评论计数数据
// 构建 Key
String commentCountHashKey = RedisKeyConstants.buildCountCommentKey(parentId);
// 判断 Hash 是否存在
boolean hasKey = redisTemplate.hasKey(commentCountHashKey);
// 若 Hash 存在,则更新子评论总数
if (hasKey) {
// 累加
redisTemplate.opsForHash()
.increment(commentCountHashKey, RedisKeyConstants.FIELD_CHILD_COMMENT_TOTAL, count);
}
// 更新一级评论的下级评论总数,进行累加操作
commentDOMapper.updateChildCommentTotal(parentId, count);
}