1.enable j2cache on find journal by id

main
Gary 9 months ago
parent f163267ccb
commit cbdb9b9666

@ -11,6 +11,8 @@ public class Constants {
public static final String REDIS_KEY_PAGE_JOURNAL_RESPONSE_DTO = "redis_key_page_journal_response_dto___";
public static final String J2CACHE_REGION_JOURNAL_QUERY_PAGE = "journal_query_page";
public static final String J2CACHE_REGION_JOURNAL_ID = "journal_id";
public static final String TOKEN_PREFIX = "Bearer ";
public static final int TOKEN_PREFIX_LENGTH = TOKEN_PREFIX.length();

@ -177,13 +177,14 @@ public class JournalController {
public Result<JournalRespDTO> findById(
@RequestHeader(value = "Authorization", required = false) String authorization,
@PathVariable @VerifyParam(required = true) String id) {
Journal journal = journalService.findById(id);
if(null==journal) {
//Journal journal = journalService.findById(id);
JournalRespDTO journalRespDTO=journalService.queryJournalById(id);
if(null==journalRespDTO) {
return Result.failed(StatusCode.MUSIC_COMMON_FAILED,"根据id无法找到期刊, id: "+id);
}
UserLoginDto user = jwtUtil.getUserLoginDto(authorization);
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);

@ -2,6 +2,8 @@ package com.luoo.music.service;
import api.PageResult;
import api.Result;
import net.oschina.j2cache.CacheChannel;
import com.luoo.music.client.UserClient;
import com.luoo.music.dao.*;
import com.luoo.music.pojo.*;
@ -53,7 +55,7 @@ public class CMSJournalService {
@Autowired
private UserClient userClient;
@Autowired
private RedisTemplate redisTemplate;
private CacheChannel cacheChannel;
/**
* +
@ -419,7 +421,7 @@ public class CMSJournalService {
newJournal.setCreateTime(oldJournal.getCreateTime());
newJournal.setPubTime(oldJournal.getPubTime());
Journal journal = journalDao.save(newJournal);
redisTemplate.opsForValue().set("journal_" + id, journal);
this.cacheChannel.evict(constants.Constants.J2CACHE_REGION_JOURNAL_ID, id);
// 删除旧的关联信息,更新的关联信息
batchDeleteJournalRelatesInfo(id, oldJournal.getJournalNo());
batchSaveJournalRelatesInfo(id, param);

@ -2,6 +2,7 @@ package com.luoo.music.service;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
@ -13,6 +14,7 @@ import javax.persistence.criteria.Root;
import api.Result;
import constants.Constants;
import enums.CollectTypeEnum;
import enums.JournalPublishEnum;
import enums.JournalStatusEnum;
import net.oschina.j2cache.CacheChannel;
@ -57,8 +59,6 @@ public class JournalService {
@Autowired
private JournalSongDao journalSongDao;
@Autowired
private IdWorker idWorker;
@Autowired
private JournalTagDao journalTagDao;
@Autowired
@ -72,7 +72,7 @@ public class JournalService {
@Autowired
private CommentDao commentDao;
private Map<Integer,List<String>> JOURNAL_RANGE_MAP=new HashMap<>();
/*
* public void updateState(String id) { journalDao.updateState(id); }
@ -119,18 +119,7 @@ public class JournalService {
* @return
*/
public Journal findById(String id) {
Journal journal = (Journal) redisTemplate.opsForValue().get("journal_"+id);
if (journal==null) {
Optional<Journal> optional=journalDao.findById(id);
if(optional.isPresent()) {
journal =optional.get();
redisTemplate.opsForValue().set("journal_"+id,journal);
}
}
return journal;
return journalDao.findById(id).orElse(null);
}
/**
@ -387,4 +376,18 @@ public class JournalService {
List<Journal> pageList = queryPage(queryReq);
return pageList.stream().map(journal -> JournalMapper.getJournalRespDTO(journal,mongoTemplate,redisTemplate,commentDao)).collect(Collectors.toList());
}
public JournalRespDTO queryJournalById(String id) {
return (JournalRespDTO) this.cacheChannel.get(Constants.J2CACHE_REGION_JOURNAL_ID, id, key -> getJournalRespDTOById(id), false).getValue();
}
private JournalRespDTO getJournalRespDTOById(String id) {
Journal journal=this.findById(id);
if(null==journal) {
return null;
}
return JournalMapper.getJournalRespDTO(journal,mongoTemplate,redisTemplate,commentDao);
}
}

@ -1,2 +1,3 @@
default: 1000, 2h
journal_query_page: 10000, 6h
journal_query_page: 10000, 6h
journal_id: 2000, 6h
Loading…
Cancel
Save