Merge remote-tracking branch 'origin/main'

main
wangqing 11 months ago
commit abc55394b6

@ -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();

@ -84,6 +84,26 @@
<artifactId>lucene-analyzers-common</artifactId>
<version>4.10.3</version>
</dependency>
<dependency>
<groupId>net.oschina.j2cache</groupId>
<artifactId>j2cache-spring-boot2-starter</artifactId>
<version>2.8.0-release</version>
</dependency>
<dependency>
<groupId>net.oschina.j2cache</groupId>
<artifactId>j2cache-core</artifactId>
<version>2.8.5-release</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<finalName>app</finalName>

@ -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<JournalRespDTO> pageList=journalService.queryJournalByPage(queryReq);
Set<String> journalCollectSet = getJournalCollectSet(user);
List<JournalRespDTO> list = pageList.stream().map(j->updateCollectStatus(j,journalCollectSet)).collect(Collectors.toList());
return Result.success(new PageResult<JournalRespDTO>(Long.valueOf(list.size()), list));
}
private Set<String> 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<String> journalCollectSet = null == user ? Collections.emptySet():getJournalCollectSet(user.getUserId());
if(StringTools.isEmpty(queryReq.getCategoryId())&&StringTools.isEmpty(queryReq.getJournalNoRange())) {
List<JournalRespDTO> cacheList = getPagableJournalRespDTO(queryReq,journalCollectSet);
List<JournalRespDTO> list = cacheList.stream().map(j->updateCollectAndTag(j,journalCollectSet)).collect(Collectors.toList());
return Result.success(new PageResult<JournalRespDTO>(Long.valueOf(list.size()), list));
}
List<Journal> pageList = journalService.queryPage(queryReq);
List<JournalRespDTO> list = pageList.stream().map(a -> JournalMapper.getJournalRespDTO(a, journalCollectSet)).collect(Collectors.toList());
return Result.success(new PageResult<JournalRespDTO>(Long.valueOf(list.size()), list));
}
private JournalRespDTO updateCollectAndTag(JournalRespDTO journalRespDTO,Set<String> journalCollectSet) {
private JournalRespDTO updateCollectStatus(JournalRespDTO journalRespDTO,Set<String> journalCollectSet) {
journalRespDTO.setHaveCollect(journalCollectSet.contains(journalRespDTO.getId()));
return journalRespDTO;
}
private List<JournalRespDTO> getPagableJournalRespDTO(JournalQueryReq queryReq,Set<String> journalCollectSet) {
String redisPageJournalResponseDtoKey=Constants.REDIS_KEY_PAGE_JOURNAL_RESPONSE_DTO+queryReq.getPageNum()+"_"+queryReq.getPageSize();
List<JournalRespDTO> journalRespDTOs = (List<JournalRespDTO>)redisTemplate.opsForValue().get(redisPageJournalResponseDtoKey);
if(null==journalRespDTOs) {
List<Journal> 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<String> getJournalCollectSet(String userId) {
Set<String> set=(Set<String>) redisTemplate.opsForValue().get(Constants.REDIS_KEY_USER_COLLECT_JOURNAL+userId);
if(null==set) {
@ -188,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);

@ -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());

@ -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,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<Integer,List<String>> 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<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);
}
/**
@ -349,4 +357,37 @@ public class JournalService {
public List<String> orderByJournalIdField(List<String> jouranlIds) {
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) , true).getValue() ;
}
return (List<JournalRespDTO>) this.cacheChannel.get(Constants.J2CACHE_REGION_JOURNAL_QUERY_PAGE, queryReq.toString(), key -> getJournalRespDTO(queryReq) , true).getValue() ;
}
private List<JournalRespDTO> getJournalRespDTO(JournalQueryReq queryReq) {
List<Journal> pageList = queryPage(queryReq);
return pageList.stream().map(a ->
JournalMapper.getJournalRespDTO(a)).collect(Collectors.toList());
}
private List<JournalRespDTO> getJournalRespDTOWithComment(JournalQueryReq queryReq) {
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);
}
}

@ -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<V> {
@Autowired
@Qualifier("stringRedisTemplate")
private RedisTemplate<String, V> redisTemplate;
private static final Logger logger = LoggerFactory.getLogger(RedisUtils.class);

@ -9,4 +9,34 @@ spring:
servlet:
multipart:
max-file-size: 20MB
max-request-size: 200MB
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

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