Commit 869d830d authored by 刘基明's avatar 刘基明

评论、点赞、转发数添加缓存

parent b263ae14
......@@ -20,4 +20,7 @@ public final class CommunityConstant {
public static final String OSS_PREFIX_FOLDER ="community/";
//图片压缩比例:50%
public static final String OSS_RESIZE_RATIO = "?x-oss-process=image/resize,p_50";
}
......@@ -2,32 +2,12 @@ package com.tanpu.community.api.constants;
public class RedisKeyConstant {
//话题页浏览量
public static final String TOPIC_PAGE_VIEW_COUNT_ ="TOPIC_PAGE_VIEW_COUNT_";
//话题总浏览量=总浏览量+带这个话题的帖子量
public static final String TOPIC_TOTAL_VIEW_COUNT_="TOPIC_TOTAL_VIEW_COUNT_";
//点赞量
public static final String TOPIC_LIKE_COUNT_="TOPIC_LIKE_COUNT_";
public static final String THEME_LIKE_COUNT ="THEME_LIKE_COUNT_";
//收藏量
public static final String TOPIC_BOOK_COUNT_="TOPIC_BOOK_COUNT_";
//用户数
public static final String TOPIC_USER_COUNT_="TOPIC_USER_COUNT_";
public static final String THEME_COMMENT_COUNT ="THEME_COMMENT_COUNT_";
//讨论量=发布主题贴数+回复总数
public static final String TOPIC_DISCUSS_COUNT_="TOPIC_DISCUSS_COUNT_";
//发帖数
public static final String TOPIC_THEME_COUNT_="TOPIC_THEME_COUNT_";
//回帖数
public static final String TOPIC_COMMENT_COUNT_="TOPIC_COMMENT_COUNT_";
//总用户数=访问话题页+发帖+回帖(去重)
public static final String TOPIC_TOTAL_USER_COUNT_ ="TOPIC_TOTAL_USER_COUNT_";
//访问话题人数
public static final String TOPIC_USER_VIEW_COUNT_="TOPIC_USER_VIEW_COUNT_";
//发帖人数
public static final String TOPIC_POST_USER_COUNT_ ="TOPIC_POST_USER_COUNT_";
//回帖人数
public static final String TOPIC_COMMENT_USER_COUNT_ ="TOPIC_COMMENT_USER_COUNT_";
// 出现在用户的搜索列表中的主题id
public static final String THEME_APPEAR_IN_SEARCH_LIST = "THEME_APPEAR_IN_SEARCH_LIST_";
public static final String THEME_FORWARD_COUNT ="THEME_FORWARD_COUNT_";
// feign 查询用户信息
public static final String CACHE_FEIGN_USER_INFO = "CACHE_FEIGN_USER_INFO_";
......@@ -43,7 +23,4 @@ public class RedisKeyConstant {
// 关注的人,上次浏览的最新主题last id
public static final String CACHE_IDOL_THEME_LAST_ID = "CACHE_IDOL_THEME_LAST_ID_";
public static final String THEME_VIEW_COUNT_="THEME_VIEW_COUNT_";
public static final String THEME_LIKE_COUNT_="THEME_LIKE_COUNT_";
public static final String THEME_BOOK_COUNT_="THEME_BOOK_COUNT_";
}
......@@ -9,6 +9,7 @@ import com.tanpu.common.api.CommonResp;
import com.tanpu.common.constant.ErrorCodeConstant;
import com.tanpu.common.exception.BizException;
import com.tanpu.common.util.JsonUtil;
import com.tanpu.community.api.CommunityConstant;
import com.tanpu.community.api.beans.qo.ESThemeQo;
import com.tanpu.community.api.beans.qo.FormerThemeQo;
import com.tanpu.community.api.beans.qo.ThemeContentQo;
......@@ -219,7 +220,6 @@ public class ThemeManager {
// 转发主题
public CreateThemeResp forward(ForwardThemeReq req, String userId) {
ThemeEntity targetTheme = themeService.queryByThemeId(req.getFormerThemeId());
ThemeEntity themeEntity = ThemeEntity.builder()
.content(JsonUtil.toJson(req.getContent()))
.topicId(req.getTopicId())
......@@ -242,6 +242,8 @@ public class ThemeManager {
} catch (Exception e) {
log.error("error in save theme to ES. themeId:{}, error:{}", themeEntity.getThemeId(), ExceptionUtils.getStackTrace(e));
}
//失效缓存
redisCache.evict(StringUtils.joinWith("_", THEME_FORWARD_COUNT, themeEntity.getThemeId()));
return CreateThemeResp.builder().themeId(themeEntity.getThemeId()).build();
}
......@@ -345,20 +347,25 @@ public class ThemeManager {
() -> this.getFormerTheme(themeQo.getFormerThemeId()), FormerThemeQo.class);
themeQo.setFormerTheme(former);
//压缩图片
themeQo.getContent().stream().forEach(o-> {
themeQo.getContent().stream().forEach(o -> {
if (o.getImgList() != null) {
for (ImagesDTO imagesDTO : o.getImgList()) {
if (StringUtils.isNotBlank(imagesDTO.getRemark()))
imagesDTO.setResizeUrl(imagesDTO.getRemark()+"?x-oss-process=image/resize,p_50");
imagesDTO.setResizeUrl(imagesDTO.getRemark() + CommunityConstant.OSS_RESIZE_RATIO);
}
}
});
// 点赞,收藏,转发
Integer likeCount = collectionService.getCountByTypeAndId(themeId, CollectionTypeEnum.LIKE_THEME);
Integer commentCount = commentService.getCommentCountByThemeId(themeId);
Integer forwardCount = themeService.getForwardCountById(themeId);
Integer likeCount = redisCache.getObject(StringUtils.joinWith("_", THEME_LIKE_COUNT, themeId), 60,
() -> collectionService.getCountByTypeAndId(themeId, CollectionTypeEnum.LIKE_THEME), Integer.class);
Integer commentCount = redisCache.getObject(StringUtils.joinWith("_", THEME_COMMENT_COUNT, themeId), 60,
() -> commentService.getCommentCountByThemeId(themeId), Integer.class);
Integer forwardCount = redisCache.getObject(StringUtils.joinWith("_", THEME_FORWARD_COUNT, themeId), 60,
() -> themeService.getForwardCountById(themeId), Integer.class);
themeQo.setCommentCount(commentCount);
themeQo.setLikeCount(likeCount);
themeQo.setForwardCount(forwardCount);
......@@ -398,6 +405,7 @@ public class ThemeManager {
/**
* 返回用户发布、回复、点赞、收藏的主题列表
*
* @param req 查询用户
* @param userId 当前用户
* @return
......@@ -433,7 +441,7 @@ public class ThemeManager {
throw new BizException("内部接口调用失败");
}
UserInfoResp user = userInfoNewCommonResp.getData();
themeQos.stream().forEach(o->reBuildAuthorInfo(o,user));
themeQos.stream().forEach(o -> reBuildAuthorInfo(o, user));
redisCache.put(StringUtils.joinWith("_", CACHE_FEIGN_USER_INFO, userId), user, 60);
}
......@@ -475,6 +483,8 @@ public class ThemeManager {
} else if (OperationTypeEnum.CANCEL.getCode().equals(req.getType())) {
collectionService.delete(req.getThemeId(), userId, CollectionTypeEnum.LIKE_THEME);
}
//失效缓存
redisCache.evict(StringUtils.joinWith("_", THEME_LIKE_COUNT, req.getThemeId()));
}
......@@ -543,7 +553,7 @@ public class ThemeManager {
*/
private void checkContent(CreateThemeReq req) {
if (ThemeTypeEnum.LONG_TEXT.getCode().equals(req.getThemeType()) && req.getTitle().length()>50){
if (ThemeTypeEnum.LONG_TEXT.getCode().equals(req.getThemeType()) && req.getTitle().length() > 50) {
throw new IllegalArgumentException("长文标题不能超过50字");
}
......@@ -557,12 +567,12 @@ public class ThemeManager {
// 腾讯云接口最多支持5000文字校验
// 检查内容是否涉黄违法
boolean b;
while (content.length()>5000){
while (content.length() > 5000) {
b = TencentcloudUtils.textModeration(content.substring(0, 5000));
if (!b) {
throw new BizException(ErrorCodeConstant.CONTENT_ILLEGAL);
}
content=content.substring(5000);
content = content.substring(5000);
}
b = TencentcloudUtils.textModeration(content);
if (!b) {
......@@ -665,7 +675,7 @@ public class ThemeManager {
return userInfoNewCommonResp.getData();
}
private void reBuildAuthorInfo(ThemeQo themeQo,UserInfoResp userInfo){
private void reBuildAuthorInfo(ThemeQo themeQo, UserInfoResp userInfo) {
themeQo.setNickName(userInfo.getNickName());
themeQo.setUserImg(userInfo.getHeadImageUrl());
themeQo.setUserIntroduction(userInfo.getIntroduction());
......
......@@ -145,8 +145,12 @@ public class CommentService {
// 失效关联主题缓存
private void evictThemeCache(String themeId){
// 评论内容
redisCache.evict(StringUtils.joinWith("_", CACHE_COMMENT_THEMEID, themeId));
// 主题内容
redisCache.evict(StringUtils.joinWith("_", CACHE_THEME_ID, themeId));
// 评论数
redisCache.evict(StringUtils.joinWith("_", THEME_LIKE_COUNT, themeId));
}
}
......@@ -18,8 +18,12 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
@Service
......@@ -230,7 +234,6 @@ public class ThemeService {
if (CollectionUtils.isEmpty(userIds)){
return Collections.emptyList();
}
//TODO 索引优化
LambdaQueryWrapper<ThemeEntity> queryWrapper = new LambdaQueryWrapper<ThemeEntity>()
.in(ThemeEntity::getAuthorId, userIds)
.last("limit " + pageStart + ", " + pageSize)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment