diff --git a/luoo_comment/src/main/java/com/luoo/comment/controller/CommentController.java b/luoo_comment/src/main/java/com/luoo/comment/controller/CommentController.java index 93785c3..5236869 100644 --- a/luoo_comment/src/main/java/com/luoo/comment/controller/CommentController.java +++ b/luoo_comment/src/main/java/com/luoo/comment/controller/CommentController.java @@ -393,6 +393,7 @@ public class CommentController extends BaseController { Comment comment = commentService.findById(commentId); cacheChannel.clear(Constants.J2CACHE_REGION_JOURNAL_COMMENT_PAGE_HOT+"_"+comment.getRootJournalId()); cacheChannel.clear(Constants.J2CACHE_REGION_JOURNAL_COMMENT_PAGE_NEW+"_"+comment.getRootJournalId()); + cacheChannel.evict(Constants.J2CACHE_REGION_JOURNAL_HOT_COMMENTS,comment.getRootJournalId()); if(StringUtils.isNotEmpty(comment.getParentId())) { cacheChannel.clear(Constants.J2CACHE_REGION_JOURNAL_COMMENT_PAGE_CHILDREN+"_"+comment.getParentId()); diff --git a/luoo_comment/src/main/java/com/luoo/comment/dao/CommentDao.java b/luoo_comment/src/main/java/com/luoo/comment/dao/CommentDao.java index ec7b5ce..161b17a 100644 --- a/luoo_comment/src/main/java/com/luoo/comment/dao/CommentDao.java +++ b/luoo_comment/src/main/java/com/luoo/comment/dao/CommentDao.java @@ -14,6 +14,7 @@ public interface CommentDao extends MongoRepository { public Page findByJournalId(String journalId, Pageable pageable); public Page findByJournalIdOrderByPublishTimeDesc(String journalId, Pageable pageable); public Page findByJournalIdOrderByThumbupCountDesc(String journalId, Pageable pageable); + public Page findByJournalIdOrderByThumbupCountDescPublishTimeDesc(String journalId, Pageable pageable); public Page findAllByOrderByPublishTimeDesc(Pageable pageable); diff --git a/luoo_comment/src/main/java/com/luoo/comment/service/CommentService.java b/luoo_comment/src/main/java/com/luoo/comment/service/CommentService.java index e1cde52..71cfee0 100644 --- a/luoo_comment/src/main/java/com/luoo/comment/service/CommentService.java +++ b/luoo_comment/src/main/java/com/luoo/comment/service/CommentService.java @@ -72,8 +72,16 @@ public class CommentService { comment.setRootJournalId(comment.getJournalId()); if(StringUtils.isNotEmpty(comment.getJournalId())){ JournalRespDTO journalRespDTO = journalClient.findById(comment.getJournalId()).getData(); - comment.setRootJournalName(journalRespDTO.getTitle()); - comment.setJournalImage(journalRespDTO.getImage()); + /** + * 处理文章评论NPE,找不到图片 + */ + comment.setRootJournalName(""); + comment.setJournalImage(""); + if (journalRespDTO !=null){ + comment.setRootJournalName(journalRespDTO.getTitle()); + comment.setJournalImage(journalRespDTO.getImage()); + } + } @@ -204,7 +212,8 @@ public class CommentService { public Page findHotByJournalId(String journalId, int page, int size) { Pageable pageable = PageRequest.of(page-1,size); - return commentDao.findByJournalIdOrderByThumbupCountDesc(journalId,pageable); +// return commentDao.findByJournalIdOrderByThumbupCountDesc(journalId,pageable); + return commentDao.findByJournalIdOrderByThumbupCountDescPublishTimeDesc(journalId,pageable); } public Page findNewByJournalId(String journalId, int page, int size) { diff --git a/luoo_common/src/main/java/constants/Constants.java b/luoo_common/src/main/java/constants/Constants.java index 9d1a300..6e3ae95 100644 --- a/luoo_common/src/main/java/constants/Constants.java +++ b/luoo_common/src/main/java/constants/Constants.java @@ -22,6 +22,8 @@ public class Constants { public static final String J2CACHE_REGION_JOURNAL_COMMENT_PAGE_CHILDREN = "journal_comment_page_children"; public static final String J2CACHE_REGION_SEARCH_AUTO_COMPLETE = "search_auto_complete"; + public static final String J2CACHE_REGION_JOURNAL_HOT_COMMENTS = "journal_hot_comments"; + public static final String RABBIT_MESSAGE_CLEAN_JOURANL_QUERY_PAGE = "clean_journal_query_page"; public static final String TOKEN_PREFIX = "Bearer "; diff --git a/luoo_music/src/main/java/com/luoo/music/dto/mapper/CommentMapper.java b/luoo_music/src/main/java/com/luoo/music/dto/mapper/CommentMapper.java index a029272..d7dba14 100644 --- a/luoo_music/src/main/java/com/luoo/music/dto/mapper/CommentMapper.java +++ b/luoo_music/src/main/java/com/luoo/music/dto/mapper/CommentMapper.java @@ -6,7 +6,8 @@ import java.util.stream.Collectors; import com.luoo.music.client.UserClient; import com.luoo.music.dto.response.UserInfoDTO; -import com.luoo.music.util.Constants; +import constants.Constants; +import net.oschina.j2cache.CacheChannel; import org.apache.commons.lang.StringUtils; import org.apache.tomcat.util.bcel.classfile.Constant; import org.springframework.data.mongodb.core.MongoTemplate; @@ -26,14 +27,6 @@ public class CommentMapper { * 显示期刊点赞最多5条评论,该五条评论随机显示,每次进入刷新新,超过部分显示为“...”点击评论进入期刊详情页并自动定位到该评论,点击用户头像同上,若为空数据则显示“暂无评论,快去抢沙发吧! */ CommentDTO commentDTO=new CommentDTO(); -// List commentList = (List) 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 commentList =commentDao.findTop5ByJournalIdOrderByThumbupCountDesc(journalId); commentDTO.setCommentList(commentList); @@ -41,35 +34,8 @@ public class CommentMapper { * 获取期刊总评论数 一级评论数加上父评论的总回复数 */ -// 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 results = mongoTemplate.aggregate(agg, "comment", TotalCommentVo.class); -//// TotalCommentVo totalCommentVo = results.getUniqueMappedResult(); -//// commentDTO.setTotalCommentReply("0"); -//// List 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); @@ -95,67 +61,39 @@ public class CommentMapper { return commentDTO; } - public static CommentDTO getCommentDTO(String journalId, MongoTemplate mongoTemplate, CommentDao commentDao, RedisTemplate redisTemplate, UserClient userClient) { + public static List getList(String journalId,CommentDao commentDao,UserClient userClient){ + List commentList =commentDao.findTop5ByJournalIdOrderByThumbupCountDesc(journalId); + if(commentList.size()>0){ + commentList= commentList.stream().map(comment -> { + UserInfoDTO userInfoDTO =userClient.findByUserId(comment.getUserId()); + if(null != userInfoDTO){ + comment.setNickName(userInfoDTO.getNickName()); + comment.setAvatar(Constants.RESOURCE_PREFIX+userInfoDTO.getAvatar()); + } + + return comment; + }).collect(Collectors.toList()); + } + return commentList; + } + + public static CommentDTO getCommentDTO(String journalId, MongoTemplate mongoTemplate, CommentDao commentDao, RedisTemplate redisTemplate, UserClient userClient, CacheChannel cacheChannel) { /** * 显示期刊点赞最多5条评论,该五条评论随机显示,每次进入刷新新,超过部分显示为“...”点击评论进入期刊详情页并自动定位到该评论,点击用户头像同上,若为空数据则显示“暂无评论,快去抢沙发吧! */ CommentDTO commentDTO=new CommentDTO(); -// List commentList = (List) 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 commentList =commentDao.findTop5ByJournalIdOrderByThumbupCountDesc(journalId); - if(commentList.size()>0){ - commentList= commentList.stream().map(comment -> { - UserInfoDTO userInfoDTO =userClient.findByUserId(comment.getUserId()); - if(null != userInfoDTO){ - comment.setNickName(userInfoDTO.getNickName()); - comment.setAvatar(Constants.RESOURCE_PREFIX+userInfoDTO.getAvatar()); - } - - return comment; - }).collect(Collectors.toList()); - } - commentDTO.setCommentList(commentList); + + + + commentDTO.setCommentList((List) cacheChannel.get(Constants.J2CACHE_REGION_JOURNAL_HOT_COMMENTS,journalId, key-> CommentMapper.getList(journalId,commentDao,userClient),false)); /** * 获取期刊总评论数 一级评论数加上父评论的总回复数 */ -// 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 results = mongoTemplate.aggregate(agg, "comment", TotalCommentVo.class); -//// TotalCommentVo totalCommentVo = results.getUniqueMappedResult(); -//// commentDTO.setTotalCommentReply("0"); -//// List 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); 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 2bdae26..e698fad 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 @@ -14,6 +14,7 @@ import java.util.Set; import java.util.stream.Collectors; import com.luoo.music.client.UserClient; +import net.oschina.j2cache.CacheChannel; import org.springframework.core.io.ClassPathResource; import org.springframework.data.mongodb.core.MongoTemplate; import com.apifan.common.random.RandomSource; @@ -78,10 +79,10 @@ public class JournalMapper { return journalRespDTO; } public static JournalRespDTO getJournalRespDTO(Journal journal, - MongoTemplate mongoTemplate, RedisTemplate redisTemplate, CommentDao commentDao, UserClient userClient) { + MongoTemplate mongoTemplate, RedisTemplate redisTemplate, CommentDao commentDao, UserClient userClient, CacheChannel cacheChannel) { JournalRespDTO journalRespDTO = getJournalRespDTO(journal); CommentDTO commentDTO = CommentMapper.getCommentDTO(journalRespDTO.getId(), mongoTemplate, commentDao, - redisTemplate,userClient); + redisTemplate,userClient,cacheChannel); journalRespDTO.setCommentList(commentDTO.getCommentList()); journalRespDTO.setTotalCommentReply(commentDTO.getTotalCommentReply()); journalRespDTO.setTotalCommentReplyInt(commentDTO.getTotalCommentReplyInt()); diff --git a/luoo_music/src/main/java/com/luoo/music/service/JournalService.java b/luoo_music/src/main/java/com/luoo/music/service/JournalService.java index 7884b48..e1b4a9c 100644 --- a/luoo_music/src/main/java/com/luoo/music/service/JournalService.java +++ b/luoo_music/src/main/java/com/luoo/music/service/JournalService.java @@ -74,7 +74,7 @@ public class JournalService { @Autowired private UserClient userClient; - + /** * 查询全部列表 * @return @@ -377,7 +377,7 @@ public class JournalService { private List getJournalRespDTOWithComment(JournalQueryReq queryReq) { List pageList = queryPage(queryReq); - return pageList.stream().map(journal -> JournalMapper.getJournalRespDTO(journal,mongoTemplate,redisTemplate,commentDao,userClient)).collect(Collectors.toList()); + return pageList.stream().map(journal -> JournalMapper.getJournalRespDTO(journal,mongoTemplate,redisTemplate,commentDao,userClient,cacheChannel)).collect(Collectors.toList()); }