fix commentList sort && optimize journal hotCommentList

main
wangqing 8 months ago
parent d965c63b0f
commit 783fc2ad16

@ -393,6 +393,7 @@ public class CommentController extends BaseController {
Comment comment = commentService.findById(commentId); Comment comment = commentService.findById(commentId);
cacheChannel.clear(Constants.J2CACHE_REGION_JOURNAL_COMMENT_PAGE_HOT+"_"+comment.getRootJournalId()); cacheChannel.clear(Constants.J2CACHE_REGION_JOURNAL_COMMENT_PAGE_HOT+"_"+comment.getRootJournalId());
cacheChannel.clear(Constants.J2CACHE_REGION_JOURNAL_COMMENT_PAGE_NEW+"_"+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())) { if(StringUtils.isNotEmpty(comment.getParentId())) {
cacheChannel.clear(Constants.J2CACHE_REGION_JOURNAL_COMMENT_PAGE_CHILDREN+"_"+comment.getParentId()); cacheChannel.clear(Constants.J2CACHE_REGION_JOURNAL_COMMENT_PAGE_CHILDREN+"_"+comment.getParentId());

@ -14,6 +14,7 @@ public interface CommentDao extends MongoRepository<Comment,String> {
public Page<Comment> findByJournalId(String journalId, Pageable pageable); public Page<Comment> findByJournalId(String journalId, Pageable pageable);
public Page<Comment> findByJournalIdOrderByPublishTimeDesc(String journalId, Pageable pageable); public Page<Comment> findByJournalIdOrderByPublishTimeDesc(String journalId, Pageable pageable);
public Page<Comment> findByJournalIdOrderByThumbupCountDesc(String journalId, Pageable pageable); public Page<Comment> findByJournalIdOrderByThumbupCountDesc(String journalId, Pageable pageable);
public Page<Comment> findByJournalIdOrderByThumbupCountDescPublishTimeDesc(String journalId, Pageable pageable);
public Page<Comment> findAllByOrderByPublishTimeDesc(Pageable pageable); public Page<Comment> findAllByOrderByPublishTimeDesc(Pageable pageable);

@ -72,8 +72,16 @@ public class CommentService {
comment.setRootJournalId(comment.getJournalId()); comment.setRootJournalId(comment.getJournalId());
if(StringUtils.isNotEmpty(comment.getJournalId())){ if(StringUtils.isNotEmpty(comment.getJournalId())){
JournalRespDTO journalRespDTO = journalClient.findById(comment.getJournalId()).getData(); 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<Comment> findHotByJournalId(String journalId, int page, int size) { public Page<Comment> findHotByJournalId(String journalId, int page, int size) {
Pageable pageable = PageRequest.of(page-1,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<Comment> findNewByJournalId(String journalId, int page, int size) { public Page<Comment> findNewByJournalId(String journalId, int page, int size) {

@ -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_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_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 RABBIT_MESSAGE_CLEAN_JOURANL_QUERY_PAGE = "clean_journal_query_page";
public static final String TOKEN_PREFIX = "Bearer "; public static final String TOKEN_PREFIX = "Bearer ";

@ -6,7 +6,8 @@ import java.util.stream.Collectors;
import com.luoo.music.client.UserClient; import com.luoo.music.client.UserClient;
import com.luoo.music.dto.response.UserInfoDTO; 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.commons.lang.StringUtils;
import org.apache.tomcat.util.bcel.classfile.Constant; import org.apache.tomcat.util.bcel.classfile.Constant;
import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.MongoTemplate;
@ -26,14 +27,6 @@ public class CommentMapper {
* 5... * 5...
*/ */
CommentDTO commentDTO=new CommentDTO(); 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 =commentDao.findTop5ByJournalIdOrderByThumbupCountDesc(journalId); List<Comment> commentList =commentDao.findTop5ByJournalIdOrderByThumbupCountDesc(journalId);
commentDTO.setCommentList(commentList); commentDTO.setCommentList(commentList);
@ -41,35 +34,8 @@ public class CommentMapper {
* *
*/ */
// String totalString = (String) redisTemplate.opsForValue().get("JOURNAL_TATAL_COMMENT_COUNT__"+journalId);
String totalString = "0"; 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"; totalString = "0";
Criteria criteria = Criteria.where("journalId").is(journalId); Criteria criteria = Criteria.where("journalId").is(journalId);
@ -95,67 +61,39 @@ public class CommentMapper {
return commentDTO; return commentDTO;
} }
public static CommentDTO getCommentDTO(String journalId, MongoTemplate mongoTemplate, CommentDao commentDao, RedisTemplate redisTemplate, UserClient userClient) { public static List<Comment> getList(String journalId,CommentDao commentDao,UserClient userClient){
List<Comment> 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... * 5...
*/ */
CommentDTO commentDTO=new CommentDTO(); 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 =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<Comment>) 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"; 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"; totalString = "0";
Criteria criteria = Criteria.where("journalId").is(journalId); Criteria criteria = Criteria.where("journalId").is(journalId);

@ -14,6 +14,7 @@ import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import com.luoo.music.client.UserClient; import com.luoo.music.client.UserClient;
import net.oschina.j2cache.CacheChannel;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.MongoTemplate;
import com.apifan.common.random.RandomSource; import com.apifan.common.random.RandomSource;
@ -78,10 +79,10 @@ public class JournalMapper {
return journalRespDTO; return journalRespDTO;
} }
public static JournalRespDTO getJournalRespDTO(Journal journal, 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); JournalRespDTO journalRespDTO = getJournalRespDTO(journal);
CommentDTO commentDTO = CommentMapper.getCommentDTO(journalRespDTO.getId(), mongoTemplate, commentDao, CommentDTO commentDTO = CommentMapper.getCommentDTO(journalRespDTO.getId(), mongoTemplate, commentDao,
redisTemplate,userClient); redisTemplate,userClient,cacheChannel);
journalRespDTO.setCommentList(commentDTO.getCommentList()); journalRespDTO.setCommentList(commentDTO.getCommentList());
journalRespDTO.setTotalCommentReply(commentDTO.getTotalCommentReply()); journalRespDTO.setTotalCommentReply(commentDTO.getTotalCommentReply());
journalRespDTO.setTotalCommentReplyInt(commentDTO.getTotalCommentReplyInt()); journalRespDTO.setTotalCommentReplyInt(commentDTO.getTotalCommentReplyInt());

@ -74,7 +74,7 @@ public class JournalService {
@Autowired @Autowired
private UserClient userClient; private UserClient userClient;
/** /**
* *
* @return * @return
@ -377,7 +377,7 @@ public class JournalService {
private List<JournalRespDTO> getJournalRespDTOWithComment(JournalQueryReq queryReq) { private List<JournalRespDTO> getJournalRespDTOWithComment(JournalQueryReq queryReq) {
List<Journal> pageList = queryPage(queryReq); List<Journal> 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());
} }

Loading…
Cancel
Save