From 955be11b23fb77622f19112f8c92332319907b22 Mon Sep 17 00:00:00 2001 From: Gary Date: Thu, 8 Feb 2024 15:14:36 +0800 Subject: [PATCH] 1.add redis cache --- .../src/main/java/constants/Constants.java | 4 ++ .../music/controller/JournalController.java | 50 +++++++++++++++++-- .../main/java/com/luoo/music/dao/TagDao.java | 3 ++ .../luoo/music/dto/mapper/JournalMapper.java | 4 +- .../music/dto/response/JournalRespDTO.java | 4 +- .../com/luoo/music/service/TagService.java | 3 ++ .../music/service/UserCollectInfoService.java | 8 +++ .../user/service/UserCollectInfoService.java | 16 +++++- 8 files changed, 84 insertions(+), 8 deletions(-) diff --git a/luoo_common/src/main/java/constants/Constants.java b/luoo_common/src/main/java/constants/Constants.java index a6d1bbb..8aca6da 100644 --- a/luoo_common/src/main/java/constants/Constants.java +++ b/luoo_common/src/main/java/constants/Constants.java @@ -5,6 +5,10 @@ public class Constants { public static final String REDIS_KEY_IMAGE_CHECK_CODE = "redis_key_image_check_code_"; public static final String REDIS_KEY_MOBILE_CHECK_CODE = "redis_key_mobile_check_code_"; + + 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 TOKEN_PREFIX = "Bearer "; public static final int TOKEN_PREFIX_LENGTH = TOKEN_PREFIX.length(); 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 3fb13c2..111bd83 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 @@ -2,6 +2,7 @@ package com.luoo.music.controller; import java.util.Arrays; import java.util.Collections; +import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; @@ -22,6 +23,7 @@ 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.util.StopWatch; import org.springframework.web.bind.annotation.*; import com.luoo.music.pojo.Journal; import com.luoo.music.pojo.Tag; @@ -32,9 +34,11 @@ import annotation.GlobalInterceptor; import annotation.VerifyParam; import api.PageResult; import api.Result; +import constants.Constants; import dto.UserLoginDto; import enums.CollectTypeEnum; import util.JwtUtil; +import util.StringTools; /** * 控制器层 @@ -69,7 +73,8 @@ public class JournalController { private RedisTemplate redisTemplate; private static final int JOURNAL_FILTER_NUMBER_RANGE=100; - + private static final Map journalRespDTOMap=new HashMap<>(); + @ApiOperation(value = "1.查询期刊信息", notes = "若authorization为空或authorization校验失败,默认返回最新的10期,期刊筛选条件对游客不可用") @GetMapping("/list") @GlobalInterceptor @@ -82,15 +87,50 @@ public class JournalController { 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 ids=cacheList.stream().map(JournalRespDTO::getId).collect(Collectors.toList()); + Map> tagMap=tagService.getTagMap(ids); + List list = cacheList.stream().map(j->updateCollectAndTag(j,journalCollectSet,tagMap)).collect(Collectors.toList()); + return Result.success(new PageResult(Long.valueOf(list.size()), list)); + } + List pageList = journalService.queryPage(queryReq); List ids=pageList.stream().map(Journal::getId).collect(Collectors.toList()); - Set journalCollectSet = null == user ? Collections.emptySet() - : userCollectInfoService.getCollectSet(user.getUserId(),ids, CollectTypeEnum.JOURNAL); Map> tagMap=tagService.getTagMap(ids); - List list = pageList.stream().map(a -> JournalMapper.getJournalRespDTO(a, journalCollectSet,mongoTemplate,redisTemplate,commentDao,tagMap)) - .collect(Collectors.toList()); + List list = pageList.stream().map(a -> JournalMapper.getJournalRespDTO(a, journalCollectSet,tagMap)).collect(Collectors.toList()); + return Result.success(new PageResult(Long.valueOf(list.size()), list)); } + private JournalRespDTO updateCollectAndTag(JournalRespDTO journalRespDTO,Set journalCollectSet, Map> tagMap) { + journalRespDTO.setTags(tagMap.getOrDefault(journalRespDTO.getId(), Collections.emptyList())); + 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); + List ids=pageList.stream().map(Journal::getId).collect(Collectors.toList()); + Map> tagMap=tagService.getTagMap(ids); + + journalRespDTOs = pageList.stream().map(journal -> JournalMapper.getJournalRespDTO(journal, journalCollectSet,mongoTemplate,redisTemplate,commentDao,tagMap)).collect(Collectors.toList()); + redisTemplate.opsForValue().set(redisPageJournalResponseDtoKey,journalRespDTOs); + } + return journalRespDTOs; + } + private Set getJournalCollectSet(String userId) { + Set set=(Set) redisTemplate.opsForValue().get(Constants.REDIS_KEY_USER_COLLECT_JOURNAL+userId); + if(null==set) { + set=userCollectInfoService.getCollectSet(userId, CollectTypeEnum.JOURNAL); + redisTemplate.opsForValue().set(Constants.REDIS_KEY_USER_COLLECT_JOURNAL+userId,set); + } + return set; + } @ApiOperation(value = "2.查询收藏期刊信息") @ApiImplicitParams({ @ApiImplicitParam(name = "userId", value = "用户id", required = true), diff --git a/luoo_music/src/main/java/com/luoo/music/dao/TagDao.java b/luoo_music/src/main/java/com/luoo/music/dao/TagDao.java index c5ff1ea..5ff9528 100644 --- a/luoo_music/src/main/java/com/luoo/music/dao/TagDao.java +++ b/luoo_music/src/main/java/com/luoo/music/dao/TagDao.java @@ -30,4 +30,7 @@ public interface TagDao extends JpaRepository, JpaSpecificationExec @Query(value = "select journal_id,name_ch from tb_journal_tag,tb_tag_info where journal_id in ?1 and tb_journal_tag.tag_id =tb_tag_info.id ;", nativeQuery = true) List getJounralId2TagPairs(List journalIds); + + @Query(value = "select name_ch from tb_journal_tag,tb_tag_info where journal_id = ?1 and tb_journal_tag.tag_id =tb_tag_info.id ;", nativeQuery = true) + List getTagNameChByJournalId(String journalId); } 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 19ab785..ff93b45 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 @@ -79,7 +79,9 @@ public class JournalMapper { } return journalRespDTO; } - + public static List getTags(String journalId,String journalNo,Map> tagMap){ + return tagMap.getOrDefault(journalId, getTags(journalNo)); + } public static JournalRespDTO getJournalRespDTO(Journal journal, Set journalCollectSet, MongoTemplate mongoTemplate, RedisTemplate redisTemplate, CommentDao commentDao, Map> tagMap) { diff --git a/luoo_music/src/main/java/com/luoo/music/dto/response/JournalRespDTO.java b/luoo_music/src/main/java/com/luoo/music/dto/response/JournalRespDTO.java index 24cd850..0366f90 100644 --- a/luoo_music/src/main/java/com/luoo/music/dto/response/JournalRespDTO.java +++ b/luoo_music/src/main/java/com/luoo/music/dto/response/JournalRespDTO.java @@ -1,5 +1,6 @@ package com.luoo.music.dto.response; +import java.io.Serializable; import java.util.List; import com.luoo.music.pojo.Comment; @@ -7,7 +8,8 @@ import io.swagger.annotations.ApiModelProperty; import lombok.Data; @Data -public class JournalRespDTO { +public class JournalRespDTO implements Serializable { + private static final long serialVersionUID = 1L; @ApiModelProperty(value = "ID") private String id; @ApiModelProperty(value = "剘刊号") diff --git a/luoo_music/src/main/java/com/luoo/music/service/TagService.java b/luoo_music/src/main/java/com/luoo/music/service/TagService.java index c047971..90b48d8 100644 --- a/luoo_music/src/main/java/com/luoo/music/service/TagService.java +++ b/luoo_music/src/main/java/com/luoo/music/service/TagService.java @@ -36,5 +36,8 @@ public class TagService { return pairs.stream().collect(Collectors.groupingBy(s -> s[0].toString(), Collectors.mapping(s -> s[1].toString(), Collectors.toList()))); } + public List getTagNameChByJournalId(String journalId){ + return tagDao.getTagNameChByJournalId(journalId); + } } diff --git a/luoo_music/src/main/java/com/luoo/music/service/UserCollectInfoService.java b/luoo_music/src/main/java/com/luoo/music/service/UserCollectInfoService.java index 3501544..d27eb91 100644 --- a/luoo_music/src/main/java/com/luoo/music/service/UserCollectInfoService.java +++ b/luoo_music/src/main/java/com/luoo/music/service/UserCollectInfoService.java @@ -38,6 +38,14 @@ public class UserCollectInfoService { return userCollectInfos.isEmpty() ? Collections.emptySet() : userCollectInfos.stream().map(UserCollectInfo::getObjectId).collect(Collectors.toSet()); } + public Set getCollectSet(String userId, CollectTypeEnum collectTypeEnum) { + Criteria criteria = Criteria.where("userId").is(userId).and("collectType") + .is(collectTypeEnum.getType()); + Query query = new Query(criteria); + List userCollectInfos = mongoTemplate.find(query, UserCollectInfo.class); + return userCollectInfos.isEmpty() ? Collections.emptySet() : userCollectInfos.stream().map(UserCollectInfo::getObjectId).collect(Collectors.toSet()); + } + public boolean isCollect(String userId, String objectId, CollectTypeEnum collectTypeEnum) { Criteria criteria = Criteria.where("userId").is(userId).and("objectId").is(objectId).and("collectType") .is(collectTypeEnum.getType()); diff --git a/luoo_user/src/main/java/com/luoo/user/service/UserCollectInfoService.java b/luoo_user/src/main/java/com/luoo/user/service/UserCollectInfoService.java index 427caea..c5caaa7 100644 --- a/luoo_user/src/main/java/com/luoo/user/service/UserCollectInfoService.java +++ b/luoo_user/src/main/java/com/luoo/user/service/UserCollectInfoService.java @@ -21,6 +21,7 @@ import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.mongodb.core.query.Update; +import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import java.util.*; import java.util.function.Function; @@ -32,13 +33,22 @@ public class UserCollectInfoService { private RabbitTemplate rabbitTemplate; @Autowired private MongoTemplate mongoTemplate; - + @Autowired + private RedisTemplate redisTemplate; @Autowired private UserCollectInfoDao userCollectInfoDao; @Autowired private UserInfoDao userInfoDao; + public Set getCollectSet(String userId, CollectTypeEnum collectTypeEnum) { + Criteria criteria = Criteria.where("userId").is(userId).and("collectType") + .is(collectTypeEnum.getType()); + Query query = new Query(criteria); + List userCollectInfos = mongoTemplate.find(query, UserCollectInfo.class); + return userCollectInfos.isEmpty() ? Collections.emptySet() : userCollectInfos.stream().map(UserCollectInfo::getObjectId).collect(Collectors.toSet()); + } + public void saveCollect(String userId, String objectId, Integer collectType) { //自己不关注自己,自己不把自己列入黑名单 if(objectId.equals(userId)) { @@ -263,6 +273,8 @@ public class UserCollectInfoService { private void addJournalCount(UserInfo user) { user.setJournalCount(user.getJournalCount() + 1); userInfoDao.save(user); + //更新redis 中的缓存 + redisTemplate.opsForValue().set(Constants.REDIS_KEY_USER_COLLECT_JOURNAL+user.getId(),getCollectSet(user.getId(), CollectTypeEnum.JOURNAL)); } private void addSongCount(UserInfo user) { @@ -273,6 +285,8 @@ public class UserCollectInfoService { private void reduceJournalCount(UserInfo user) { user.setJournalCount(user.getJournalCount() - 1); userInfoDao.save(user); + //更新redis 中的缓存 + redisTemplate.opsForValue().set(Constants.REDIS_KEY_USER_COLLECT_JOURNAL+user.getId(),getCollectSet(user.getId(), CollectTypeEnum.JOURNAL)); } private void reduceSongCount(UserInfo user) {