1.add redis cache

main
Gary 10 months ago
parent 84c9a11eaf
commit 955be11b23

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

@ -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<String,JournalRespDTO> 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<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<String> ids=cacheList.stream().map(JournalRespDTO::getId).collect(Collectors.toList());
Map<String,List<String>> tagMap=tagService.getTagMap(ids);
List<JournalRespDTO> list = cacheList.stream().map(j->updateCollectAndTag(j,journalCollectSet,tagMap)).collect(Collectors.toList());
return Result.success(new PageResult<JournalRespDTO>(Long.valueOf(list.size()), list));
}
List<Journal> pageList = journalService.queryPage(queryReq);
List<String> ids=pageList.stream().map(Journal::getId).collect(Collectors.toList());
Set<String> journalCollectSet = null == user ? Collections.emptySet()
: userCollectInfoService.getCollectSet(user.getUserId(),ids, CollectTypeEnum.JOURNAL);
Map<String,List<String>> tagMap=tagService.getTagMap(ids);
List<JournalRespDTO> list = pageList.stream().map(a -> JournalMapper.getJournalRespDTO(a, journalCollectSet,mongoTemplate,redisTemplate,commentDao,tagMap))
.collect(Collectors.toList());
List<JournalRespDTO> list = pageList.stream().map(a -> JournalMapper.getJournalRespDTO(a, journalCollectSet,tagMap)).collect(Collectors.toList());
return Result.success(new PageResult<JournalRespDTO>(Long.valueOf(list.size()), list));
}
private JournalRespDTO updateCollectAndTag(JournalRespDTO journalRespDTO,Set<String> journalCollectSet, Map<String, List<String>> tagMap) {
journalRespDTO.setTags(tagMap.getOrDefault(journalRespDTO.getId(), Collections.emptyList()));
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);
List<String> ids=pageList.stream().map(Journal::getId).collect(Collectors.toList());
Map<String,List<String>> 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<String> getJournalCollectSet(String userId) {
Set<String> set=(Set<String>) 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),

@ -30,4 +30,7 @@ public interface TagDao extends JpaRepository<Tag, String>, 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<Object[]> getJounralId2TagPairs(List<String> 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<String> getTagNameChByJournalId(String journalId);
}

@ -79,7 +79,9 @@ public class JournalMapper {
}
return journalRespDTO;
}
public static List<String> getTags(String journalId,String journalNo,Map<String, List<String>> tagMap){
return tagMap.getOrDefault(journalId, getTags(journalNo));
}
public static JournalRespDTO getJournalRespDTO(Journal journal, Set<String> journalCollectSet,
MongoTemplate mongoTemplate, RedisTemplate redisTemplate, CommentDao commentDao,
Map<String, List<String>> tagMap) {

@ -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 = "剘刊号")

@ -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<String> getTagNameChByJournalId(String journalId){
return tagDao.getTagNameChByJournalId(journalId);
}
}

@ -38,6 +38,14 @@ public class UserCollectInfoService {
return userCollectInfos.isEmpty() ? Collections.emptySet() : userCollectInfos.stream().map(UserCollectInfo::getObjectId).collect(Collectors.toSet());
}
public Set<String> getCollectSet(String userId, CollectTypeEnum collectTypeEnum) {
Criteria criteria = Criteria.where("userId").is(userId).and("collectType")
.is(collectTypeEnum.getType());
Query query = new Query(criteria);
List<UserCollectInfo> 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());

@ -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<String> getCollectSet(String userId, CollectTypeEnum collectTypeEnum) {
Criteria criteria = Criteria.where("userId").is(userId).and("collectType")
.is(collectTypeEnum.getType());
Query query = new Query(criteria);
List<UserCollectInfo> 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) {

Loading…
Cancel
Save