From 7af89b09270163165f0bd0076fc095bca66e1888 Mon Sep 17 00:00:00 2001 From: Gary Date: Mon, 4 Mar 2024 17:23:49 +0800 Subject: [PATCH 1/3] 1.add cache for search category and journal filter --- .../music/controller/JournalController.java | 25 +---------- .../music/controller/SearchController.java | 16 +------- .../music/dto/response/JournalFilterDTO.java | 4 +- .../music/dto/response/SearchCategoryDTO.java | 5 ++- .../com/luoo/music/dto/response/TagDTO.java | 5 ++- .../luoo/music/service/JournalService.java | 39 ++++++++++++------ .../com/luoo/music/service/TagService.java | 41 +++++++++++++------ 7 files changed, 69 insertions(+), 66 deletions(-) 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 5b07243..f35b0a0 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 @@ -1,10 +1,8 @@ package com.luoo.music.controller; import java.util.Collections; -import java.util.HashMap; import java.util.HashSet; import java.util.List; -import java.util.Map; import java.util.Set; import java.util.concurrent.TimeUnit; 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.Tag; 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 annotation.GlobalInterceptor; import annotation.VerifyParam; @@ -41,7 +37,6 @@ import constants.Constants; import dto.UserLoginDto; import enums.CollectTypeEnum; import util.JwtUtil; -import util.StringTools; /** * 控制器层 @@ -63,9 +58,6 @@ public class JournalController { @Autowired private SongController songController; - @Autowired - private TagService tagService; - @Autowired private JwtUtil jwtUtil; @@ -78,9 +70,6 @@ public class JournalController { @Autowired 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 @@ -230,12 +219,7 @@ public class JournalController { @GlobalInterceptor(checkAppUserLogin = true) public Result getFilterList( @RequestHeader(value = "Authorization", required = false) String authorization) { - JournalFilterDTO journalFilterDTO=new JournalFilterDTO(); - 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); + return Result.success(journalService.getJournalFilterDTO()); } @ApiOperation(value = "5.根据当前期刊的标签推荐6期类似期刊") @@ -259,12 +243,7 @@ public class JournalController { 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端打开期刊调用一次统计接口") @RequestMapping(value="/stat/visits/{id}", method= RequestMethod.PUT) public Result statVisitsCount(@ApiParam(value = "期刊ID", required = true) @PathVariable String id) { diff --git a/luoo_music/src/main/java/com/luoo/music/controller/SearchController.java b/luoo_music/src/main/java/com/luoo/music/controller/SearchController.java index a7268c6..05fb856 100644 --- a/luoo_music/src/main/java/com/luoo/music/controller/SearchController.java +++ b/luoo_music/src/main/java/com/luoo/music/controller/SearchController.java @@ -14,13 +14,11 @@ import com.luoo.music.dto.response.SongRespDTO; import io.swagger.annotations.*; -import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.PageRequest; import org.springframework.web.bind.annotation.*; import com.luoo.music.pojo.Journal; import com.luoo.music.pojo.JournalSong; -import com.luoo.music.pojo.Tag; import com.luoo.music.service.JournalService; import com.luoo.music.service.JournalSongService; import com.luoo.music.service.SearchService; @@ -29,7 +27,6 @@ import annotation.GlobalInterceptor; import annotation.VerifyParam; import api.PageResult; import api.Result; -import constants.Constants; /** * 控制器层 @@ -59,10 +56,7 @@ public class SearchController { @GetMapping("/category") @GlobalInterceptor public Result> getCategory() { - List tags = tagService.getCategoryTags(); - List searchCategoryDTOs = tags.stream().map(this::getSearchCategoryDTO) - .collect(Collectors.toList()); - return Result.success(searchCategoryDTOs); + return Result.success(tagService.getCategoryDTO()); } @ApiOperation(value = "2.1 搜索歌曲", notes = "根据关键词模糊搜索歌曲") @@ -113,12 +107,4 @@ public class SearchController { private int getLimit(Integer 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; - } } diff --git a/luoo_music/src/main/java/com/luoo/music/dto/response/JournalFilterDTO.java b/luoo_music/src/main/java/com/luoo/music/dto/response/JournalFilterDTO.java index 4f55bd4..1675d11 100644 --- a/luoo_music/src/main/java/com/luoo/music/dto/response/JournalFilterDTO.java +++ b/luoo_music/src/main/java/com/luoo/music/dto/response/JournalFilterDTO.java @@ -1,12 +1,14 @@ package com.luoo.music.dto.response; +import java.io.Serializable; import java.util.List; import io.swagger.annotations.ApiModelProperty; import lombok.Data; @Data -public class JournalFilterDTO { +public class JournalFilterDTO implements Serializable { + private static final long serialVersionUID = 1L; @ApiModelProperty(value = "风格筛选条件") private List styleList; @ApiModelProperty(value = "语言筛选条件") diff --git a/luoo_music/src/main/java/com/luoo/music/dto/response/SearchCategoryDTO.java b/luoo_music/src/main/java/com/luoo/music/dto/response/SearchCategoryDTO.java index 7a064d0..3a60076 100644 --- a/luoo_music/src/main/java/com/luoo/music/dto/response/SearchCategoryDTO.java +++ b/luoo_music/src/main/java/com/luoo/music/dto/response/SearchCategoryDTO.java @@ -1,10 +1,13 @@ package com.luoo.music.dto.response; +import java.io.Serializable; + import io.swagger.annotations.ApiModelProperty; import lombok.Data; @Data -public class SearchCategoryDTO { +public class SearchCategoryDTO 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/dto/response/TagDTO.java b/luoo_music/src/main/java/com/luoo/music/dto/response/TagDTO.java index 63bed1f..349b16d 100644 --- a/luoo_music/src/main/java/com/luoo/music/dto/response/TagDTO.java +++ b/luoo_music/src/main/java/com/luoo/music/dto/response/TagDTO.java @@ -1,10 +1,13 @@ package com.luoo.music.dto.response; +import java.io.Serializable; + import io.swagger.annotations.ApiModelProperty; import lombok.Data; @Data -public class TagDTO { +public class TagDTO 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/JournalService.java b/luoo_music/src/main/java/com/luoo/music/service/JournalService.java index d1c1e62..57b4b72 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 @@ -41,8 +41,12 @@ 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.JournalFilterDTO; 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.Tag; /** * 服务层 @@ -53,6 +57,7 @@ import com.luoo.music.pojo.Journal; @Service public class JournalService { private static final Pattern NUMBER_PATTERN = Pattern.compile("[0-9]*"); + private static final int JOURNAL_FILTER_NUMBER_RANGE=100; @Autowired private JournalDao journalDao; @@ -73,13 +78,9 @@ public class JournalService { @Autowired private CommentDao commentDao; - private Map> JOURNAL_RANGE_MAP=new HashMap<>(); - /* - * public void updateState(String id) { journalDao.updateState(id); } - * - * - * public void addThumbup(String id){ journalDao.addThumbup(id); } - */ + @Autowired + private TagService tagService; + /** * 查询全部列表 * @return @@ -270,10 +271,6 @@ public class JournalService { public List getJournalNoList(int journalFilterNumberRange) { //获取最大的期刊号 int maxJournalNo=journalDao.getMaxJournalNo(); - if(JOURNAL_RANGE_MAP.containsKey(maxJournalNo)) { - return JOURNAL_RANGE_MAP.get(maxJournalNo); - } - JOURNAL_RANGE_MAP.clear(); List journalNoList=new ArrayList<>(); int i=1; int start=1; @@ -292,7 +289,6 @@ public class JournalService { i++; } Collections.reverse(journalNoList); - JOURNAL_RANGE_MAP.put(maxJournalNo, journalNoList); return journalNoList; } @@ -402,4 +398,23 @@ public class JournalService { } 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; + } } 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 15e88e7..50b4d98 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 @@ -1,20 +1,28 @@ package com.luoo.music.service; -import java.util.Collections; import java.util.List; -import java.util.Map; import java.util.stream.Collectors; +import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; 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 api.Result; +import constants.Constants; +import net.oschina.j2cache.CacheChannel; + @Service public class TagService { @Autowired private TagDao tagDao; + @Autowired + private CacheChannel cacheChannel; public List getLanguageList() { return tagDao.getLanguageList(); @@ -28,19 +36,26 @@ public class TagService { return tagDao.getCategoryTags(); } - public Map> getTagMap(List journalIds) { - - if (journalIds.isEmpty()) { - return Collections.emptyMap(); - } - List pairs = tagDao.getJounralId2TagPairs(journalIds); - 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); } + @SuppressWarnings("unchecked") + public List getCategoryDTO() { + return (List) this.cacheChannel.get("default", "search_category", key -> getSearchCategoryDTO(), false).getValue(); + } + + private List getSearchCategoryDTO() { + List 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; + } } From 90d710f099645da8343bf8e8cb66dca15c3692b7 Mon Sep 17 00:00:00 2001 From: Gary Date: Mon, 4 Mar 2024 17:37:39 +0800 Subject: [PATCH 2/3] 1.evict cache when journal add/delete --- .../java/com/luoo/music/controller/CMSJournalController.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/luoo_music/src/main/java/com/luoo/music/controller/CMSJournalController.java b/luoo_music/src/main/java/com/luoo/music/controller/CMSJournalController.java index 3c27535..50a014c 100644 --- a/luoo_music/src/main/java/com/luoo/music/controller/CMSJournalController.java +++ b/luoo_music/src/main/java/com/luoo/music/controller/CMSJournalController.java @@ -50,6 +50,7 @@ public class CMSJournalController { @ApiOperation(value = "新增期刊", notes = "新增期刊") @RequestMapping(value = "/add", method = RequestMethod.POST) public Result add(@ApiParam(value = "期刊信息", required = true) @Valid @RequestBody JournalAddModel addModel){ + cacheChannel.evict("default", "journal_filter"); cacheChannel.clear(Constants.J2CACHE_REGION_JOURNAL_QUERY_PAGE); return journalService.add(addModel); } @@ -86,6 +87,7 @@ public class CMSJournalController { @ApiOperation(value = "删除单条期刊", notes = "物理删除") @RequestMapping(value="/{id}", method= RequestMethod.DELETE) public Result delete(@ApiParam(value = "期刊ID", required = true) @PathVariable String id){ + cacheChannel.evict("default", "journal_filter"); cacheChannel.clear(Constants.J2CACHE_REGION_JOURNAL_QUERY_PAGE); return journalService.deleteById(id); } From f298ad1f3182fa5a76a8b28e67336796f6d814fe Mon Sep 17 00:00:00 2001 From: Gary Date: Mon, 4 Mar 2024 17:44:49 +0800 Subject: [PATCH 3/3] 1.prevent user login when user status is disable --- .../src/main/java/com/luoo/user/service/UserInfoService.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/luoo_user/src/main/java/com/luoo/user/service/UserInfoService.java b/luoo_user/src/main/java/com/luoo/user/service/UserInfoService.java index 8a8ffad..adb9536 100644 --- a/luoo_user/src/main/java/com/luoo/user/service/UserInfoService.java +++ b/luoo_user/src/main/java/com/luoo/user/service/UserInfoService.java @@ -210,6 +210,9 @@ public class UserInfoService { user.setLastUseDeviceBrand(userInfo.getLastUseDeviceBrand()); user.setLastLoginIp(userInfo.getLastLoginIp()); } + if (!UserStatusEnum.ENABLE.getStatus().equals(userInfo.getStatus())) { + return null; + } user.setLastLoginTime(new Date()); userInfoDao.save(user); return Constants.TOKEN_PREFIX