fix: optimize journal list redis cache

main
wangqing 11 months ago
parent eb6055d477
commit be42cc9432

@ -8,7 +8,7 @@ public class Constants {
public static final String REDIS_KEY_USER_COLLECT_JOURNAL = "redis_key_user_collect_journal_";
public static final String REDIS_KEY_PAGE_JOURNAL_RESPONSE_DTO = "redis_key_page_journal_response_dto_";
public static final String REDIS_KEY_PAGE_JOURNAL_RESPONSE_DTO = "redis_key_page_journal_response_dto___";
public static final String TOKEN_PREFIX = "Bearer ";
public static final int TOKEN_PREFIX_LENGTH = TOKEN_PREFIX.length();

@ -21,48 +21,72 @@ public class CommentMapper {
* 5...
*/
CommentDTO commentDTO=new CommentDTO();
List<Comment> commentList = (List<Comment>) redisTemplate.opsForValue().get("JOURNAL_TOP5_COMMENT___"+journalId);
if (null!= commentList) {
commentDTO.setCommentList(commentList);
} else {
commentList = commentDao.findTop5ByJournalIdOrderByThumbupCountDesc(journalId);
commentDTO.setCommentList(commentList);
redisTemplate.opsForValue().set("JOURNAL_TOP5_COMMENT___"+journalId,commentList,6, TimeUnit.HOURS);
}
// List<Comment> commentList = (List<Comment>) redisTemplate.opsForValue().get("JOURNAL_TOP5_COMMENT___"+journalId);
// if (null!= commentList) {
// commentDTO.setCommentList(commentList);
// } else {
// commentList = commentDao.findTop5ByJournalIdOrderByThumbupCountDesc(journalId);
// commentDTO.setCommentList(commentList);
// redisTemplate.opsForValue().set("JOURNAL_TOP5_COMMENT___"+journalId,commentList,6, TimeUnit.HOURS);
// }
List<Comment> commentList =commentDao.findTop5ByJournalIdOrderByThumbupCountDesc(journalId);
commentDTO.setCommentList(commentList);
/**
*
*/
String totalString = (String) redisTemplate.opsForValue().get("JOURNAL_TATAL_COMMENT_COUNT__"+journalId);
if (StringUtils.isNotBlank(totalString)) {
commentDTO.setTotalCommentReply(totalString);
}else {
totalString = "0";
Criteria criteria = Criteria.where("journalId").is(journalId);
Aggregation agg = Aggregation.newAggregation(Aggregation.match(criteria), // 匹配条件
Aggregation.group().sum("commentCount").as("totalComment"));
AggregationResults<TotalCommentVo> results = mongoTemplate.aggregate(agg, "comment", TotalCommentVo.class);
TotalCommentVo totalCommentVo = results.getUniqueMappedResult();
commentDTO.setTotalCommentReply("0");
List<Comment> list = commentDao.findByJournalId(journalId);
int total = 0;
if (null != list && list.size() > 0) {
total = list.size();
}
if (null != totalCommentVo) {
total = total + totalCommentVo.getTotalComment();
totalString = total +"";
if (total > 99) {
totalString = "99+";
}
// String totalString = (String) redisTemplate.opsForValue().get("JOURNAL_TATAL_COMMENT_COUNT__"+journalId);
String totalString = "0";
// if (StringUtils.isNotBlank(totalString)) {
// commentDTO.setTotalCommentReply(totalString);
// }else {
//// totalString = "0";
//// Criteria criteria = Criteria.where("journalId").is(journalId);
//// Aggregation agg = Aggregation.newAggregation(Aggregation.match(criteria), // 匹配条件
//// Aggregation.group().sum("commentCount").as("totalComment"));
//// AggregationResults<TotalCommentVo> results = mongoTemplate.aggregate(agg, "comment", TotalCommentVo.class);
//// TotalCommentVo totalCommentVo = results.getUniqueMappedResult();
//// commentDTO.setTotalCommentReply("0");
//// List<Comment> list = commentDao.findByJournalId(journalId);
//// int total = 0;
//// if (null != list && list.size() > 0) {
//// total = list.size();
//// }
//// if (null != totalCommentVo) {
//// total = total + totalCommentVo.getTotalComment();
//// totalString = total +"";
//// if (total > 99) {
//// totalString = "99+";
//// }
//// }
//// commentDTO.setTotalCommentReply(totalString);
//// commentDTO.setTotalCommentReplyInt(total);
//// redisTemplate.opsForValue().set("JOURNAL_TATAL_COMMENT_COUNT__"+journalId,totalString,6,TimeUnit.HOURS);
// }
totalString = "0";
Criteria criteria = Criteria.where("journalId").is(journalId);
Aggregation agg = Aggregation.newAggregation(Aggregation.match(criteria), // 匹配条件
Aggregation.group().sum("commentCount").as("totalComment"));
AggregationResults<TotalCommentVo> results = mongoTemplate.aggregate(agg, "comment", TotalCommentVo.class);
TotalCommentVo totalCommentVo = results.getUniqueMappedResult();
commentDTO.setTotalCommentReply("0");
List<Comment> list = commentDao.findByJournalId(journalId);
int total = 0;
if (null != list && list.size() > 0) {
total = list.size();
}
if (null != totalCommentVo) {
total = total + totalCommentVo.getTotalComment();
totalString = total +"";
if (total > 99) {
totalString = "99+";
}
commentDTO.setTotalCommentReply(totalString);
commentDTO.setTotalCommentReplyInt(total);
redisTemplate.opsForValue().set("JOURNAL_TATAL_COMMENT_COUNT__"+journalId,totalString,6,TimeUnit.HOURS);
}
commentDTO.setTotalCommentReply(totalString);
commentDTO.setTotalCommentReplyInt(total);
return commentDTO;
}
}

Loading…
Cancel
Save