From baaa64c3ad331f33f45823f97a81033ea11e7b60 Mon Sep 17 00:00:00 2001 From: Gary Date: Sat, 20 Jan 2024 20:02:39 +0800 Subject: [PATCH] 1optimise collect operation --- .../music/controller/JournalController.java | 71 ++++++++++++------- .../luoo/music/controller/SongController.java | 33 ++++++--- .../luoo/user/service/UserCollectService.java | 11 ++- 3 files changed, 80 insertions(+), 35 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 388cf54..4dd16d7 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,7 +1,14 @@ package com.luoo.music.controller; + +import java.util.Collections; import java.util.Date; +import java.util.HashSet; import java.util.List; +import java.util.Map; +import java.util.Set; import java.util.stream.Collectors; +import java.util.stream.Stream; +import java.util.stream.StreamSupport; import com.luoo.music.dao.UserCollectDao; import com.luoo.music.dto.request.JournalQueryReq; @@ -28,6 +35,7 @@ import util.JwtUtil; /** * 控制器层 + * * @author Administrator * */ @@ -41,46 +49,61 @@ public class JournalController { private ArticleService articleService; @Autowired private JwtUtil jwtUtil; - @ApiOperation(value = "1.查询期刊信息", notes="若token为空或token校验失败,默认返回最新的10期,期刊筛选条件对游客不可用") + + @ApiOperation(value = "1.查询期刊信息", notes = "若token为空或token校验失败,默认返回最新的10期,期刊筛选条件对游客不可用") @GetMapping("/list") @GlobalInterceptor - public Result> page(@RequestHeader(value = "token", required = false) String token, - @VerifyParam JournalQueryReq queryReq){ - UserLoginDto user=jwtUtil.getUserLoginDto(token); - if(null==user) { + public Result> page(@RequestHeader(value = "token", required = false) String token, + @VerifyParam JournalQueryReq queryReq) { + UserLoginDto user = jwtUtil.getUserLoginDto(token); + if (null == user) { queryReq.setLanguage(null); queryReq.setStyle(null); queryReq.setPageNum(1); queryReq.setPageSize(10); } Page
pageList = articleService.queryPage(queryReq); - List list=pageList.stream().map(a->getArticleRespDTO(a,user)).collect(Collectors.toList()); - return Result.success(new PageResult(Long.valueOf(list.size()), list) ); + List collectIds = getCollectIds(pageList, user, CollectTypeEnum.JOURNAL.getType()); + Set journalCollectSet = getCollectSet(collectIds); + List list = pageList.stream().map(a -> getArticleRespDTO(a, journalCollectSet)) + .collect(Collectors.toList()); + return Result.success(new PageResult(Long.valueOf(list.size()), list)); } - private JournalRespDTO getArticleRespDTO(Article article, UserLoginDto userLoginDto) { - //StopWatch sw=new StopWatch(); - //sw.start("stage1"); - JournalRespDTO journalRespDTO=new JournalRespDTO(); + + private Set getCollectSet(List collectIds) { + if (collectIds.isEmpty()) { + return Collections.emptySet(); + } + Set set = new HashSet<>(); + userCollectDao.findAllById(collectIds).forEach(u -> set.add(u.getObjectId())); + return set; + } + + private List getCollectIds(Page
pageList, UserLoginDto user, Integer type) { + if (null == user) { + return Collections.emptyList(); + } + return pageList.stream().map(a -> getCollectId(user.getUserId(), a.getId(), type)).collect(Collectors.toList()); + } + + private String getCollectId(String userId, String objectId, Integer type) { + return userId + "_" + objectId + "_" + type; + } + + private JournalRespDTO getArticleRespDTO(Article article, Set journalCollectSet) { + JournalRespDTO journalRespDTO = new JournalRespDTO(); journalRespDTO.setId(article.getId()); journalRespDTO.setJournalNo(article.getVolid()); journalRespDTO.setTitle(article.getTitle()); - journalRespDTO.setImage(Constants.MUSIC_RESOURCE_PREFIX+article.getImage()); + journalRespDTO.setImage(Constants.MUSIC_RESOURCE_PREFIX + article.getImage()); journalRespDTO.setDate(getEditDate(article)); - //sw.stop(); - //System.out.println(sw.prettyPrint()); - //sw.start("stage2"); - //根据userId查询是否已收藏 - //if(null!=userLoginDto) { - // long userCollectCount=userCollectDao.countByUserIdAndObjectIdAndCollectType(userLoginDto.getUserId(), journalRespDTO.getId(), CollectTypeEnum.JOURNAL.getType()); - // journalRespDTO.setHaveCollect(userCollectCount>0); - //} - //sw.stop(); - //System.out.println(sw.prettyPrint()); + journalRespDTO.setHaveCollect(journalCollectSet.contains(journalRespDTO.getId())); return journalRespDTO; } + private String getEditDate(Article article) { - Date date=null==article.getUpdatetime()?article.getCreatetime():article.getUpdatetime(); + Date date = null == article.getUpdatetime() ? article.getCreatetime() : article.getUpdatetime(); return DateUtil.format(date, DateTimePatternEnum.YYYY_DOT_MM_DOT_DD.getPattern()); } - + } 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 fb3bd04..74dcb1d 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 @@ -25,6 +25,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.web.bind.annotation.*; +import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Map; @@ -64,7 +65,9 @@ public class SongController { return Result.unauthorized(null); } List songs = songService.findByVolid(journalNo); - List results = songs.stream().map(s -> getSongRespDTO(s, user)).collect(Collectors.toList()); + List collectIds = getCollectIds(songs, user, CollectTypeEnum.SONG.getType()); + Set songCollectSet = getCollectSet(collectIds); + List results = songs.stream().map(s -> getSongRespDTO(s, songCollectSet)).collect(Collectors.toList()); return Result.success(results); } @@ -79,11 +82,11 @@ public class SongController { public Result> random( @PathVariable @VerifyParam(required = true, regex = VerifyRegexEnum.RANDOM_SONG_LIMIT) Integer limit) { List songs = songService.random(limit); - List results = songs.stream().map(s -> getSongRespDTO(s, null)).collect(Collectors.toList()); + List results = songs.stream().map(s -> getSongRespDTO(s, Collections.emptySet())).collect(Collectors.toList()); return Result.success(results); } - private SongRespDTO getSongRespDTO(Song song, UserLoginDto userLoginDto) { + private SongRespDTO getSongRespDTO(Song song, Set songCollectSet) { SongRespDTO songRespDTO = new SongRespDTO(); songRespDTO.setJournalNo(song.getVolid()); songRespDTO.setAlbum(song.getAlbum()); @@ -94,14 +97,28 @@ public class SongController { songRespDTO.setPic(Constants.MUSIC_RESOURCE_PREFIX + song.getUrl().replace(".mp3", ".jpg")); songRespDTO.setLrc(Constants.MUSIC_RESOURCE_PREFIX + song.getUrl().replace(".mp3", ".lyric")); songRespDTO.setSongNo(song.getSongno()); - // 根据userId查询是否已收藏 - //if (null != userLoginDto) { - // long userCollectCount=userCollectDao.countByUserIdAndObjectIdAndCollectType(userLoginDto.getUserId(), songRespDTO.getId(), CollectTypeEnum.SONG.getType()); - // songRespDTO.setHaveCollect(userCollectCount>0); - //} + songRespDTO.setHaveCollect(songCollectSet.contains(songRespDTO.getId())); return songRespDTO; } + private Set getCollectSet(List collectIds) { + if (collectIds.isEmpty()) { + return Collections.emptySet(); + } + Set set = new HashSet<>(); + userCollectDao.findAllById(collectIds).forEach(u -> set.add(u.getObjectId())); + return set; + } + + private List getCollectIds(List list, UserLoginDto user, Integer type) { + if (null == user) { + return Collections.emptyList(); + } + return list.stream().map(a -> getCollectId(user.getUserId(), a.getId(), type)).collect(Collectors.toList()); + } + private String getCollectId(String userId, String objectId, Integer type) { + return userId + "_" + objectId + "_" + type; + } /** * 根据ID查询 * diff --git a/luoo_user/src/main/java/com/luoo/user/service/UserCollectService.java b/luoo_user/src/main/java/com/luoo/user/service/UserCollectService.java index f5b2579..9a24388 100644 --- a/luoo_user/src/main/java/com/luoo/user/service/UserCollectService.java +++ b/luoo_user/src/main/java/com/luoo/user/service/UserCollectService.java @@ -1,6 +1,7 @@ package com.luoo.user.service; import java.util.Date; +import java.util.Optional; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -22,11 +23,13 @@ public class UserCollectService { if (null == collectTypeEnum) { throw new BizException(StatusCode.VALIDATE_FAILED); } - UserCollect dbCollect = userCollectDao.findByUserIdAndObjectIdAndCollectType(userId, objectId, collectType); - if (null!=dbCollect) { + String collectId=userId+"_"+objectId+"_"+collectType; + Optional dbCollect = userCollectDao.findById(collectId);//.findByUserIdAndObjectIdAndCollectType(userId, objectId, collectType); + if (dbCollect.isPresent()) { return; } UserCollect userCollect = new UserCollect(); + userCollect.setCollectId(collectId); userCollect.setUserId(userId); userCollect.setCollectTime(new Date()); userCollect.setObjectId(objectId); @@ -36,6 +39,8 @@ public class UserCollectService { public void deleteUserCollectByUserIdAndObjectIdAndCollectType(String userId, String objectId, Integer collectType) { - userCollectDao.deleteByUserIdAndObjectIdAndCollectType(userId, objectId, collectType); + String collectId=userId+"_"+objectId+"_"+collectType; + userCollectDao.deleteById(collectId); + //userCollectDao.deleteByUserIdAndObjectIdAndCollectType(userId, objectId, collectType); } }