From c150bb5b6a7ae62ca863f2265384390c27eda374 Mon Sep 17 00:00:00 2001 From: wangqing <408564126@qq.com> Date: Tue, 6 Feb 2024 18:43:35 +0800 Subject: [PATCH] optimize journal list performance --- .../music/controller/JournalController.java | 10 ++- .../luoo/music/dto/mapper/JournalMapper.java | 74 ++++++++++++++++--- 2 files changed, 70 insertions(+), 14 deletions(-) diff --git a/luoo_music/src/main/java/com/luoo/music/controller/JournalController.java b/luoo_music/src/main/java/com/luoo/music/controller/JournalController.java index 4b5cf48..1c0c2fc 100644 --- a/luoo_music/src/main/java/com/luoo/music/controller/JournalController.java +++ b/luoo_music/src/main/java/com/luoo/music/controller/JournalController.java @@ -19,6 +19,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.redis.core.RedisTemplate; import org.springframework.web.bind.annotation.*; import com.luoo.music.pojo.Journal; import com.luoo.music.pojo.Tag; @@ -61,6 +62,9 @@ public class JournalController { @Autowired private CommentDao commentDao; + + @Autowired + private RedisTemplate redisTemplate; private static final int JOURNAL_FILTER_NUMBER_RANGE=100; @@ -80,7 +84,7 @@ public class JournalController { List ids=pageList.stream().map(Journal::getId).collect(Collectors.toList()); Set journalCollectSet = null == user ? Collections.emptySet() : userCollectInfoService.getCollectSet(user.getUserId(),ids, CollectTypeEnum.JOURNAL); - List list = pageList.stream().map(a -> JournalMapper.getJournalRespDTO(a, journalCollectSet,mongoTemplate,commentDao)) + List list = pageList.stream().map(a -> JournalMapper.getJournalRespDTO(a, journalCollectSet,mongoTemplate,redisTemplate,commentDao)) .collect(Collectors.toList()); return Result.success(new PageResult(Long.valueOf(list.size()), list)); } @@ -103,7 +107,7 @@ public class JournalController { } List pageList = journalService.orderByField(objectIds); Set journalCollectSet = new HashSet<>(objectIds); - List list = pageList.stream().map(a -> JournalMapper.getJournalRespDTO(a, journalCollectSet,mongoTemplate,commentDao)) + List list = pageList.stream().map(a -> JournalMapper.getJournalRespDTO(a, journalCollectSet,mongoTemplate,redisTemplate,commentDao)) .collect(Collectors.toList()); return Result.success(new PageResult(Long.valueOf(list.size()), list)); } @@ -119,7 +123,7 @@ public class JournalController { return Result.unauthorized(null); } Journal journal = journalService.findById(id); - JournalRespDTO journalRespDTO=JournalMapper.getJournalRespDTO(journal, Collections.emptySet(),mongoTemplate,commentDao); + JournalRespDTO journalRespDTO=JournalMapper.getJournalRespDTO(journal, Collections.emptySet(),mongoTemplate,redisTemplate,commentDao); boolean isCollect=null == user ? false:userCollectInfoService.isCollect(user.getUserId(),id, CollectTypeEnum.JOURNAL); journalRespDTO.setHaveCollect(isCollect); return Result.success(journalRespDTO); diff --git a/luoo_music/src/main/java/com/luoo/music/dto/mapper/JournalMapper.java b/luoo_music/src/main/java/com/luoo/music/dto/mapper/JournalMapper.java index 31795a1..8525d95 100644 --- a/luoo_music/src/main/java/com/luoo/music/dto/mapper/JournalMapper.java +++ b/luoo_music/src/main/java/com/luoo/music/dto/mapper/JournalMapper.java @@ -11,24 +11,31 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; +import org.apache.commons.lang.StringUtils; import org.springframework.core.io.ClassPathResource; import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.core.aggregation.Aggregation; +import org.springframework.data.mongodb.core.aggregation.AggregationResults; +import org.springframework.data.mongodb.core.query.Criteria; + import com.apifan.common.random.RandomSource; import com.apifan.common.random.entity.Poem; import com.luoo.music.dao.CommentDao; -import com.luoo.music.dto.response.CommentDTO; import com.luoo.music.dto.response.JournalRespDTO; +import com.luoo.music.dto.response.TotalCommentVo; +import com.luoo.music.pojo.Comment; import com.luoo.music.pojo.Journal; import constants.Constants; import enums.DateTimePatternEnum; +import org.springframework.data.redis.core.RedisTemplate; import util.DateUtil; import util.StringTools; public class JournalMapper { // mock data - private static final List DEFAULT_TAGS=Arrays.asList("暂无标签"); private static final String JOURNAL_TAG_FILE_PATH = "journalTags.txt"; private static final Map> journalTagMap = new HashMap<>(); private static final String[] EDITOR = new String[] { "左岸以西", "落在低处" }; @@ -52,9 +59,9 @@ public class JournalMapper { } public static JournalRespDTO getJournalRespDTO(Journal journal) { - return getJournalRespDTO(journal, Collections.emptySet()); + return getJournalRespDTO(journal,Collections.emptySet()); } - + public static JournalRespDTO getJournalRespDTO(Journal journal, Set journalCollectSet) { JournalRespDTO journalRespDTO = new JournalRespDTO(); journalRespDTO.setId(journal.getId()); @@ -78,13 +85,58 @@ public class JournalMapper { } return journalRespDTO; } - public static JournalRespDTO getJournalRespDTO(Journal journal, Set journalCollectSet, - MongoTemplate mongoTemplate, CommentDao commentDao) { - JournalRespDTO journalRespDTO = getJournalRespDTO(journal, journalCollectSet); - CommentDTO commentDTO = CommentMapper.getCommentDTO(journalRespDTO.getId(), mongoTemplate, commentDao); - journalRespDTO.setCommentList(commentDTO.getCommentList()); - journalRespDTO.setTotalCommentReply(commentDTO.getTotalCommentReply()); + MongoTemplate mongoTemplate, RedisTemplate redisTemplate,CommentDao commentDao) { + JournalRespDTO journalRespDTO = getJournalRespDTO(journal,journalCollectSet); + + /** + * 显示期刊点赞最多5条评论,该五条评论随机显示,每次进入刷新新,超过部分显示为“...”点击评论进入期刊详情页并自动定位到该评论,点击用户头像同上,若为空数据则显示“暂无评论,快去抢沙发吧! + */ + + List commentList = (List) redisTemplate.opsForValue().get("JOURNAL_TOP5_COMMENT___"+journal.getId()); + if (null!= commentList) { + journalRespDTO.setCommentList(commentList); + } else { + commentList = commentDao.findTop5ByJournalIdOrderByThumbupCountDesc(journal.getId()); + journalRespDTO.setCommentList(commentList); + redisTemplate.opsForValue().set("JOURNAL_TOP5_COMMENT___"+journal.getId(),commentList,6, TimeUnit.HOURS); + } + +// List commentList = commentDao.findTop5ByJournalIdOrderByThumbupCountDesc(journal.getId()); +// +// journalRespDTO.setCommentList(commentList); + + /** + * 获取期刊总评论数 一级评论数加上父评论的总回复数 + */ + + String totalString = (String) redisTemplate.opsForValue().get("JOURNAL_TATAL_COMMENT_COUNT___"+journal.getId()); + if (StringUtils.isNotBlank(totalString)) { + journalRespDTO.setTotalCommentReply(totalString); + }else { + totalString = "0"; + Criteria criteria = Criteria.where("journalId").is(journal.getId()); + Aggregation agg = Aggregation.newAggregation(Aggregation.match(criteria), // 匹配条件 + Aggregation.group().sum("commentCount").as("totalComment")); + AggregationResults results = mongoTemplate.aggregate(agg, "comment", TotalCommentVo.class); + TotalCommentVo totalCommentVo = results.getUniqueMappedResult(); + journalRespDTO.setTotalCommentReply("0"); + List list = commentDao.findByJournalId(journal.getId()); + 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+"; + } + } + journalRespDTO.setTotalCommentReply(totalString); + } + + return journalRespDTO; } @@ -94,7 +146,7 @@ public class JournalMapper { } private static List getTags(String journalNo) { - return journalTagMap.getOrDefault(journalNo, DEFAULT_TAGS); + return journalTagMap.getOrDefault(journalNo, Collections.emptyList()); } private static String getEditDate(Journal journal) {