1.fix empty list in cache

main
Gary 7 months ago
parent fb7ea2ae13
commit 427fc9e4d1

@ -1,6 +1,7 @@
package com.luoo.music.service;
import java.util.*;
import java.util.function.Function;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import javax.persistence.criteria.CriteriaBuilder;
@ -14,6 +15,8 @@ import constants.Constants;
import enums.JournalPublishEnum;
import enums.JournalStatusEnum;
import net.oschina.j2cache.CacheChannel;
import net.oschina.j2cache.CacheObject;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.factory.annotation.Autowired;
@ -354,18 +357,33 @@ public class JournalService {
return journalSongDao.orderByJournalIdField(jouranlIds);
}
@SuppressWarnings("unchecked")
public List<JournalRespDTO> queryJournalByPage(JournalQueryReq queryReq) {
if(StringTools.isEmpty(queryReq.getCategoryId())&&StringTools.isEmpty(queryReq.getJournalNoRange())) {
return (List<JournalRespDTO>) this.cacheChannel.get(Constants.J2CACHE_REGION_JOURNAL_QUERY_PAGE, queryReq.toString(), key -> getJournalRespDTOWithComment(queryReq) , false).getValue() ;
return queryJournalByPage(queryReq, key -> getJournalRespDTOWithComment(queryReq));
}
List<JournalRespDTO> list= (List<JournalRespDTO>) this.cacheChannel.get(Constants.J2CACHE_REGION_JOURNAL_QUERY_PAGE, queryReq.toString(), key -> getJournalRespDTO(queryReq) , false).getValue() ;
List<JournalRespDTO> list= queryJournalByPage(queryReq,key -> getJournalRespDTO(queryReq)) ;
list.parallelStream().forEach(j->{
updateComment(j);
});
return list;
}
@SuppressWarnings("unchecked")
private List<JournalRespDTO> queryJournalByPage(JournalQueryReq queryReq,
Function<JournalQueryReq, List<JournalRespDTO>> function) {
CacheObject cacheObject=this.cacheChannel.get(Constants.J2CACHE_REGION_JOURNAL_QUERY_PAGE, queryReq.toString());
if(null==cacheObject||null==cacheObject.getValue()) {
List<JournalRespDTO> result = function.apply(queryReq);
if(CollectionUtils.isEmpty(result)) {
return Collections.emptyList();
}
this.cacheChannel.set(Constants.J2CACHE_REGION_JOURNAL_QUERY_PAGE, queryReq.toString(), result);
return result;
}
return (List<JournalRespDTO>) cacheObject.getValue();
}
private void updateComment(JournalRespDTO journalRespDTO) {
CommentDTO commentDTO = CommentMapper.getCommentDTO(journalRespDTO.getId(), mongoTemplate, commentDao,
redisTemplate,userClient,cacheChannel);

Loading…
Cancel
Save