1.add total in page result

main
Gary 9 months ago
parent 33e36c43c2
commit 9cf043ace8

@ -35,6 +35,7 @@ caffeine:
region: region:
default: 100000, 365d default: 100000, 365d
journal_query_page: 10000, 365d journal_query_page: 10000, 365d
journal_query_count: 10000, 365d
journal_id: 2000, 365d journal_id: 2000, 365d
journal_no: 2000, 365d journal_no: 2000, 365d
journal_song_key: 10000, 365d journal_song_key: 10000, 365d

@ -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_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_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_ID = "journal_id";
public static final String J2CACHE_REGION_JOURNAL_NO = "journal_no"; public static final String J2CACHE_REGION_JOURNAL_NO = "journal_no";
public static final String J2CACHE_REGION_JOURNAL_NO_SONG_ID = "journal_song_key"; public static final String J2CACHE_REGION_JOURNAL_NO_SONG_ID = "journal_song_key";

@ -52,6 +52,7 @@ public class CMSJournalController {
public Result add(@ApiParam(value = "期刊信息", required = true) @Valid @RequestBody JournalAddModel addModel){ public Result add(@ApiParam(value = "期刊信息", required = true) @Valid @RequestBody JournalAddModel addModel){
cacheChannel.evict("default", "journal_filter"); cacheChannel.evict("default", "journal_filter");
cacheChannel.clear(Constants.J2CACHE_REGION_JOURNAL_QUERY_PAGE); cacheChannel.clear(Constants.J2CACHE_REGION_JOURNAL_QUERY_PAGE);
cacheChannel.clear(Constants.J2CACHE_REGION_JOURNAL_QUERY_COUNT);
return journalService.add(addModel); return journalService.add(addModel);
} }
@ -80,6 +81,7 @@ public class CMSJournalController {
private void cleanCache(String id) { private void cleanCache(String id) {
cacheChannel.evict("default", "journal_filter"); cacheChannel.evict("default", "journal_filter");
cacheChannel.clear(Constants.J2CACHE_REGION_JOURNAL_QUERY_PAGE); 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); cacheChannel.evict(constants.Constants.J2CACHE_REGION_JOURNAL_ID, id);
String journalNo=journalService.findJournalNoById(id); String journalNo=journalService.findJournalNoById(id);
cacheChannel.evict(constants.Constants.J2CACHE_REGION_JOURNAL_SONG_LIST, journalNo); cacheChannel.evict(constants.Constants.J2CACHE_REGION_JOURNAL_SONG_LIST, journalNo);

@ -77,11 +77,12 @@ public class JournalController {
@VerifyParam JournalQueryReq queryReq) { @VerifyParam JournalQueryReq queryReq) {
UserLoginDto user = jwtUtil.getUserLoginDto(authorization); UserLoginDto user = jwtUtil.getUserLoginDto(authorization);
updateQuery(user,queryReq); updateQuery(user,queryReq);
Long count = journalService.countJournal(queryReq);
List<JournalRespDTO> pageList=journalService.queryJournalByPage(queryReq); List<JournalRespDTO> pageList=journalService.queryJournalByPage(queryReq);
// pageList = pageList.stream().map(journalRespDTO -> randomCDN(journalRespDTO)).collect(Collectors.toList()); // pageList = pageList.stream().map(journalRespDTO -> randomCDN(journalRespDTO)).collect(Collectors.toList());
Set<String> journalCollectSet = getJournalCollectSet(user); Set<String> journalCollectSet = getJournalCollectSet(user);
updateCollectStatus(pageList,journalCollectSet); updateCollectStatus(pageList,journalCollectSet);
return Result.success(new PageResult<JournalRespDTO>(Long.valueOf(pageList.size()), pageList)); return Result.success(new PageResult<JournalRespDTO>(count, pageList));
} }
private void updateCollectStatus(List<JournalRespDTO> pageList, Set<String> journalCollectSet) { private void updateCollectStatus(List<JournalRespDTO> pageList, Set<String> journalCollectSet) {
@ -119,13 +120,14 @@ public class JournalController {
if (objectIds.isEmpty()) { if (objectIds.isEmpty()) {
return Result.success(new PageResult<JournalRespDTO>(0L, Collections.emptyList())); return Result.success(new PageResult<JournalRespDTO>(0L, Collections.emptyList()));
} }
Long count = Long.valueOf(getJournalCollectSet(queryReq.getUserId()).size());
List<Journal> pageList = journalService.orderByField(objectIds); List<Journal> pageList = journalService.orderByField(objectIds);
UserLoginDto user = jwtUtil.getUserLoginDto(authorization); UserLoginDto user = jwtUtil.getUserLoginDto(authorization);
Set<String> journalCollectSet = getMyCollectSet(user.getUserId(),queryReq.getUserId(),objectIds); Set<String> journalCollectSet = getMyCollectSet(user.getUserId(),queryReq.getUserId(),objectIds);
List<JournalRespDTO> list = pageList.stream().map(a -> JournalMapper.getJournalRespDTO(a, journalCollectSet)) List<JournalRespDTO> list = pageList.stream().map(a -> JournalMapper.getJournalRespDTO(a, journalCollectSet))
.collect(Collectors.toList()); .collect(Collectors.toList());
// list = list.stream().map(journalRespDTO -> randomCDN(journalRespDTO)).collect(Collectors.toList()); // list = list.stream().map(journalRespDTO -> randomCDN(journalRespDTO)).collect(Collectors.toList());
return Result.success(new PageResult<JournalRespDTO>(Long.valueOf(list.size()), list)); return Result.success(new PageResult<JournalRespDTO>(count, list));
} }

@ -14,6 +14,7 @@ import annotation.VerifyParam;
import api.PageResult; import api.PageResult;
import api.Result; import api.Result;
import api.StatusCode; import api.StatusCode;
import constants.Constants;
import dto.UserLoginDto; import dto.UserLoginDto;
import enums.CollectTypeEnum; import enums.CollectTypeEnum;
import enums.VerifyRegexEnum; import enums.VerifyRegexEnum;
@ -26,10 +27,12 @@ import util.JwtUtil;
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.data.redis.core.RedisTemplate;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.*; import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@RestController @RestController
@ -43,6 +46,8 @@ public class SongController {
private JournalSongService journalSongService; private JournalSongService journalSongService;
@Autowired @Autowired
private JournalService journalService; private JournalService journalService;
@Autowired
private RedisTemplate redisTemplate;
//@Autowired //@Autowired
//private SongInfoService songInfoService; //private SongInfoService songInfoService;
@Autowired @Autowired
@ -140,12 +145,13 @@ public class SongController {
if (objectIds.isEmpty()) { if (objectIds.isEmpty()) {
return Result.success(new PageResult<SongRespDTO>(0L, Collections.emptyList())); return Result.success(new PageResult<SongRespDTO>(0L, Collections.emptyList()));
} }
Long count = (long) getSongCollectSet(queryUserId).size();
List<JournalSong> songs = journalSongService.orderByField(objectIds); List<JournalSong> songs = journalSongService.orderByField(objectIds);
Set<String> songCollectSet = getMyCollectSet(loginUserId,queryUserId,objectIds,isJouranlSource); Set<String> songCollectSet = getMyCollectSet(loginUserId,queryUserId,objectIds,isJouranlSource);
List<SongRespDTO> results = songs.stream().map(s -> SongMapper.getSongRespDTO(s, songCollectSet)) List<SongRespDTO> results = songs.stream().map(s -> SongMapper.getSongRespDTO(s, songCollectSet))
.collect(Collectors.toList()); .collect(Collectors.toList());
// results = results.stream().map(songRespDTO -> randomCDN(songRespDTO)).collect(Collectors.toList()); // results = results.stream().map(songRespDTO -> randomCDN(songRespDTO)).collect(Collectors.toList());
return Result.success(new PageResult<SongRespDTO>(Long.valueOf(results.size()), results)); return Result.success(new PageResult<SongRespDTO>(count, results));
} }
private Set<String> getMyCollectSet(String loginUser, String checkUser, List<String> objectIds, boolean isJouranlSource) { private Set<String> getMyCollectSet(String loginUser, String checkUser, List<String> objectIds, boolean isJouranlSource) {
@ -160,12 +166,20 @@ public class SongController {
} }
private List<String> getSongIds(CollectQueryReq queryReq) { private List<String> getSongIds(CollectQueryReq queryReq) {
if(null==queryReq.getPageNum()||queryReq.getPageNum()<1||null==queryReq.getPageSize()||queryReq.getPageSize()<1) { 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()); PageRequest pageRequest = PageRequest.of(queryReq.getPageNum()-1, queryReq.getPageSize());
return userCollectInfoService.findByUserIdAndCollectType(queryReq.getUserId(), CollectTypeEnum.SONG, pageRequest); return userCollectInfoService.findByUserIdAndCollectType(queryReq.getUserId(), CollectTypeEnum.SONG, pageRequest);
} }
private Set<String> getSongCollectSet(String userId) {
Set<String> set=(Set<String>) 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") @ApiOperation(value = "3.随机播放歌曲", notes = "雀乐FM")
@ApiImplicitParams({ @ApiImplicitParam(name = "limit", value = "随机歌曲数最少1首最多30首", required = false) }) @ApiImplicitParams({ @ApiImplicitParam(name = "limit", value = "随机歌曲数最少1首最多30首", required = false) })
@GetMapping("/random/{limit}") @GetMapping("/random/{limit}")

@ -89,6 +89,9 @@ public interface JournalDao extends JpaRepository<Journal,String>,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) @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<Journal> fuzzySearch(String keyword, int offset, int limit); List<Journal> 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) @Query(value = "select journal_no FROM tb_journal where id=?1", nativeQuery = true)
String findJournalNoById(String journalId); String findJournalNoById(String journalId);

@ -60,6 +60,9 @@ public interface JournalSongDao extends JpaRepository<JournalSong,String>, JpaSp
@Query(value = "select * from tb_journal_song where match(name, artist, album) against(?1 IN BOOLEAN MODE) limit ?2,?3 ", nativeQuery = true) @Query(value = "select * from tb_journal_song where match(name, artist, album) against(?1 IN BOOLEAN MODE) limit ?2,?3 ", nativeQuery = true)
List<JournalSong> fuzzySearch(String keyword,int offset, int limit); List<JournalSong> 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) @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<JournalSong> findByJournalId(String journalId); List<JournalSong> findByJournalId(String journalId);
} }

@ -27,9 +27,12 @@ public class JournalQueryReq implements Serializable {
@Override @Override
public String toString() { 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) { private String getString(String value) {
return StringTools.isEmpty(value) ? null : value; return StringTools.isEmpty(value) ? null : value;
} }

@ -434,4 +434,15 @@ public class JournalService {
public List<Journal> recommend() { public List<Journal> recommend() {
return journalDao.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<String> ids=getJournalIds(queryReq);
Pair<Integer,Integer> journalNoRange=getJournalNoRange(queryReq.getJournalNoRange());
Specification<Journal> specification = createJournalSpecification(ids,journalNoRange);
return journalDao.count(specification);
}
} }

@ -166,6 +166,4 @@ public class JournalSongService {
} }
}; };
} }
} }

@ -3,12 +3,14 @@ package com.luoo.music.service;
import enums.CollectTypeEnum; import enums.CollectTypeEnum;
import java.util.Collections; import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable; 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.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.mongodb.core.query.Query;
@ -42,10 +44,11 @@ public class UserCollectInfoService {
public Set<String> getCollectSet(String userId, CollectTypeEnum collectTypeEnum) { public Set<String> getCollectSet(String userId, CollectTypeEnum collectTypeEnum) {
Criteria criteria = Criteria.where("userId").is(userId).and("collectType").is(collectTypeEnum.getType()); 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<UserCollectInfo> userCollectInfos = mongoTemplate.find(query, UserCollectInfo.class); List<UserCollectInfo> userCollectInfos = mongoTemplate.find(query, UserCollectInfo.class);
return userCollectInfos.isEmpty() ? Collections.emptySet() 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) { public boolean isCollect(String userId, String objectId, CollectTypeEnum collectTypeEnum) {

@ -37,6 +37,7 @@ caffeine:
region: region:
default: 100000, 365d default: 100000, 365d
journal_query_page: 10000, 365d journal_query_page: 10000, 365d
journal_query_count: 10000, 365d
journal_id: 2000, 365d journal_id: 2000, 365d
journal_no: 2000, 365d journal_no: 2000, 365d
journal_song_key: 10000, 365d journal_song_key: 10000, 365d

@ -17,6 +17,7 @@ import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable; 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.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.mongodb.core.query.Query;
@ -44,10 +45,11 @@ public class UserCollectInfoService {
public Set<String> getCollectSet(String userId, CollectTypeEnum collectTypeEnum) { public Set<String> getCollectSet(String userId, CollectTypeEnum collectTypeEnum) {
Criteria criteria = Criteria.where("userId").is(userId).and("collectType").is(collectTypeEnum.getType()); 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<UserCollectInfo> userCollectInfos = mongoTemplate.find(query, UserCollectInfo.class); List<UserCollectInfo> userCollectInfos = mongoTemplate.find(query, UserCollectInfo.class);
return userCollectInfos.isEmpty() ? Collections.emptySet() 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) { public void saveCollect(String userId, String objectId, Integer collectType) {
@ -310,6 +312,9 @@ public class UserCollectInfoService {
private void addSongCount(UserInfo user) { private void addSongCount(UserInfo user) {
user.setSongCount(user.getSongCount() + 1); user.setSongCount(user.getSongCount() + 1);
userInfoDao.save(user); 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) { private void reduceJournalCount(UserInfo user, String objectId) {
@ -324,6 +329,9 @@ public class UserCollectInfoService {
private void reduceSongCount(UserInfo user) { private void reduceSongCount(UserInfo user) {
user.setSongCount(user.getSongCount() - 1); user.setSongCount(user.getSongCount() - 1);
userInfoDao.save(user); 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) { public UserRelationEnum getRelation(String userId, String otherId) {

Loading…
Cancel
Save