From 1b41cf2ee056ebefdb977fef997c0542c2b1feec Mon Sep 17 00:00:00 2001 From: Gary Date: Thu, 25 Apr 2024 16:16:41 +0800 Subject: [PATCH 1/5] =?UTF-8?q?1.fix=20full=20text=20fuzzy=20search=20for?= =?UTF-8?q?=20case=20'=E8=A5=BF=E5=AE=89=E7=8B=AC=E7=AB=8B=E9=9F=B3?= =?UTF-8?q?=E4=B9=90=E5=90=88=E8=BE=912=E2=80=93=E5=BA=9F=E5=9F=8E?= =?UTF-8?q?=E7=94=9C=E6=A2=A6'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/luoo/music/service/JournalService.java | 8 +++++++- .../java/com/luoo/music/service/JournalSongService.java | 8 +++++++- 2 files changed, 14 insertions(+), 2 deletions(-) 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 10d71d7..ac22197 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 @@ -23,6 +23,8 @@ 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 org.springframework.util.CollectionUtils; + import util.StringTools; import com.luoo.music.dao.CommentDao; @@ -297,7 +299,11 @@ public class JournalService { if(keyword.length()==1) { return searchByLike(keyword,pageRequest); } - return journalDao.fuzzySearch("+\""+keyword+"\"", pageRequest.getPageNumber()*pageRequest.getPageSize(), pageRequest.getPageSize()); + List fullTextFuzzySearchResults = journalDao.fuzzySearch("+\""+keyword+"\"", pageRequest.getPageNumber()*pageRequest.getPageSize(), pageRequest.getPageSize()); + if(CollectionUtils.isEmpty(fullTextFuzzySearchResults)) { + return searchByLike(keyword, pageRequest); + } + return fullTextFuzzySearchResults; } public static boolean isNumeric(String str){ diff --git a/luoo_music/src/main/java/com/luoo/music/service/JournalSongService.java b/luoo_music/src/main/java/com/luoo/music/service/JournalSongService.java index c0644a9..a44d146 100644 --- a/luoo_music/src/main/java/com/luoo/music/service/JournalSongService.java +++ b/luoo_music/src/main/java/com/luoo/music/service/JournalSongService.java @@ -12,6 +12,8 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.jpa.domain.Specification; import org.springframework.stereotype.Service; +import org.springframework.util.CollectionUtils; + import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Predicate; @@ -144,7 +146,11 @@ public class JournalSongService { if(keyword.length()==1) { return searchByLike(keyword,pageRequest); } - return journalSongDao.fuzzySearch("+\""+keyword+"\"", pageRequest.getPageNumber()*pageRequest.getPageSize(), pageRequest.getPageSize()); + List fullTextFuzzySearchResults = journalSongDao.fuzzySearch("+\""+keyword+"\"", pageRequest.getPageNumber()*pageRequest.getPageSize(), pageRequest.getPageSize()); + if(CollectionUtils.isEmpty(fullTextFuzzySearchResults)) { + return searchByLike(keyword, pageRequest); + } + return fullTextFuzzySearchResults; } private List searchByLike(String keyword, PageRequest pageRequest) { From c2616cc9186c32e527c8b7969ca1bc407677a002 Mon Sep 17 00:00:00 2001 From: Gary Date: Mon, 29 Apr 2024 11:31:57 +0800 Subject: [PATCH 2/5] 1.update default timeout of redis client lettuce for j2cache --- luoo_comment/src/main/resources/bootstrap.yml | 5 +++-- luoo_music/src/main/resources/bootstrap.yml | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/luoo_comment/src/main/resources/bootstrap.yml b/luoo_comment/src/main/resources/bootstrap.yml index e5b99ed..c4703ea 100644 --- a/luoo_comment/src/main/resources/bootstrap.yml +++ b/luoo_comment/src/main/resources/bootstrap.yml @@ -7,10 +7,11 @@ spring: uri: http://116.62.145.60:12000 # uri: http://127.0.0.1:12000 -redis: +lettuce: # 地址, 多个地址使用‘,’逗号分割 hosts: ${spring.redis.host}:${spring.redis.port} password: ${spring.redis.password} + timeout: 180000 j2cache: openSpringCache: true @@ -26,7 +27,7 @@ j2cache: L2: #使用springRedis替换二级缓存 provider_class: net.oschina.j2cache.cache.support.redis.SpringRedisProvider - config_section: redis + config_section: lettuce #使用springRedis进行广播通知缓失效 broadcast: net.oschina.j2cache.cache.support.redis.SpringRedisPubSubPolicy diff --git a/luoo_music/src/main/resources/bootstrap.yml b/luoo_music/src/main/resources/bootstrap.yml index 9d82c57..956ca39 100644 --- a/luoo_music/src/main/resources/bootstrap.yml +++ b/luoo_music/src/main/resources/bootstrap.yml @@ -10,10 +10,11 @@ spring: multipart: max-file-size: 20MB max-request-size: 200MB -redis: +lettuce: # 地址, 多个地址使用‘,’逗号分割 hosts: ${spring.redis.host}:${spring.redis.port} password: ${spring.redis.password} + timeout: 180000 j2cache: openSpringCache: true # 缓存中不存在时,运行缓存空对象 @@ -28,7 +29,7 @@ j2cache: L2: #使用springRedis替换二级缓存 provider_class: net.oschina.j2cache.cache.support.redis.SpringRedisProvider - config_section: redis + config_section: lettuce #使用springRedis进行广播通知缓失效 broadcast: net.oschina.j2cache.cache.support.redis.SpringRedisPubSubPolicy From dfd43ffae52e2843f4ef46c53ea5736a714871d7 Mon Sep 17 00:00:00 2001 From: Gary Date: Mon, 29 Apr 2024 13:49:38 +0800 Subject: [PATCH 3/5] 1.async clean cache when update/delete/add journal --- .../java/com/luoo/music/MusicApplication.java | 3 ++ .../controller/CMSJournalController.java | 38 ++++--------------- .../music/listener/CleanJournalCache.java | 33 +++++++++++++++- 3 files changed, 42 insertions(+), 32 deletions(-) diff --git a/luoo_music/src/main/java/com/luoo/music/MusicApplication.java b/luoo_music/src/main/java/com/luoo/music/MusicApplication.java index 1ee26b6..f13a989 100644 --- a/luoo_music/src/main/java/com/luoo/music/MusicApplication.java +++ b/luoo_music/src/main/java/com/luoo/music/MusicApplication.java @@ -13,6 +13,8 @@ import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.core.env.Environment; import org.springframework.data.jpa.repository.config.EnableJpaAuditing; +import org.springframework.scheduling.annotation.EnableAsync; + import util.IdWorker; import util.JwtUtil; @@ -27,6 +29,7 @@ import java.net.UnknownHostException; @EnableFeignClients @EnableJpaAuditing @EnableCaching +@EnableAsync public class MusicApplication { static Logger logger= LoggerFactory.getLogger(MusicApplication.class); public static void main(String[] args) throws UnknownHostException { 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 5bebfbe..75279a9 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 @@ -9,6 +9,7 @@ import com.luoo.music.dto.request.cms.JournalPublishReq; import com.luoo.music.dto.request.cms.JournalQueryModel; import com.luoo.music.dto.response.cms.JournalListRespDto; import com.luoo.music.dto.response.cms.JournalRespDto; +import com.luoo.music.listener.CleanJournalCache; import com.luoo.music.service.CMSJournalService; import com.luoo.music.service.S3Service; import io.swagger.annotations.Api; @@ -35,9 +36,8 @@ public class CMSJournalController { private CMSJournalService journalService; @Autowired private S3Service s3Service; - @Autowired - private CacheChannel cacheChannel; + private CleanJournalCache cleanJournalCache; @ApiOperation(value = "查询期刊列表", notes = "查询期刊列表") @RequestMapping(value="/search/{page}/{size}", method= RequestMethod.POST) @@ -50,20 +50,10 @@ public class CMSJournalController { @ApiOperation(value = "新增期刊", notes = "新增期刊") @RequestMapping(value = "/add", method = RequestMethod.POST) public Result add(@ApiParam(value = "期刊信息", required = true) @Valid @RequestBody JournalAddModel addModel){ - cleanCache(); + cleanJournalCache.cleanCache(); return journalService.add(addModel); } - private void cleanCache() { - try { - cacheChannel.evict("default", "journal_filter"); - cacheChannel.clear(Constants.J2CACHE_REGION_JOURNAL_QUERY_PAGE); - cacheChannel.clear(Constants.J2CACHE_REGION_JOURNAL_QUERY_COUNT); - }catch(Exception e) { - e.printStackTrace(); - } - } - @ApiOperation(value = "查询期刊详情", notes = "查询期刊详情") @RequestMapping(value="/{id}",method= RequestMethod.GET) public Result findOne(@ApiParam(value = "期刊ID", required = true) @PathVariable String id){ @@ -74,7 +64,7 @@ public class CMSJournalController { @RequestMapping(value = "/{id}", method = RequestMethod.PUT) public Result update(@ApiParam(value = "期刊ID", required = true) @PathVariable String id, @ApiParam(value = "期刊信息", required = true) @RequestBody JournalAddModel updateModel){ - cleanCache(id); + cleanJournalCache.cleanCache(id); return journalService.update(id, updateModel); } @@ -82,36 +72,22 @@ public class CMSJournalController { @RequestMapping(value="/publish/{id}",method= RequestMethod.PUT) public Result publish(@ApiParam(value = "期刊ID", required = true) @PathVariable String id, @ApiParam(value = "期刊发布请求对象", required = true) @RequestBody JournalPublishReq queryModel){ - cleanCache(id); + cleanJournalCache.cleanCache(id); return journalService.publish(id, queryModel); } - private void cleanCache(String id) { - try { - cacheChannel.evict("default", "journal_filter"); - cacheChannel.clear(Constants.J2CACHE_REGION_JOURNAL_QUERY_PAGE); - cacheChannel.clear(Constants.J2CACHE_REGION_JOURNAL_QUERY_COUNT); - cacheChannel.evict(constants.Constants.J2CACHE_REGION_JOURNAL_ID, id); - String journalNo=journalService.findJournalNoById(id); - cacheChannel.evict(constants.Constants.J2CACHE_REGION_JOURNAL_SONG_LIST, journalNo); - cacheChannel.evict(constants.Constants.J2CACHE_REGION_JOURNAL_NO,journalNo); - }catch(Exception e) { - e.printStackTrace(); - } - } - @ApiOperation(value = "更新期刊启停状态", notes = "更新期刊启停状态") @RequestMapping(value="/update/state/{id}",method= RequestMethod.PUT) public Result updateColumnState(@ApiParam(value = "期刊ID", required = true) @PathVariable String id, @ApiParam(value = "期刊启停状态,0:停用,1:启用", required = true) @RequestBody String state){ - cleanCache(id); + cleanJournalCache.cleanCache(id); return journalService.updateJournalState(id, state); } @ApiOperation(value = "删除单条期刊", notes = "物理删除") @RequestMapping(value="/{id}", method= RequestMethod.DELETE) public Result delete(@ApiParam(value = "期刊ID", required = true) @PathVariable String id){ - cleanCache(id); + cleanJournalCache.cleanCache(id); return journalService.deleteById(id); } diff --git a/luoo_music/src/main/java/com/luoo/music/listener/CleanJournalCache.java b/luoo_music/src/main/java/com/luoo/music/listener/CleanJournalCache.java index 7dd26cf..7db3759 100644 --- a/luoo_music/src/main/java/com/luoo/music/listener/CleanJournalCache.java +++ b/luoo_music/src/main/java/com/luoo/music/listener/CleanJournalCache.java @@ -3,6 +3,7 @@ package com.luoo.music.listener; import constants.Constants; import net.oschina.j2cache.CacheChannel; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; import com.luoo.music.dao.JournalDao; @@ -20,7 +21,37 @@ public class CleanJournalCache { private CacheChannel cacheChannel; @Autowired private JournalDao journalDao; - + + @Async + public void cleanCache() { + try { + cacheChannel.evict("default", "journal_filter"); + cacheChannel.clear(Constants.J2CACHE_REGION_JOURNAL_QUERY_PAGE); + cacheChannel.clear(Constants.J2CACHE_REGION_JOURNAL_QUERY_COUNT); + }catch(Exception e) { + e.printStackTrace(); + } + } + + @Async + public void cleanCache(String id) { + try { + cacheChannel.evict("default", "journal_filter"); + cacheChannel.clear(Constants.J2CACHE_REGION_JOURNAL_QUERY_PAGE); + cacheChannel.clear(Constants.J2CACHE_REGION_JOURNAL_QUERY_COUNT); + cacheChannel.evict(constants.Constants.J2CACHE_REGION_JOURNAL_ID, id); + Optional optional = journalDao.findById(id); + if (!optional.isPresent()) { + return; + } + String journalNo=optional.get().getJournalNo(); + cacheChannel.evict(constants.Constants.J2CACHE_REGION_JOURNAL_SONG_LIST, journalNo); + cacheChannel.evict(constants.Constants.J2CACHE_REGION_JOURNAL_NO,journalNo); + }catch(Exception e) { + e.printStackTrace(); + } + } + public void clean(String journalId) { Optional optional = journalDao.findById(journalId); if (!optional.isPresent()) { From 734121bd36e02d4af0aa12fa2ecec771e9b8a887 Mon Sep 17 00:00:00 2001 From: wangqing <408564126@qq.com> Date: Mon, 29 Apr 2024 22:29:38 +0800 Subject: [PATCH 4/5] fix children's comment --- .../java/com/luoo/comment/controller/CommentController.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/luoo_comment/src/main/java/com/luoo/comment/controller/CommentController.java b/luoo_comment/src/main/java/com/luoo/comment/controller/CommentController.java index faa84ba..c7866db 100644 --- a/luoo_comment/src/main/java/com/luoo/comment/controller/CommentController.java +++ b/luoo_comment/src/main/java/com/luoo/comment/controller/CommentController.java @@ -400,7 +400,7 @@ public class CommentController extends BaseController { List list = (List) cacheChannel.get(Constants.J2CACHE_REGION_JOURNAL_COMMENT_PAGE_CHILDREN+"_"+parentId,page+"_"+size, key->getChildrenCommentList(parentId,page,size),true).getValue(); // List list = getChildrenCommentList(parentId,page,size); //验证是否登录,并且拿到ID - Long count = (Long) cacheChannel.get(Constants.J2CACHE_REGION_JOURNAL_COMMENT_PAGE_CHILDREN_COUNT,parentId, key->getChildrenCommentListCount(parentId,page,size),true).getValue(); + Integer count = (Integer) cacheChannel.get(Constants.J2CACHE_REGION_JOURNAL_COMMENT_PAGE_CHILDREN_COUNT,parentId, key->getChildrenCommentListCount(parentId,page,size),true).getValue(); UserLoginDto userLoginDto = getUserLoginDto(authorization); if (null == userLoginDto) { @@ -409,7 +409,7 @@ public class CommentController extends BaseController { String userId = userLoginDto.getUserId(); Set publicationLikeSet = publicationLikeDao.findAllByUserId(userId).parallelStream().map(p->p.getLikedItemId()).collect(Collectors.toSet()); list = list.parallelStream().map(c->updateHaveThumbup(c,publicationLikeSet)).collect(Collectors.toList()); - return Result.success(new PageResult(count,list)); + return Result.success(new PageResult(Long.valueOf(count),list)); } From fb7ea2ae1356c8a60128266350ae0abc7d5e5e4f Mon Sep 17 00:00:00 2001 From: Gary Date: Tue, 30 Apr 2024 14:55:38 +0800 Subject: [PATCH 5/5] 1.remove invalid cache in journal query page --- .../java/com/luoo/music/controller/JournalController.java | 6 ++++++ .../main/java/com/luoo/music/controller/SongController.java | 3 +++ .../main/java/com/luoo/music/service/JournalService.java | 4 ++-- 3 files changed, 11 insertions(+), 2 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 4a843d4..0a55915 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 @@ -70,6 +70,9 @@ public class JournalController { UserLoginDto user = jwtUtil.getUserLoginDto(authorization); updateQuery(deviceType,user,queryReq); Long count = journalService.countJournal(queryReq); + if(0==count) { + return Result.success(new PageResult(count, Collections.emptyList())); + } List pageList=journalService.queryJournalByPage(queryReq); Set journalCollectSet = getJournalCollectSet(user); updateCollectStatus(pageList,journalCollectSet); @@ -115,6 +118,9 @@ public class JournalController { return Result.success(new PageResult(0L, Collections.emptyList())); } Long count = Long.valueOf(getJournalCollectSet(queryReq.getUserId()).size()); + if(0==count) { + return Result.success(new PageResult(count, Collections.emptyList())); + } List pageList = journalService.orderByField(objectIds); UserLoginDto user = jwtUtil.getUserLoginDto(authorization); Set journalCollectSet = getMyCollectSet(user.getUserId(),queryReq.getUserId(),objectIds); diff --git a/luoo_music/src/main/java/com/luoo/music/controller/SongController.java b/luoo_music/src/main/java/com/luoo/music/controller/SongController.java index bb95c7e..87d8074 100644 --- a/luoo_music/src/main/java/com/luoo/music/controller/SongController.java +++ b/luoo_music/src/main/java/com/luoo/music/controller/SongController.java @@ -109,6 +109,9 @@ public class SongController { return Result.success(new PageResult(0L, Collections.emptyList())); } Long count = (long) getSongCollectSet(queryUserId).size(); + if(0==count) { + return Result.success(new PageResult(count, Collections.emptyList())); + } List songs = journalSongService.orderByField(objectIds); Set songCollectSet = getMyCollectSet(loginUserId,queryUserId,objectIds,isJouranlSource); List results = songs.stream().map(s -> SongMapper.getSongRespDTO(s, songCollectSet)) 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 ac22197..60e971c 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 @@ -357,9 +357,9 @@ public class JournalService { @SuppressWarnings("unchecked") public List queryJournalByPage(JournalQueryReq queryReq) { if(StringTools.isEmpty(queryReq.getCategoryId())&&StringTools.isEmpty(queryReq.getJournalNoRange())) { - return (List) this.cacheChannel.get(Constants.J2CACHE_REGION_JOURNAL_QUERY_PAGE, queryReq.toString(), key -> getJournalRespDTOWithComment(queryReq) , true).getValue() ; + return (List) this.cacheChannel.get(Constants.J2CACHE_REGION_JOURNAL_QUERY_PAGE, queryReq.toString(), key -> getJournalRespDTOWithComment(queryReq) , false).getValue() ; } - List list= (List) this.cacheChannel.get(Constants.J2CACHE_REGION_JOURNAL_QUERY_PAGE, queryReq.toString(), key -> getJournalRespDTO(queryReq) , true).getValue() ; + List list= (List) this.cacheChannel.get(Constants.J2CACHE_REGION_JOURNAL_QUERY_PAGE, queryReq.toString(), key -> getJournalRespDTO(queryReq) , false).getValue() ; list.parallelStream().forEach(j->{ updateComment(j); });