1.promote jouranl song search performance

main
Gary 11 months ago
parent 7217d20389
commit 77fc490c6c

@ -13,6 +13,7 @@ public class Constants {
public static final String J2CACHE_REGION_JOURNAL_QUERY_PAGE = "journal_query_page";
public static final String J2CACHE_REGION_JOURNAL_ID = "journal_id";
public static final String J2CACHE_REGION_JOURNAL_SONG_KEY = "journal_song_key";
public static final String J2CACHE_REGION_JOURNAL_SONG_LIST = "journal_song_list";
public static final String J2CACHE_REGION_JOURNAL_COMMENT_PAGE_HOT = "journal_comment_page_hot";

@ -91,10 +91,16 @@ public class JournalController {
updateQuery(user,queryReq);
List<JournalRespDTO> pageList=journalService.queryJournalByPage(queryReq);
Set<String> journalCollectSet = getJournalCollectSet(user);
List<JournalRespDTO> list = pageList.stream().map(j->updateCollectStatus(j,journalCollectSet)).collect(Collectors.toList());
return Result.success(new PageResult<JournalRespDTO>(Long.valueOf(list.size()), list));
updateCollectStatus(pageList,journalCollectSet);
return Result.success(new PageResult<JournalRespDTO>(Long.valueOf(pageList.size()), pageList));
}
private void updateCollectStatus(List<JournalRespDTO> pageList, Set<String> journalCollectSet) {
pageList.forEach(j->{
j.setHaveCollect(journalCollectSet.contains(j.getId()));
});
}
private Set<String> getJournalCollectSet(UserLoginDto user) {
return null == user ? Collections.emptySet():getJournalCollectSet(user.getUserId());
}
@ -105,11 +111,6 @@ public class JournalController {
queryReq.setPageSize(10);
}
}
private JournalRespDTO updateCollectStatus(JournalRespDTO journalRespDTO,Set<String> journalCollectSet) {
journalRespDTO.setHaveCollect(journalCollectSet.contains(journalRespDTO.getId()));
return journalRespDTO;
}
private Set<String> getJournalCollectSet(String userId) {
Set<String> set=(Set<String>) redisTemplate.opsForValue().get(Constants.REDIS_KEY_USER_COLLECT_JOURNAL+userId);
if(null==set) {

@ -70,15 +70,25 @@ public class SongController {
if (null == user && !isLatest10(journalNo)) {
return Result.unauthorized(null);
}
List<JournalSong> songs = journalSongService.findByVolid(journalNo);
List<String> ids=songs.stream().map(JournalSong::getJournalNoSongId).collect(Collectors.toList());
Set<String> songCollectSet = null == user ? Collections.emptySet()
: userCollectInfoService.getCollectSet(user.getUserId(),ids, CollectTypeEnum.SONG);
List<SongRespDTO> results = songs.stream().map(s -> SongMapper.getSongRespDTO(s, songCollectSet))
.collect(Collectors.toList());
List<SongRespDTO> results = journalSongService.findByJournalNo(journalNo);
Set<String> songCollectSet = getSongCollectSet(results,user);
updateCollectStatus(results,songCollectSet);
return Result.success(results);
}
private void updateCollectStatus(List<SongRespDTO> results, Set<String> songCollectSet) {
results.forEach(s->{
s.setHaveCollect(songCollectSet.contains(s.getId()));
});
}
private Set<String> getSongCollectSet(List<SongRespDTO> results, UserLoginDto user) {
List<String> ids=results.stream().map(SongRespDTO::getId).collect(Collectors.toList());
return null == user ? Collections.emptySet()
: userCollectInfoService.getCollectSet(user.getUserId(),ids, CollectTypeEnum.SONG);
}
private boolean isLatest10(String journalNo) {
return journalService.isLatest10ByJournalNo(journalNo);
}

@ -1,12 +1,15 @@
package com.luoo.music.dto.response;
import java.io.Serializable;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class SongRespDTO {
public class SongRespDTO implements Serializable {
private static final long serialVersionUID = 1L;
private String id;
@ApiModelProperty(value = "歌曲名")
private String title;

@ -1,12 +1,15 @@
package com.luoo.music.service;
import com.luoo.music.dao.JournalSongDao;
import com.luoo.music.dto.mapper.SongMapper;
import com.luoo.music.dto.response.SearchCategoryDTO;
import com.luoo.music.dto.response.SongRespDTO;
import com.luoo.music.pojo.Journal;
import com.luoo.music.pojo.JournalSong;
import com.luoo.music.pojo.SongInfo;
import constants.Constants;
import enums.CollectTypeEnum;
import net.oschina.j2cache.CacheChannel;
import org.springframework.beans.factory.annotation.Autowired;
@ -27,6 +30,8 @@ import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
@Service
public class JournalSongService {
@ -128,4 +133,16 @@ public class JournalSongService {
public JournalSong findByJournalNoSongId(String id) {
return (JournalSong) this.cacheChannel.get(Constants.J2CACHE_REGION_JOURNAL_QUERY_PAGE, id, key -> journalSongDao.findByJournalNoSongId(id) , true).getValue() ;
}
public List<SongRespDTO> findByJournalNo(String journalNo) {
return (List<SongRespDTO>) this.cacheChannel.get(Constants.J2CACHE_REGION_JOURNAL_SONG_LIST, journalNo, key -> getSongRespDTO(journalNo) , true).getValue() ;
}
private List<SongRespDTO> getSongRespDTO(String journalNo) {
List<JournalSong> songs = findByVolid(journalNo);
List<String> ids=songs.stream().map(JournalSong::getJournalNoSongId).collect(Collectors.toList());
return songs.stream().map(s -> SongMapper.getSongRespDTO(s))
.collect(Collectors.toList());
}
}

@ -1,4 +1,5 @@
default: 1000, 2h
journal_query_page: 10000, 6h
journal_id: 2000, 6h
journal_song_key: 10000, 6h
journal_song_key: 10000, 6h
journal_song_list: 2000, 6h

@ -1,114 +0,0 @@
package com.luoo.user.controller;
import api.Result;
import api.StatusCode;
import com.luoo.user.pojo.UserInfo;
import com.luoo.user.service.FriendService;
import com.luoo.user.service.UserInfoService;
import constants.Constants;
import controller.BaseController;
import dto.UserLoginDto;
import dto.UserMessageDto;
import enums.MessageTypeEnum;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
@RestController
@RequestMapping("/friend")
@Api(tags = "FriendController")
public class FriendController extends BaseController {
@Autowired
private HttpServletRequest request;
@Autowired
private FriendService friendService;
@Autowired
private UserInfoService userInfoService;
@Autowired
private RabbitTemplate rabbitTemplate;
/**
* 12
*
* @return
*/
@ApiOperation(value = "关注好友",notes = "friendid为关注好友的IDtype为1时是关注操作type为2时是接黑名单操作不是取关操作")
@PutMapping("/like/{friendid}/{type}")
public Result<Void> addFriend(@PathVariable String friendid, @PathVariable String type,@RequestHeader(value = "Authorization", required = true) String authorization) {
//验证是否登录并且拿到ID
UserLoginDto userLoginDto = getUserLoginDto(authorization);
if (null == userLoginDto) {
return Result.unauthorized(null);
}
String userid = userLoginDto.getUserId();
//判断是添加好友还是非好友(判断是关注操作还是拉黑操作)
if (type != null) {
if (type.equals("1")) {
//添加好友
int flag = friendService.addFriend(userid,friendid);
if (flag == 0) {
return Result.failed(StatusCode.FRIEND_CAN_NOT_REPEAT_ADD_FRIEND); // 不能重复添加好友
}
if (flag == 1) {
userInfoService.updatefanscountandfollowcount(1,userid,friendid);
UserInfo userInfo =userInfoService.findById(userid);
/**
* friend
*/
UserMessageDto userMessageDto = new UserMessageDto();
userMessageDto.setType(MessageTypeEnum.FOLLOW.getType()); //消息类型 1私信 2新赞3新关注 4新评论 5.系统消息
userMessageDto.setUserId(friendid);
userMessageDto.setContent(userInfo.getNickName()+" 关注了你");
userMessageDto.setTitle("您有新的粉丝");
userMessageDto.setSendUserAvatar(Constants.RESOURCE_PREFIX+userInfo.getAvatar());
userMessageDto.setSendUserId(userid);
userMessageDto.setSendUserNickName(userInfo.getNickName());
rabbitTemplate.convertAndSend("userMessage",userMessageDto);
return Result.success();
}
} else if (type.equals("2")) {
//添加非好友
int flag = friendService.addNoFriend(userid,friendid);
if (flag == 0) {
return Result.failed(StatusCode.FRIEND_CAN_NOT_REPEAT_ADD_NON_FRIEND);
}
if (flag == 1) {
return Result.success();
}
}
return Result.failed(StatusCode.VALIDATE_FAILED);
} else {
return Result.failed(StatusCode.VALIDATE_FAILED);
}
}
@ApiOperation(value = "取消关注好友")
@DeleteMapping("/{friendid}")
public Result<Void> deleteFriend(@PathVariable String friendid,@RequestHeader(value = "Authorization", required = true) String authorization){
//验证是否登录并且拿到ID
UserLoginDto userLoginDto = getUserLoginDto(authorization);
if (null == userLoginDto) {
return Result.unauthorized(null);
}
String userid = userLoginDto.getUserId();
friendService.deleteFriend(userid,friendid);
userInfoService.updatefanscountandfollowcount(-1,userid,friendid);
return Result.success();
}
}
Loading…
Cancel
Save