optimize journal list performance

main
wangqing 10 months ago
parent 9e73d18cd0
commit c150bb5b6a

@ -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;
@ -62,6 +63,9 @@ public class JournalController {
@Autowired
private CommentDao commentDao;
@Autowired
private RedisTemplate redisTemplate;
private static final int JOURNAL_FILTER_NUMBER_RANGE=100;
@ApiOperation(value = "1.查询期刊信息", notes = "若authorization为空或authorization校验失败默认返回最新的10期期刊筛选条件对游客不可用")
@ -80,7 +84,7 @@ public class JournalController {
List<String> ids=pageList.stream().map(Journal::getId).collect(Collectors.toList());
Set<String> journalCollectSet = null == user ? Collections.emptySet()
: userCollectInfoService.getCollectSet(user.getUserId(),ids, CollectTypeEnum.JOURNAL);
List<JournalRespDTO> list = pageList.stream().map(a -> JournalMapper.getJournalRespDTO(a, journalCollectSet,mongoTemplate,commentDao))
List<JournalRespDTO> list = pageList.stream().map(a -> JournalMapper.getJournalRespDTO(a, journalCollectSet,mongoTemplate,redisTemplate,commentDao))
.collect(Collectors.toList());
return Result.success(new PageResult<JournalRespDTO>(Long.valueOf(list.size()), list));
}
@ -103,7 +107,7 @@ public class JournalController {
}
List<Journal> pageList = journalService.orderByField(objectIds);
Set<String> journalCollectSet = new HashSet<>(objectIds);
List<JournalRespDTO> list = pageList.stream().map(a -> JournalMapper.getJournalRespDTO(a, journalCollectSet,mongoTemplate,commentDao))
List<JournalRespDTO> list = pageList.stream().map(a -> JournalMapper.getJournalRespDTO(a, journalCollectSet,mongoTemplate,redisTemplate,commentDao))
.collect(Collectors.toList());
return Result.success(new PageResult<JournalRespDTO>(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);

@ -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<String> DEFAULT_TAGS=Arrays.asList("暂无标签");
private static final String JOURNAL_TAG_FILE_PATH = "journalTags.txt";
private static final Map<String, List<String>> journalTagMap = new HashMap<>();
private static final String[] EDITOR = new String[] { "左岸以西", "落在低处" };
@ -78,13 +85,58 @@ public class JournalMapper {
}
return journalRespDTO;
}
public static JournalRespDTO getJournalRespDTO(Journal journal, Set<String> journalCollectSet,
MongoTemplate mongoTemplate, CommentDao commentDao) {
MongoTemplate mongoTemplate, RedisTemplate redisTemplate,CommentDao commentDao) {
JournalRespDTO journalRespDTO = getJournalRespDTO(journal,journalCollectSet);
CommentDTO commentDTO = CommentMapper.getCommentDTO(journalRespDTO.getId(), mongoTemplate, commentDao);
journalRespDTO.setCommentList(commentDTO.getCommentList());
journalRespDTO.setTotalCommentReply(commentDTO.getTotalCommentReply());
/**
* 5...
*/
List<Comment> commentList = (List<Comment>) 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<Comment> 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<TotalCommentVo> results = mongoTemplate.aggregate(agg, "comment", TotalCommentVo.class);
TotalCommentVo totalCommentVo = results.getUniqueMappedResult();
journalRespDTO.setTotalCommentReply("0");
List<Comment> 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<String> getTags(String journalNo) {
return journalTagMap.getOrDefault(journalNo, DEFAULT_TAGS);
return journalTagMap.getOrDefault(journalNo, Collections.emptyList());
}
private static String getEditDate(Journal journal) {

Loading…
Cancel
Save