From 9cf043ace80af263e4a90ae60500abccc7d7f961 Mon Sep 17 00:00:00 2001 From: Gary Date: Mon, 15 Apr 2024 02:02:40 +0800 Subject: [PATCH] 1.add total in page result --- luoo_comment/src/main/resources/bootstrap.yml | 1 + .../src/main/java/constants/Constants.java | 2 ++ .../music/controller/CMSJournalController.java | 2 ++ .../music/controller/JournalController.java | 6 ++++-- .../luoo/music/controller/SongController.java | 18 ++++++++++++++++-- .../java/com/luoo/music/dao/JournalDao.java | 3 +++ .../com/luoo/music/dao/JournalSongDao.java | 3 +++ .../music/dto/request/JournalQueryReq.java | 5 ++++- .../com/luoo/music/service/JournalService.java | 11 +++++++++++ .../luoo/music/service/JournalSongService.java | 2 -- .../music/service/UserCollectInfoService.java | 7 +++++-- luoo_music/src/main/resources/bootstrap.yml | 1 + .../user/service/UserCollectInfoService.java | 12 ++++++++++-- 13 files changed, 62 insertions(+), 11 deletions(-) diff --git a/luoo_comment/src/main/resources/bootstrap.yml b/luoo_comment/src/main/resources/bootstrap.yml index df71fa5..e5b99ed 100644 --- a/luoo_comment/src/main/resources/bootstrap.yml +++ b/luoo_comment/src/main/resources/bootstrap.yml @@ -35,6 +35,7 @@ caffeine: region: default: 100000, 365d journal_query_page: 10000, 365d + journal_query_count: 10000, 365d journal_id: 2000, 365d journal_no: 2000, 365d journal_song_key: 10000, 365d diff --git a/luoo_common/src/main/java/constants/Constants.java b/luoo_common/src/main/java/constants/Constants.java index 5c25514..a896e7b 100644 --- a/luoo_common/src/main/java/constants/Constants.java +++ b/luoo_common/src/main/java/constants/Constants.java @@ -9,9 +9,11 @@ public class Constants { public static final String REDIS_KEY_USER_COLLECT_JOURNAL = "redis_key_user_collect_journal:"; + public static final String REDIS_KEY_USER_COLLECT_SONG = "redis_key_user_collect_song:"; public static final String J2CACHE_REGION_JOURNAL_QUERY_PAGE = "journal_query_page"; + public static final String J2CACHE_REGION_JOURNAL_QUERY_COUNT = "journal_query_count"; public static final String J2CACHE_REGION_JOURNAL_ID = "journal_id"; public static final String J2CACHE_REGION_JOURNAL_NO = "journal_no"; public static final String J2CACHE_REGION_JOURNAL_NO_SONG_ID = "journal_song_key"; 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 b48be0b..ba470d9 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 @@ -52,6 +52,7 @@ public class CMSJournalController { public Result add(@ApiParam(value = "期刊信息", required = true) @Valid @RequestBody JournalAddModel addModel){ cacheChannel.evict("default", "journal_filter"); cacheChannel.clear(Constants.J2CACHE_REGION_JOURNAL_QUERY_PAGE); + cacheChannel.clear(Constants.J2CACHE_REGION_JOURNAL_QUERY_COUNT); return journalService.add(addModel); } @@ -80,6 +81,7 @@ public class CMSJournalController { private void cleanCache(String id) { 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); 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 28413f8..3f9ea91 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 @@ -77,11 +77,12 @@ public class JournalController { @VerifyParam JournalQueryReq queryReq) { UserLoginDto user = jwtUtil.getUserLoginDto(authorization); updateQuery(user,queryReq); + Long count = journalService.countJournal(queryReq); List pageList=journalService.queryJournalByPage(queryReq); // pageList = pageList.stream().map(journalRespDTO -> randomCDN(journalRespDTO)).collect(Collectors.toList()); Set journalCollectSet = getJournalCollectSet(user); updateCollectStatus(pageList,journalCollectSet); - return Result.success(new PageResult(Long.valueOf(pageList.size()), pageList)); + return Result.success(new PageResult(count, pageList)); } private void updateCollectStatus(List pageList, Set journalCollectSet) { @@ -119,13 +120,14 @@ public class JournalController { if (objectIds.isEmpty()) { return Result.success(new PageResult(0L, Collections.emptyList())); } + Long count = Long.valueOf(getJournalCollectSet(queryReq.getUserId()).size()); List pageList = journalService.orderByField(objectIds); UserLoginDto user = jwtUtil.getUserLoginDto(authorization); Set journalCollectSet = getMyCollectSet(user.getUserId(),queryReq.getUserId(),objectIds); List list = pageList.stream().map(a -> JournalMapper.getJournalRespDTO(a, journalCollectSet)) .collect(Collectors.toList()); // list = list.stream().map(journalRespDTO -> randomCDN(journalRespDTO)).collect(Collectors.toList()); - return Result.success(new PageResult(Long.valueOf(list.size()), list)); + return Result.success(new PageResult(count, list)); } 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 a928e7a..7c5d528 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 @@ -14,6 +14,7 @@ import annotation.VerifyParam; import api.PageResult; import api.Result; import api.StatusCode; +import constants.Constants; import dto.UserLoginDto; import enums.CollectTypeEnum; import enums.VerifyRegexEnum; @@ -26,10 +27,12 @@ import util.JwtUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.PageRequest; +import org.springframework.data.redis.core.RedisTemplate; import org.springframework.util.CollectionUtils; import org.springframework.web.bind.annotation.*; import java.util.*; +import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; @RestController @@ -43,6 +46,8 @@ public class SongController { private JournalSongService journalSongService; @Autowired private JournalService journalService; + @Autowired + private RedisTemplate redisTemplate; //@Autowired //private SongInfoService songInfoService; @Autowired @@ -140,12 +145,13 @@ public class SongController { if (objectIds.isEmpty()) { return Result.success(new PageResult(0L, Collections.emptyList())); } + Long count = (long) getSongCollectSet(queryUserId).size(); List songs = journalSongService.orderByField(objectIds); Set songCollectSet = getMyCollectSet(loginUserId,queryUserId,objectIds,isJouranlSource); List results = songs.stream().map(s -> SongMapper.getSongRespDTO(s, songCollectSet)) .collect(Collectors.toList()); // results = results.stream().map(songRespDTO -> randomCDN(songRespDTO)).collect(Collectors.toList()); - return Result.success(new PageResult(Long.valueOf(results.size()), results)); + return Result.success(new PageResult(count, results)); } private Set getMyCollectSet(String loginUser, String checkUser, List objectIds, boolean isJouranlSource) { @@ -160,12 +166,20 @@ public class SongController { } private List getSongIds(CollectQueryReq queryReq) { if(null==queryReq.getPageNum()||queryReq.getPageNum()<1||null==queryReq.getPageSize()||queryReq.getPageSize()<1) { - return userCollectInfoService.findByUserIdAndCollectType(queryReq.getUserId(), CollectTypeEnum.SONG); + return new ArrayList<>(getSongCollectSet(queryReq.getUserId())); } PageRequest pageRequest = PageRequest.of(queryReq.getPageNum()-1, queryReq.getPageSize()); return userCollectInfoService.findByUserIdAndCollectType(queryReq.getUserId(), CollectTypeEnum.SONG, pageRequest); } + private Set getSongCollectSet(String userId) { + Set set=(Set) redisTemplate.opsForValue().get(Constants.REDIS_KEY_USER_COLLECT_SONG+userId); + if(null==set) { + set=userCollectInfoService.getCollectSet(userId, CollectTypeEnum.SONG); + redisTemplate.opsForValue().set(Constants.REDIS_KEY_USER_COLLECT_SONG+userId,set,6,TimeUnit.HOURS); + } + return set; + } @ApiOperation(value = "3.随机播放歌曲", notes = "雀乐FM") @ApiImplicitParams({ @ApiImplicitParam(name = "limit", value = "随机歌曲数,最少1首,最多30首", required = false) }) @GetMapping("/random/{limit}") diff --git a/luoo_music/src/main/java/com/luoo/music/dao/JournalDao.java b/luoo_music/src/main/java/com/luoo/music/dao/JournalDao.java index c72de8c..97d1388 100644 --- a/luoo_music/src/main/java/com/luoo/music/dao/JournalDao.java +++ b/luoo_music/src/main/java/com/luoo/music/dao/JournalDao.java @@ -89,6 +89,9 @@ public interface JournalDao extends JpaRepository,JpaSpecificati @Query(value = "select * from tb_journal where match(journal_no, title) against(?1 IN BOOLEAN MODE) and state='1' and is_publish='1' limit ?2,?3 ", nativeQuery = true) List fuzzySearch(String keyword, int offset, int limit); + @Query(value = "select count(*) from tb_journal where match(journal_no, title) against(?1 IN BOOLEAN MODE) and state='1' and is_publish='1'", nativeQuery = true) + Long fuzzySearchCount(String keyword); + @Query(value = "select journal_no FROM tb_journal where id=?1", nativeQuery = true) String findJournalNoById(String journalId); diff --git a/luoo_music/src/main/java/com/luoo/music/dao/JournalSongDao.java b/luoo_music/src/main/java/com/luoo/music/dao/JournalSongDao.java index 03f6618..1da1b64 100644 --- a/luoo_music/src/main/java/com/luoo/music/dao/JournalSongDao.java +++ b/luoo_music/src/main/java/com/luoo/music/dao/JournalSongDao.java @@ -60,6 +60,9 @@ public interface JournalSongDao extends JpaRepository, JpaSp @Query(value = "select * from tb_journal_song where match(name, artist, album) against(?1 IN BOOLEAN MODE) limit ?2,?3 ", nativeQuery = true) List fuzzySearch(String keyword,int offset, int limit); + @Query(value = "select count(*) from tb_journal_song where match(name, artist, album) against(?1 IN BOOLEAN MODE)", nativeQuery = true) + Long fuzzySearchCount(String keyword); + @Query(value = "select * from tb_journal_song tjs where journal_no = (select journal_no from tb_journal where id=?1) order by song_no", nativeQuery = true) List findByJournalId(String journalId); } diff --git a/luoo_music/src/main/java/com/luoo/music/dto/request/JournalQueryReq.java b/luoo_music/src/main/java/com/luoo/music/dto/request/JournalQueryReq.java index 6c18802..1032948 100644 --- a/luoo_music/src/main/java/com/luoo/music/dto/request/JournalQueryReq.java +++ b/luoo_music/src/main/java/com/luoo/music/dto/request/JournalQueryReq.java @@ -27,9 +27,12 @@ public class JournalQueryReq implements Serializable { @Override public String toString() { - return pageNum + "_" + pageSize + "_" + getString(categoryId) + "_" + getString(journalNoRange); + return pageNum + "_" + pageSize + "_" + countString(); } + public String countString() { + return getString(categoryId) + "_" + getString(journalNoRange); + } private String getString(String value) { return StringTools.isEmpty(value) ? null : 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 fa8ca88..10d71d7 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 @@ -434,4 +434,15 @@ public class JournalService { public List recommend() { return journalDao.recommend(); } + + public Long countJournal(JournalQueryReq queryReq) { + return Long.valueOf(this.cacheChannel.get(Constants.J2CACHE_REGION_JOURNAL_QUERY_COUNT, queryReq.countString(), key -> getJournalCount(queryReq) , false).getValue().toString()); + } + + private Long getJournalCount(JournalQueryReq queryReq) { + List ids=getJournalIds(queryReq); + Pair journalNoRange=getJournalNoRange(queryReq.getJournalNoRange()); + Specification specification = createJournalSpecification(ids,journalNoRange); + return journalDao.count(specification); + } } 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 98396d3..c0644a9 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 @@ -166,6 +166,4 @@ public class JournalSongService { } }; } - - } 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 5a2e2ea..f5e6617 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 @@ -3,12 +3,14 @@ package com.luoo.music.service; import enums.CollectTypeEnum; import java.util.Collections; +import java.util.LinkedHashSet; import java.util.List; import java.util.Set; import java.util.stream.Collectors; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Query; @@ -42,10 +44,11 @@ public class UserCollectInfoService { public Set getCollectSet(String userId, CollectTypeEnum collectTypeEnum) { Criteria criteria = Criteria.where("userId").is(userId).and("collectType").is(collectTypeEnum.getType()); - Query query = new Query(criteria); + Sort createTime = Sort.by(Sort.Direction.DESC, "createTime"); + Query query = new Query(criteria).with(createTime); List userCollectInfos = mongoTemplate.find(query, UserCollectInfo.class); return userCollectInfos.isEmpty() ? Collections.emptySet() - : userCollectInfos.stream().map(UserCollectInfo::getObjectId).collect(Collectors.toSet()); + : userCollectInfos.stream().map(UserCollectInfo::getObjectId).collect(Collectors.toCollection(LinkedHashSet::new)); } public boolean isCollect(String userId, String objectId, CollectTypeEnum collectTypeEnum) { diff --git a/luoo_music/src/main/resources/bootstrap.yml b/luoo_music/src/main/resources/bootstrap.yml index d0cd052..9d82c57 100644 --- a/luoo_music/src/main/resources/bootstrap.yml +++ b/luoo_music/src/main/resources/bootstrap.yml @@ -37,6 +37,7 @@ caffeine: region: default: 100000, 365d journal_query_page: 10000, 365d + journal_query_count: 10000, 365d journal_id: 2000, 365d journal_no: 2000, 365d journal_song_key: 10000, 365d 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 62fe844..a9e7c7b 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 @@ -17,6 +17,7 @@ import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Query; @@ -44,10 +45,11 @@ public class UserCollectInfoService { public Set getCollectSet(String userId, CollectTypeEnum collectTypeEnum) { Criteria criteria = Criteria.where("userId").is(userId).and("collectType").is(collectTypeEnum.getType()); - Query query = new Query(criteria); + Sort createTime = Sort.by(Sort.Direction.DESC, "createTime"); + Query query = new Query(criteria).with(createTime); List userCollectInfos = mongoTemplate.find(query, UserCollectInfo.class); return userCollectInfos.isEmpty() ? Collections.emptySet() - : userCollectInfos.stream().map(UserCollectInfo::getObjectId).collect(Collectors.toSet()); + : userCollectInfos.stream().map(UserCollectInfo::getObjectId).collect(Collectors.toCollection(LinkedHashSet::new)); } public void saveCollect(String userId, String objectId, Integer collectType) { @@ -310,6 +312,9 @@ public class UserCollectInfoService { private void addSongCount(UserInfo user) { user.setSongCount(user.getSongCount() + 1); userInfoDao.save(user); + // 更新redis 中的缓存 + redisTemplate.opsForValue().set(Constants.REDIS_KEY_USER_COLLECT_SONG + user.getId(), + getCollectSet(user.getId(), CollectTypeEnum.SONG),6,TimeUnit.HOURS); } private void reduceJournalCount(UserInfo user, String objectId) { @@ -324,6 +329,9 @@ public class UserCollectInfoService { private void reduceSongCount(UserInfo user) { user.setSongCount(user.getSongCount() - 1); userInfoDao.save(user); + // 更新redis 中的缓存 + redisTemplate.opsForValue().set(Constants.REDIS_KEY_USER_COLLECT_SONG + user.getId(), + getCollectSet(user.getId(), CollectTypeEnum.SONG),6,TimeUnit.HOURS); } public UserRelationEnum getRelation(String userId, String otherId) {