fix: add j2cache to level_1 & level_N,remove cache when cache evict

main
wangqing 11 months ago
parent 2abbece9b5
commit a1c00dd6fc

@ -92,24 +92,7 @@ public class CommentController extends BaseController {
* ,parentID2
* @return
*/
@ApiOperation(value = "根据期刊ID获取分页评论列表(废除),用最新和热门两个接口,可以默认用热门展示",notes = "根据期刊ID获取根节点评论分页列表 数据库中保存的有期刊号的都是根节点期刊号为空的代表有父评论且parentID一定有值拿到当然根节点评论列表每个根评论有 commentCount字段页面可以显示有 280个回复然后根据 @GetMapping(\"/comment/{parentId}/{page}/{size}\") 接口分页拿到该评论下的子评论列表页面在请求添加评论的时候当给评论添加子评论的时候父评论parentID有值就无法添加子评论并且不显示回复2字控制到两级")
@ApiImplicitParams({@ApiImplicitParam(name = "journalId", value = "剘刊id", required = true),
@ApiImplicitParam(name = "page", value = "分页: 页码以1开始", required = true),
@ApiImplicitParam(name = "size", value = "分页: 每页数量", required = true)})
@GetMapping("/{journalId}/{page}/{size}")
public Result findAllByJournalId(@PathVariable String journalId,@PathVariable int page,@PathVariable int size, @RequestHeader(value = "Authorization", required = false) String authorization) {
//验证是否登录并且拿到ID
UserLoginDto userLoginDto = getUserLoginDto(authorization);
if (null == userLoginDto) {
Page<Comment> pageList = commentService.findByJournalId(journalId,page,size);
List<CommentResp> list =pageList.stream().map(x->getCommentResp(x,"")).collect(Collectors.toList());
return Result.success(new PageResult<CommentResp>(Long.valueOf(list.size()),list));
}
String userId = userLoginDto.getUserId();
Page<Comment> pageList = commentService.findByJournalId(journalId,page,size);
List<CommentResp> list =pageList.stream().map(x->getCommentResp(x,userId)).collect(Collectors.toList());
return Result.success(new PageResult<CommentResp>(Long.valueOf(list.size()),list));
}
@ApiOperation(value = "根据期刊ID获取分页评论列表热门,按点赞数倒序",notes = "根据期刊ID获取根节点评论分页列表 数据库中保存的有期刊号的都是根节点期刊号为空的代表有父评论且parentID一定有值拿到当然根节点评论列表每个根评论有 commentCount字段页面可以显示有 280个回复然后根据 @GetMapping(\"/comment/{parentId}/{page}/{size}\") 接口分页拿到该评论下的子评论列表页面在请求添加评论的时候当给评论添加子评论的时候父评论parentID有值就无法添加子评论并且不显示回复2字控制到两级")
@ -118,8 +101,8 @@ public class CommentController extends BaseController {
@ApiImplicitParam(name = "size", value = "分页: 每页数量", required = true)})
@GetMapping("/hot/{journalId}/{page}/{size}")
public Result findHotByJournalId(@PathVariable String journalId,@PathVariable int page,@PathVariable int size, @RequestHeader(value = "Authorization", required = false) String authorization) {
// List<CommentResp> list = (List<CommentResp>) cacheChannel.get(Constants.J2CACHE_REGION_JOURNAL_COMMENT_PAGE_HOT+"_"+journalId,page+"_"+size, key->getHotList(journalId,page,size),true).getValue();
List<CommentResp> list = getHotList(journalId,page,size);
List<CommentResp> list = (List<CommentResp>) cacheChannel.get(Constants.J2CACHE_REGION_JOURNAL_COMMENT_PAGE_HOT+"_"+journalId,page+"_"+size, key->getHotList(journalId,page,size),true).getValue();
// List<CommentResp> list = getHotList(journalId,page,size);
//验证是否登录并且拿到ID
UserLoginDto userLoginDto = getUserLoginDto(authorization);
if (null == userLoginDto) {
@ -157,7 +140,8 @@ public class CommentController extends BaseController {
@ApiImplicitParam(name = "size", value = "分页: 每页数量", required = true)})
@GetMapping("/new/{journalId}/{page}/{size}")
public Result findNewByJournalId(@PathVariable String journalId,@PathVariable int page,@PathVariable int size, @RequestHeader(value = "Authorization", required = false) String authorization) {
List<CommentResp> list = getNewList(journalId,page,size);
// List<CommentResp> list = getNewList(journalId,page,size);
List<CommentResp> list = (List<CommentResp>) cacheChannel.get(Constants.J2CACHE_REGION_JOURNAL_COMMENT_PAGE_NEW+"_"+journalId,page+"_"+size, key->getNewList(journalId,page,size),true).getValue();
//验证是否登录并且拿到ID
UserLoginDto userLoginDto = getUserLoginDto(authorization);
if (null == userLoginDto) {
@ -305,6 +289,7 @@ public class CommentController extends BaseController {
return Result.failed("您只能删除自己发表的评论");
}
commentService.deleteById(commentId);
return Result.success();
}
@ -317,8 +302,8 @@ public class CommentController extends BaseController {
@GetMapping("/comment/{parentId}/{page}/{size}")
public Result findByParentId(@PathVariable String parentId,@PathVariable int page,@PathVariable int size, @RequestHeader(value = "Authorization", required = false) String authorization) {
List<CommentResp> list = getChildrenCommentList(parentId,page,size);
List<CommentResp> list = (List<CommentResp>) cacheChannel.get(Constants.J2CACHE_REGION_JOURNAL_COMMENT_PAGE_CHILDREN+"_"+parentId,page+"_"+size, key->getChildrenCommentList(parentId,page,size),true).getValue();
// List<CommentResp> list = getChildrenCommentList(parentId,page,size);
//验证是否登录并且拿到ID
UserLoginDto userLoginDto = getUserLoginDto(authorization);
if (null == userLoginDto) {

@ -5,6 +5,8 @@ import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.repository.MongoRepository;
import java.util.List;
public interface CommentDao extends MongoRepository<Comment,String> {
public Page<Comment> findByParentId(String parentId, Pageable pageable);
@ -14,4 +16,7 @@ public interface CommentDao extends MongoRepository<Comment,String> {
public Page<Comment> findByJournalIdOrderByThumbupCountDesc(String journalId, Pageable pageable);
public Page<Comment> findAllByOrderByPublishTimeDesc(Pageable pageable);
public List<Comment> findAllByUserId(String userId);
}

@ -0,0 +1,47 @@
package com.luoo.comment.listener;
import com.luoo.comment.dao.CommentDao;
import com.luoo.comment.pojo.Comment;
import constants.Constants;
import net.oschina.j2cache.CacheChannel;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
@Component
@RabbitListener(queues = "userInfoCommentCache")
public class UserInfoCommentCacheListener {
@Autowired
private CacheChannel cacheChannel;
@Autowired
private CommentDao commentDao;
@RabbitHandler
public void excuteClearCache(String userId) {
List<Comment> commentList = commentDao.findAllByUserId(userId);
Set<String> journalIdSet = commentList.parallelStream().map(comment -> comment.getRootJournalId()).collect(Collectors.toSet());
Set<String> parentIdSet = commentList.parallelStream().map(comment -> comment.getParentId()).collect(Collectors.toSet());
journalIdSet.stream().parallel().forEach(x->this.clearJournalCache(x));
parentIdSet.stream().parallel().forEach(x->this.clearParentCache(x));
}
private void clearJournalCache(String journalId) {
cacheChannel.clear(Constants.J2CACHE_REGION_JOURNAL_COMMENT_PAGE_HOT+"_"+journalId);
cacheChannel.clear(Constants.J2CACHE_REGION_JOURNAL_COMMENT_PAGE_NEW+"_"+journalId);
// System.out.println("journalId——————"+journalId);
}
private void clearParentCache(String parentId) {
cacheChannel.clear(Constants.J2CACHE_REGION_JOURNAL_COMMENT_PAGE_CHILDREN+"_"+parentId);
// System.out.println("parentId_____"+parentId);
}
}

@ -11,6 +11,7 @@ import constants.Constants;
import dto.UserLoginDto;
import dto.UserMessageDto;
import enums.MessageTypeEnum;
import net.oschina.j2cache.CacheChannel;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
@ -49,6 +50,9 @@ public class CommentService {
@Autowired
private JournalClient journalClient;
@Autowired
private CacheChannel cacheChannel;
public List<Comment> findAll(){
return commentDao.findAll();
}
@ -71,8 +75,17 @@ public class CommentService {
comment.setJournalImage(journalRespDTO.getImage());
}
if(StringUtils.isEmpty(comment.getParentId())) {
cacheChannel.clear(Constants.J2CACHE_REGION_JOURNAL_COMMENT_PAGE_HOT+"_"+comment.getJournalId());
cacheChannel.clear(Constants.J2CACHE_REGION_JOURNAL_COMMENT_PAGE_NEW+"_"+comment.getJournalId());
// System.out.println("journalId____"+comment.getJournalId());
}
// 如果当前添加的评论,有父节点,那么根节点的评论回复数要加一
if (comment.getParentId()!=null && !"".equals(comment.getParentId())){
cacheChannel.clear(Constants.J2CACHE_REGION_JOURNAL_COMMENT_PAGE_CHILDREN+"_"+comment.getParentId());
// System.out.println("parentId____"+comment.getParentId());
Comment parentComment = commentDao.findById(comment.getParentId()).get();
CommentHis commentHis = new CommentHis();
@ -131,7 +144,15 @@ public class CommentService {
public void deleteById(String id) {
Comment comment =commentDao.findById(id).get();
if(StringUtils.isNotEmpty(comment.getParentId())) {
cacheChannel.clear(Constants.J2CACHE_REGION_JOURNAL_COMMENT_PAGE_CHILDREN+"_"+comment.getParentId());
} else {
cacheChannel.clear(Constants.J2CACHE_REGION_JOURNAL_COMMENT_PAGE_HOT+"_"+comment.getRootJournalId());
cacheChannel.clear(Constants.J2CACHE_REGION_JOURNAL_COMMENT_PAGE_NEW+"_"+comment.getRootJournalId());
}
commentDao.deleteById(id);
}
public Page<Comment> findByParentId(String parentId, int page, int size) {

@ -12,7 +12,12 @@ 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_COMMENT_PAGE_HOT = "journal_comment_page_hot";
public static final String J2CACHE_REGION_JOURNAL_COMMENT_PAGE_NEW = "journal_comment_page_new";
public static final String J2CACHE_REGION_JOURNAL_COMMENT_PAGE_CHILDREN = "journal_comment_page_children";
public static final String TOKEN_PREFIX = "Bearer ";
public static final int TOKEN_PREFIX_LENGTH = TOKEN_PREFIX.length();

@ -14,6 +14,7 @@ import java.util.stream.Collectors;
import com.luoo.user.dto.TotalCommentVo;
import com.luoo.user.pojo.*;
import com.luoo.user.service.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
@ -86,6 +87,9 @@ public class MyController extends BaseController {
@Autowired
private MongoTemplate mongoTemplate;
@Autowired
private RabbitTemplate rabbitTemplate;
public static String USER_AVATAR_DIRECTORY = "user/avatar/";
public static String USER_FEEDBACK_IMAGE_DIRECTORY = "user/feedback/";
@ -117,6 +121,7 @@ public class MyController extends BaseController {
if (!StringTools.isEmpty(nickName)) {
user.setNickName(nickName);
rabbitTemplate.convertAndSend("userInfoCommentCache",userLoginDto.getUserId());
}
if (!StringTools.isEmpty(userInfoUpdateDto.getSignature())) {
user.setSignature(userInfoUpdateDto.getSignature());
@ -150,6 +155,7 @@ public class MyController extends BaseController {
user.setAvatar(filePath);
userInfoService.update(user);
rabbitTemplate.convertAndSend("userInfoCommentCache",userLoginDto.getUserId());
return Result.success(Constants.RESOURCE_PREFIX + filePath);
}

Loading…
Cancel
Save