1.add cache for search category and journal filter

main
Gary 9 months ago
parent 5cd9494eba
commit 7af89b0927

@ -1,10 +1,8 @@
package com.luoo.music.controller; package com.luoo.music.controller;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -29,8 +27,6 @@ import org.springframework.web.bind.annotation.*;
import com.luoo.music.pojo.Journal; import com.luoo.music.pojo.Journal;
import com.luoo.music.pojo.Tag; import com.luoo.music.pojo.Tag;
import com.luoo.music.service.JournalService; import com.luoo.music.service.JournalService;
import com.luoo.music.service.SongInfoService;
import com.luoo.music.service.TagService;
import com.luoo.music.service.UserCollectInfoService; import com.luoo.music.service.UserCollectInfoService;
import annotation.GlobalInterceptor; import annotation.GlobalInterceptor;
import annotation.VerifyParam; import annotation.VerifyParam;
@ -41,7 +37,6 @@ import constants.Constants;
import dto.UserLoginDto; import dto.UserLoginDto;
import enums.CollectTypeEnum; import enums.CollectTypeEnum;
import util.JwtUtil; import util.JwtUtil;
import util.StringTools;
/** /**
* *
@ -63,9 +58,6 @@ public class JournalController {
@Autowired @Autowired
private SongController songController; private SongController songController;
@Autowired
private TagService tagService;
@Autowired @Autowired
private JwtUtil jwtUtil; private JwtUtil jwtUtil;
@ -78,9 +70,6 @@ public class JournalController {
@Autowired @Autowired
private RedisTemplate redisTemplate; 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期期刊筛选条件对游客不可用") @ApiOperation(value = "1.查询期刊信息", notes = "若authorization为空或authorization校验失败默认返回最新的10期期刊筛选条件对游客不可用")
@GetMapping("/list") @GetMapping("/list")
@GlobalInterceptor @GlobalInterceptor
@ -230,12 +219,7 @@ public class JournalController {
@GlobalInterceptor(checkAppUserLogin = true) @GlobalInterceptor(checkAppUserLogin = true)
public Result<JournalFilterDTO> getFilterList( public Result<JournalFilterDTO> getFilterList(
@RequestHeader(value = "Authorization", required = false) String authorization) { @RequestHeader(value = "Authorization", required = false) String authorization) {
JournalFilterDTO journalFilterDTO=new JournalFilterDTO(); return Result.success(journalService.getJournalFilterDTO());
journalFilterDTO.setJournalNoList(journalService.getJournalNoList(JOURNAL_FILTER_NUMBER_RANGE));
journalFilterDTO.setLanguageList(tagService.getLanguageList().stream().map(this::getTagDTO).collect(Collectors.toList()));
journalFilterDTO.setStyleList(tagService.getStyleList().stream().map(this::getTagDTO).collect(Collectors.toList()));
return Result.success(journalFilterDTO);
} }
@ApiOperation(value = "5.根据当前期刊的标签推荐6期类似期刊") @ApiOperation(value = "5.根据当前期刊的标签推荐6期类似期刊")
@ -259,12 +243,7 @@ public class JournalController {
return Result.success(list); return Result.success(list);
} }
private TagDTO getTagDTO(Tag tag) {
TagDTO tagDTO=new TagDTO();
tagDTO.setName(tag.getNameCh());
tagDTO.setId(tag.getId());
return tagDTO;
}
@ApiOperation(value = "统计期刊的浏览数", notes = "APP端打开期刊调用一次统计接口") @ApiOperation(value = "统计期刊的浏览数", notes = "APP端打开期刊调用一次统计接口")
@RequestMapping(value="/stat/visits/{id}", method= RequestMethod.PUT) @RequestMapping(value="/stat/visits/{id}", method= RequestMethod.PUT)
public Result statVisitsCount(@ApiParam(value = "期刊ID", required = true) @PathVariable String id) { public Result statVisitsCount(@ApiParam(value = "期刊ID", required = true) @PathVariable String id) {

@ -14,13 +14,11 @@ import com.luoo.music.dto.response.SongRespDTO;
import io.swagger.annotations.*; import io.swagger.annotations.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import com.luoo.music.pojo.Journal; import com.luoo.music.pojo.Journal;
import com.luoo.music.pojo.JournalSong; import com.luoo.music.pojo.JournalSong;
import com.luoo.music.pojo.Tag;
import com.luoo.music.service.JournalService; import com.luoo.music.service.JournalService;
import com.luoo.music.service.JournalSongService; import com.luoo.music.service.JournalSongService;
import com.luoo.music.service.SearchService; import com.luoo.music.service.SearchService;
@ -29,7 +27,6 @@ import annotation.GlobalInterceptor;
import annotation.VerifyParam; import annotation.VerifyParam;
import api.PageResult; import api.PageResult;
import api.Result; import api.Result;
import constants.Constants;
/** /**
* *
@ -59,10 +56,7 @@ public class SearchController {
@GetMapping("/category") @GetMapping("/category")
@GlobalInterceptor @GlobalInterceptor
public Result<List<SearchCategoryDTO>> getCategory() { public Result<List<SearchCategoryDTO>> getCategory() {
List<Tag> tags = tagService.getCategoryTags(); return Result.success(tagService.getCategoryDTO());
List<SearchCategoryDTO> searchCategoryDTOs = tags.stream().map(this::getSearchCategoryDTO)
.collect(Collectors.toList());
return Result.success(searchCategoryDTOs);
} }
@ApiOperation(value = "2.1 搜索歌曲", notes = "根据关键词模糊搜索歌曲") @ApiOperation(value = "2.1 搜索歌曲", notes = "根据关键词模糊搜索歌曲")
@ -113,12 +107,4 @@ public class SearchController {
private int getLimit(Integer limit) { private int getLimit(Integer limit) {
return null == limit || 0 == limit ? DEFAULT_AUTO_COMPLETE_LIMIT : limit; return null == limit || 0 == limit ? DEFAULT_AUTO_COMPLETE_LIMIT : limit;
} }
private SearchCategoryDTO getSearchCategoryDTO(Tag tag) {
SearchCategoryDTO searchCategoryDTO = new SearchCategoryDTO();
BeanUtils.copyProperties(tag, searchCategoryDTO);
searchCategoryDTO.setImage(Constants.TAG_RESOURCE_PREFIX + tag.getImage());
searchCategoryDTO.setThumbnail(Constants.TAG_RESOURCE_PREFIX + tag.getThumbnail());
return searchCategoryDTO;
}
} }

@ -1,12 +1,14 @@
package com.luoo.music.dto.response; package com.luoo.music.dto.response;
import java.io.Serializable;
import java.util.List; import java.util.List;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import lombok.Data; import lombok.Data;
@Data @Data
public class JournalFilterDTO { public class JournalFilterDTO implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "风格筛选条件") @ApiModelProperty(value = "风格筛选条件")
private List<TagDTO> styleList; private List<TagDTO> styleList;
@ApiModelProperty(value = "语言筛选条件") @ApiModelProperty(value = "语言筛选条件")

@ -1,10 +1,13 @@
package com.luoo.music.dto.response; package com.luoo.music.dto.response;
import java.io.Serializable;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import lombok.Data; import lombok.Data;
@Data @Data
public class SearchCategoryDTO { public class SearchCategoryDTO implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "ID") @ApiModelProperty(value = "ID")
private String id; private String id;
@ApiModelProperty(value = "中文名") @ApiModelProperty(value = "中文名")

@ -1,10 +1,13 @@
package com.luoo.music.dto.response; package com.luoo.music.dto.response;
import java.io.Serializable;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import lombok.Data; import lombok.Data;
@Data @Data
public class TagDTO { public class TagDTO implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "ID") @ApiModelProperty(value = "ID")
private String id; private String id;
@ApiModelProperty(value = "中文名") @ApiModelProperty(value = "中文名")

@ -41,8 +41,12 @@ import com.luoo.music.dao.JournalTagDao;
import com.luoo.music.dao.TagDao; import com.luoo.music.dao.TagDao;
import com.luoo.music.dto.mapper.JournalMapper; import com.luoo.music.dto.mapper.JournalMapper;
import com.luoo.music.dto.request.JournalQueryReq; import com.luoo.music.dto.request.JournalQueryReq;
import com.luoo.music.dto.response.JournalFilterDTO;
import com.luoo.music.dto.response.JournalRespDTO; import com.luoo.music.dto.response.JournalRespDTO;
import com.luoo.music.dto.response.SearchCategoryDTO;
import com.luoo.music.dto.response.TagDTO;
import com.luoo.music.pojo.Journal; import com.luoo.music.pojo.Journal;
import com.luoo.music.pojo.Tag;
/** /**
* *
@ -53,6 +57,7 @@ import com.luoo.music.pojo.Journal;
@Service @Service
public class JournalService { public class JournalService {
private static final Pattern NUMBER_PATTERN = Pattern.compile("[0-9]*"); private static final Pattern NUMBER_PATTERN = Pattern.compile("[0-9]*");
private static final int JOURNAL_FILTER_NUMBER_RANGE=100;
@Autowired @Autowired
private JournalDao journalDao; private JournalDao journalDao;
@ -73,13 +78,9 @@ public class JournalService {
@Autowired @Autowired
private CommentDao commentDao; private CommentDao commentDao;
private Map<Integer,List<String>> JOURNAL_RANGE_MAP=new HashMap<>(); @Autowired
/* private TagService tagService;
* public void updateState(String id) { journalDao.updateState(id); }
*
*
* public void addThumbup(String id){ journalDao.addThumbup(id); }
*/
/** /**
* *
* @return * @return
@ -270,10 +271,6 @@ public class JournalService {
public List<String> getJournalNoList(int journalFilterNumberRange) { public List<String> getJournalNoList(int journalFilterNumberRange) {
//获取最大的期刊号 //获取最大的期刊号
int maxJournalNo=journalDao.getMaxJournalNo(); int maxJournalNo=journalDao.getMaxJournalNo();
if(JOURNAL_RANGE_MAP.containsKey(maxJournalNo)) {
return JOURNAL_RANGE_MAP.get(maxJournalNo);
}
JOURNAL_RANGE_MAP.clear();
List<String> journalNoList=new ArrayList<>(); List<String> journalNoList=new ArrayList<>();
int i=1; int i=1;
int start=1; int start=1;
@ -292,7 +289,6 @@ public class JournalService {
i++; i++;
} }
Collections.reverse(journalNoList); Collections.reverse(journalNoList);
JOURNAL_RANGE_MAP.put(maxJournalNo, journalNoList);
return journalNoList; return journalNoList;
} }
@ -402,4 +398,23 @@ public class JournalService {
} }
return JournalMapper.getJournalRespDTO(journal,mongoTemplate,redisTemplate,commentDao); return JournalMapper.getJournalRespDTO(journal,mongoTemplate,redisTemplate,commentDao);
} }
public JournalFilterDTO getJournalFilterDTO() {
return (JournalFilterDTO) this.cacheChannel.get("default", "journal_filter", key -> getJournalFilter(), false).getValue();
}
private JournalFilterDTO getJournalFilter() {
JournalFilterDTO journalFilterDTO=new JournalFilterDTO();
journalFilterDTO.setJournalNoList(getJournalNoList(JOURNAL_FILTER_NUMBER_RANGE));
journalFilterDTO.setLanguageList(tagService.getLanguageList().stream().map(this::getTagDTO).collect(Collectors.toList()));
journalFilterDTO.setStyleList(tagService.getStyleList().stream().map(this::getTagDTO).collect(Collectors.toList()));
return journalFilterDTO;
}
private TagDTO getTagDTO(Tag tag) {
TagDTO tagDTO=new TagDTO();
tagDTO.setName(tag.getNameCh());
tagDTO.setId(tag.getId());
return tagDTO;
}
} }

@ -1,20 +1,28 @@
package com.luoo.music.service; package com.luoo.music.service;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.luoo.music.dao.TagDao; import com.luoo.music.dao.TagDao;
import com.luoo.music.dto.response.JournalRespDTO;
import com.luoo.music.dto.response.SearchCategoryDTO;
import com.luoo.music.dto.response.TagDTO;
import com.luoo.music.pojo.Tag; import com.luoo.music.pojo.Tag;
import api.Result;
import constants.Constants;
import net.oschina.j2cache.CacheChannel;
@Service @Service
public class TagService { public class TagService {
@Autowired @Autowired
private TagDao tagDao; private TagDao tagDao;
@Autowired
private CacheChannel cacheChannel;
public List<Tag> getLanguageList() { public List<Tag> getLanguageList() {
return tagDao.getLanguageList(); return tagDao.getLanguageList();
@ -28,19 +36,26 @@ public class TagService {
return tagDao.getCategoryTags(); return tagDao.getCategoryTags();
} }
public Map<String, List<String>> getTagMap(List<String> journalIds) { public List<String> getTagNameChByJournalId(String journalId) {
return tagDao.getTagNameChByJournalId(journalId);
if (journalIds.isEmpty()) {
return Collections.emptyMap();
} }
List<Object[]> pairs = tagDao.getJounralId2TagPairs(journalIds);
return pairs.stream().collect(Collectors.groupingBy(s -> s[0].toString(),
Collectors.mapping(s -> s[1].toString(), Collectors.toList())));
@SuppressWarnings("unchecked")
public List<SearchCategoryDTO> getCategoryDTO() {
return (List<SearchCategoryDTO>) this.cacheChannel.get("default", "search_category", key -> getSearchCategoryDTO(), false).getValue();
} }
public List<String> getTagNameChByJournalId(String journalId) { private List<SearchCategoryDTO> getSearchCategoryDTO() {
return tagDao.getTagNameChByJournalId(journalId); List<Tag> tags = getCategoryTags();
return tags.stream().map(this::getSearchCategoryDTO)
.collect(Collectors.toList());
} }
private SearchCategoryDTO getSearchCategoryDTO(Tag tag) {
SearchCategoryDTO searchCategoryDTO = new SearchCategoryDTO();
BeanUtils.copyProperties(tag, searchCategoryDTO);
searchCategoryDTO.setImage(Constants.TAG_RESOURCE_PREFIX + tag.getImage());
searchCategoryDTO.setThumbnail(Constants.TAG_RESOURCE_PREFIX + tag.getThumbnail());
return searchCategoryDTO;
}
} }

Loading…
Cancel
Save