diff --git a/luoo_common/src/main/java/constants/Constants.java b/luoo_common/src/main/java/constants/Constants.java
index ebc5253..2436751 100644
--- a/luoo_common/src/main/java/constants/Constants.java
+++ b/luoo_common/src/main/java/constants/Constants.java
@@ -9,6 +9,10 @@ public class Constants {
public static final String REDIS_KEY_USER_COLLECT_JOURNAL = "redis_key_user_collect_journal_";
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();
diff --git a/luoo_music/pom.xml b/luoo_music/pom.xml
index 127a0e4..2869c2f 100644
--- a/luoo_music/pom.xml
+++ b/luoo_music/pom.xml
@@ -84,6 +84,26 @@
lucene-analyzers-common
4.10.3
+
+ net.oschina.j2cache
+ j2cache-spring-boot2-starter
+ 2.8.0-release
+
+
+ net.oschina.j2cache
+ j2cache-core
+ 2.8.5-release
+
+
+ org.slf4j
+ slf4j-simple
+
+
+ org.slf4j
+ slf4j-api
+
+
+
app
diff --git a/luoo_music/src/main/java/com/luoo/music/controller/JournalController.java b/luoo_music/src/main/java/com/luoo/music/controller/JournalController.java
index f7cca33..3262489 100644
--- a/luoo_music/src/main/java/com/luoo/music/controller/JournalController.java
+++ b/luoo_music/src/main/java/com/luoo/music/controller/JournalController.java
@@ -88,39 +88,28 @@ public class JournalController {
@RequestHeader(value = "Authorization", required = false) String authorization,
@VerifyParam JournalQueryReq queryReq) {
UserLoginDto user = jwtUtil.getUserLoginDto(authorization);
+ updateQuery(user,queryReq);
+ List pageList=journalService.queryJournalByPage(queryReq);
+ Set journalCollectSet = getJournalCollectSet(user);
+ List list = pageList.stream().map(j->updateCollectStatus(j,journalCollectSet)).collect(Collectors.toList());
+ return Result.success(new PageResult(Long.valueOf(list.size()), list));
+ }
+
+ private Set getJournalCollectSet(UserLoginDto user) {
+ return null == user ? Collections.emptySet():getJournalCollectSet(user.getUserId());
+ }
+ private void updateQuery(UserLoginDto user, JournalQueryReq queryReq) {
if (null == user) {
queryReq.setCategoryId(null);
queryReq.setPageNum(1);
queryReq.setPageSize(10);
}
- Set journalCollectSet = null == user ? Collections.emptySet():getJournalCollectSet(user.getUserId());
-
- if(StringTools.isEmpty(queryReq.getCategoryId())&&StringTools.isEmpty(queryReq.getJournalNoRange())) {
- List cacheList = getPagableJournalRespDTO(queryReq,journalCollectSet);
- List list = cacheList.stream().map(j->updateCollectAndTag(j,journalCollectSet)).collect(Collectors.toList());
- return Result.success(new PageResult(Long.valueOf(list.size()), list));
- }
-
- List pageList = journalService.queryPage(queryReq);
- List list = pageList.stream().map(a -> JournalMapper.getJournalRespDTO(a, journalCollectSet)).collect(Collectors.toList());
-
- return Result.success(new PageResult(Long.valueOf(list.size()), list));
}
- private JournalRespDTO updateCollectAndTag(JournalRespDTO journalRespDTO,Set journalCollectSet) {
+ private JournalRespDTO updateCollectStatus(JournalRespDTO journalRespDTO,Set journalCollectSet) {
journalRespDTO.setHaveCollect(journalCollectSet.contains(journalRespDTO.getId()));
return journalRespDTO;
}
- private List getPagableJournalRespDTO(JournalQueryReq queryReq,Set journalCollectSet) {
- String redisPageJournalResponseDtoKey=Constants.REDIS_KEY_PAGE_JOURNAL_RESPONSE_DTO+queryReq.getPageNum()+"_"+queryReq.getPageSize();
-
- List journalRespDTOs = (List)redisTemplate.opsForValue().get(redisPageJournalResponseDtoKey);
- if(null==journalRespDTOs) {
- List pageList = journalService.queryPage(queryReq);
- journalRespDTOs = pageList.stream().map(journal -> JournalMapper.getJournalRespDTO(journal, journalCollectSet,mongoTemplate,redisTemplate,commentDao)).collect(Collectors.toList());
- redisTemplate.opsForValue().set(redisPageJournalResponseDtoKey,journalRespDTOs,6, TimeUnit.HOURS);
- }
- return journalRespDTOs;
- }
+
private Set getJournalCollectSet(String userId) {
Set set=(Set) redisTemplate.opsForValue().get(Constants.REDIS_KEY_USER_COLLECT_JOURNAL+userId);
if(null==set) {
@@ -188,13 +177,14 @@ public class JournalController {
public Result 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);
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 49b6b51..621a886 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
@@ -65,6 +65,18 @@ public class JournalMapper {
return journalRespDTO;
}
+ public static JournalRespDTO getJournalRespDTO(Journal journal,
+ MongoTemplate mongoTemplate, RedisTemplate redisTemplate, CommentDao commentDao) {
+ JournalRespDTO journalRespDTO = getJournalRespDTO(journal);
+ CommentDTO commentDTO = CommentMapper.getCommentDTO(journalRespDTO.getId(), mongoTemplate, commentDao,
+ redisTemplate);
+ journalRespDTO.setCommentList(commentDTO.getCommentList());
+ journalRespDTO.setTotalCommentReply(commentDTO.getTotalCommentReply());
+ journalRespDTO.setTotalCommentReplyInt(commentDTO.getTotalCommentReplyInt());
+
+ return journalRespDTO;
+ }
+
private static String getPublishDate(Journal journal) {
LocalDateTime date = null == journal.getPubTime() ? journal.getCreateTime() : journal.getPubTime();
return DateUtil.format(date, DateTimePatternEnum.YYYY_DOT_MM_DOT_DD.getPattern());
diff --git a/luoo_music/src/main/java/com/luoo/music/service/CMSJournalService.java b/luoo_music/src/main/java/com/luoo/music/service/CMSJournalService.java
index c5681ef..b5d06a5 100644
--- a/luoo_music/src/main/java/com/luoo/music/service/CMSJournalService.java
+++ b/luoo_music/src/main/java/com/luoo/music/service/CMSJournalService.java
@@ -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);
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 d4189fb..0b46d56 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
@@ -2,7 +2,9 @@ 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;
import javax.persistence.criteria.CriteriaBuilder;
@@ -11,25 +13,35 @@ import javax.persistence.criteria.Predicate;
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;
+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;
+import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
+import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import util.IdWorker;
+import util.StringTools;
+import com.luoo.music.dao.CommentDao;
import com.luoo.music.dao.JournalDao;
import com.luoo.music.dao.JournalSongDao;
import com.luoo.music.dao.JournalTagDao;
import com.luoo.music.dao.TagDao;
+import com.luoo.music.dto.mapper.JournalMapper;
import com.luoo.music.dto.request.JournalQueryReq;
+import com.luoo.music.dto.response.JournalRespDTO;
import com.luoo.music.pojo.Journal;
/**
@@ -47,13 +59,20 @@ public class JournalService {
@Autowired
private JournalSongDao journalSongDao;
- @Autowired
- private IdWorker idWorker;
@Autowired
private JournalTagDao journalTagDao;
@Autowired
private RedisTemplate redisTemplate;
+
+ @Autowired
+ private CacheChannel cacheChannel;
+
+ @Autowired
+ private MongoTemplate mongoTemplate;
+ @Autowired
+ private CommentDao commentDao;
+
private Map> JOURNAL_RANGE_MAP=new HashMap<>();
/*
* public void updateState(String id) { journalDao.updateState(id); }
@@ -100,18 +119,7 @@ public class JournalService {
* @return
*/
public Journal findById(String id) {
-
- Journal journal = (Journal) redisTemplate.opsForValue().get("journal_"+id);
-
- if (journal==null) {
- Optional optional=journalDao.findById(id);
- if(optional.isPresent()) {
- journal =optional.get();
- redisTemplate.opsForValue().set("journal_"+id,journal);
- }
- }
-
- return journal;
+ return journalDao.findById(id).orElse(null);
}
/**
@@ -349,4 +357,37 @@ public class JournalService {
public List orderByJournalIdField(List jouranlIds) {
return journalSongDao.orderByJournalIdField(jouranlIds);
}
+
+ @SuppressWarnings("unchecked")
+ public List queryJournalByPage(JournalQueryReq queryReq) {
+ if(StringTools.isEmpty(queryReq.getCategoryId())&&StringTools.isEmpty(queryReq.getJournalNoRange())) {
+ return (List) this.cacheChannel.get(Constants.J2CACHE_REGION_JOURNAL_QUERY_PAGE, queryReq.toString(), key -> getJournalRespDTOWithComment(queryReq) , true).getValue() ;
+ }
+ return (List) this.cacheChannel.get(Constants.J2CACHE_REGION_JOURNAL_QUERY_PAGE, queryReq.toString(), key -> getJournalRespDTO(queryReq) , true).getValue() ;
+ }
+
+ private List getJournalRespDTO(JournalQueryReq queryReq) {
+ List pageList = queryPage(queryReq);
+ return pageList.stream().map(a ->
+ JournalMapper.getJournalRespDTO(a)).collect(Collectors.toList());
+ }
+
+ private List getJournalRespDTOWithComment(JournalQueryReq queryReq) {
+ List 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);
+ }
}
diff --git a/luoo_music/src/main/java/com/luoo/music/util/RedisUtils.java b/luoo_music/src/main/java/com/luoo/music/util/RedisUtils.java
index de293cd..28d8600 100644
--- a/luoo_music/src/main/java/com/luoo/music/util/RedisUtils.java
+++ b/luoo_music/src/main/java/com/luoo/music/util/RedisUtils.java
@@ -3,6 +3,7 @@ package com.luoo.music.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
@@ -14,6 +15,7 @@ import java.util.concurrent.TimeUnit;
public class RedisUtils {
@Autowired
+ @Qualifier("stringRedisTemplate")
private RedisTemplate redisTemplate;
private static final Logger logger = LoggerFactory.getLogger(RedisUtils.class);
diff --git a/luoo_music/src/main/resources/bootstrap.yml b/luoo_music/src/main/resources/bootstrap.yml
index 57f74fb..099afc9 100644
--- a/luoo_music/src/main/resources/bootstrap.yml
+++ b/luoo_music/src/main/resources/bootstrap.yml
@@ -9,4 +9,34 @@ spring:
servlet:
multipart:
max-file-size: 20MB
- max-request-size: 200MB
\ No newline at end of file
+ max-request-size: 200MB
+redis:
+ # 地址, 多个地址使用‘,’逗号分割
+ hosts: ${spring.redis.host}:${spring.redis.port}
+
+j2cache:
+ openSpringCache: true
+ # 缓存中不存在时,运行缓存空对象
+ allowNullValues: true
+ redisClient: lettuce
+ l2CacheOpen: true
+ serialization: json
+ sync_ttl_to_redis: true
+ # 一级缓存使用caffeine
+ L1:
+ provider_class: caffeine
+ L2:
+ #使用springRedis替换二级缓存
+ provider_class: net.oschina.j2cache.cache.support.redis.SpringRedisProvider
+ config_section: redis
+ #使用springRedis进行广播通知缓失效
+ broadcast: net.oschina.j2cache.cache.support.redis.SpringRedisPubSubPolicy
+
+# 上面配置的一级缓存为caffeine, 那么这里对一级缓存的配置就必须以这个caffeine开头
+caffeine:
+ properties: caffeine.properties
+---
+spring:
+ cache:
+ # 一级缓存使用caffeine
+ type: caffeine
\ No newline at end of file
diff --git a/luoo_music/src/main/resources/caffeine.properties b/luoo_music/src/main/resources/caffeine.properties
new file mode 100644
index 0000000..71c0866
--- /dev/null
+++ b/luoo_music/src/main/resources/caffeine.properties
@@ -0,0 +1,3 @@
+default: 1000, 2h
+journal_query_page: 10000, 6h
+journal_id: 2000, 6h
\ No newline at end of file