Commit 9a58c57f authored by 张辰's avatar 张辰

cache

parent 3fdd9bb1
......@@ -26,6 +26,17 @@ public class RedisKeyConstant {
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_";
// feign 查询用户信息
public static final String CACGE_FEIGN_USER_INFO = "CACHE_FEIGN_USER_INFO_";
// 主题下的评论
public static final String CACHE_COMMENT_THEMEID = "CACHE_COMMENT_THEMEID_";
// 评论本身
public static final String CAHCE_COMMENT_ID = "CACHE_COMMENT_ID_";
// 主题本身
public static final String CACHE_THEME_ID = "CACHE_THEME_ID_";
public static final String THEME_VIEW_COUNT_="THEME_VIEW_COUNT_";
public static final String THEME_LIKE_COUNT_="THEME_LIKE_COUNT_";
......
......@@ -45,6 +45,10 @@ public class RedisCache {
return ret;
}
public void evict(String key) {
delete(key);
}
private String get(String key) {
key = cacheName + ":" + key;
return redisHelper.get(key);
......@@ -60,6 +64,10 @@ public class RedisCache {
}
}
private void delete(String key) {
redisHelper.delete(key);
}
public static class Builder {
RedisCache cache = new RedisCache();
......
package com.tanpu.community.config;
import com.tanpu.community.cache.RedisCache;
import com.tanpu.community.util.SpringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.Resource;
@Configuration
public class CacheConfig {
@Autowired
private SpringUtils springUtils;
@Bean
public RedisCache redisCache() {
return new RedisCache.Builder().cacheName("community2").build();
......
......@@ -8,17 +8,20 @@ import com.tanpu.common.exception.BizException;
import com.tanpu.community.api.beans.qo.ThemeQo;
import com.tanpu.community.api.beans.req.theme.*;
import com.tanpu.community.api.beans.resp.CreateThemeResp;
import com.tanpu.community.cache.RedisCache;
import com.tanpu.community.manager.ThemeManager;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import static com.tanpu.community.api.constants.RedisKeyConstant.CACHE_THEME_ID;
@RestController
@Slf4j
@RequestMapping(value = "/api/theme")
......@@ -28,6 +31,8 @@ public class ThemeController {
private ThemeManager themeManager;
@Resource
private UserHolder userHolder;
@Autowired
private RedisCache redisCache;
@AuthLogin
@ApiOperation("发表主题")
......@@ -57,7 +62,8 @@ public class ThemeController {
@ResponseBody
public CommonResp<ThemeQo> getThemeMainText(@RequestParam String themeId) {
String userId = userHolder.getUserId();
ThemeQo themeQo = themeManager.getDetail(themeId, userId);
ThemeQo themeQo = redisCache.getObject(StringUtils.joinWith("_", CACHE_THEME_ID, themeId), 60,
() -> themeManager.getDetail(themeId, userId), ThemeQo.class);
return CommonResp.success(themeQo);
}
......
......@@ -2,6 +2,11 @@ package com.tanpu.community.dao.mapper.community;
import com.tanpu.community.dao.entity.community.VisitSummaryEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import java.util.List;
/**
* <p>
......@@ -12,5 +17,12 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
* @since 2021-07-22
*/
public interface VisitSummaryMapper extends BaseMapper<VisitSummaryEntity> {
@Select("select * from visit_summary where ident=#{ident}")
VisitSummaryEntity selectByIdent(@Param("ident") String ident);
@Update("update visit_summary set duration=duration+#{duration} where ident=#{ident}")
void updateDurByIdent(@Param("duration") Integer dur, @Param("ident") String ident);
@Select("select ref_id from visit_summary where visitor_id=#{visitorId} and ref_id in (#{refIds})")
List<String> selectRefIdByUserId(@Param("visitorId") String visitorId, @Param("refIds") String refIds);
}
......@@ -11,12 +11,14 @@ import com.tanpu.community.api.enums.CollectionTypeEnum;
import com.tanpu.community.api.enums.CommentTypeEnum;
import com.tanpu.community.api.enums.OperationTypeEnum;
import com.tanpu.community.api.enums.ReportTypeEnum;
import com.tanpu.community.cache.RedisCache;
import com.tanpu.community.dao.entity.community.CommentEntity;
import com.tanpu.community.feign.fatools.FeignClientForFatools;
import com.tanpu.community.service.CollectionService;
import com.tanpu.community.service.CommentService;
import com.tanpu.community.service.ReportLogService;
import com.tanpu.community.util.ConvertUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
......@@ -26,6 +28,8 @@ import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import static com.tanpu.community.api.constants.RedisKeyConstant.CACGE_FEIGN_USER_INFO;
@Service
public class CommentManager {
......@@ -41,6 +45,9 @@ public class CommentManager {
@Resource
private ReportLogService reportLogService;
@Autowired
private RedisCache redisCache;
// 评论(对主题)
public void comment(CreateCommentReq req, String userId) {
CommentEntity commentEntity = CommentEntity.builder()
......@@ -60,13 +67,13 @@ public class CommentManager {
List<CommentEntity> commentEntities = commentService.selectByThemeIdAndParentId(themeId, null);
List<CommentQo> commentQos = ConvertUtil.commentEntity2Qos(commentEntities);
Set<String> likeCommentList = collectionService.getListByUser(userId, CollectionTypeEnum.LIKE_COMMENT);
for (CommentQo commentQo : commentQos) {
//用户信息
String authorId = commentQo.getAuthorId();
CommonResp<UserInfoNew> userInfoNewCommonResp = feignClientForFatools.queryUsersListNew(authorId);
CommonResp<UserInfoNew> userInfoNewCommonResp = redisCache.getObject(StringUtils.joinWith(CACGE_FEIGN_USER_INFO, authorId),
60 * 10, () -> feignClientForFatools.queryUsersListNew(authorId), CommonResp.class);
if (userInfoNewCommonResp.isNotSuccess()) {
throw new BizException("内部接口调用失败");
}
......
......@@ -10,6 +10,7 @@ import com.tanpu.community.api.beans.req.theme.*;
import com.tanpu.community.api.beans.resp.CreateThemeResp;
import com.tanpu.community.api.beans.resp.ThemeFullSearchResp;
import com.tanpu.community.api.enums.*;
import com.tanpu.community.cache.RedisCache;
import com.tanpu.community.dao.entity.community.*;
import com.tanpu.community.dao.entity.user.UserInfoEntity;
import com.tanpu.community.feign.fatools.FeignClientForFatools;
......@@ -31,6 +32,8 @@ import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
import static com.tanpu.community.api.constants.RedisKeyConstant.CACHE_THEME_ID;
@Slf4j
@Service
public class ThemeManager {
......@@ -73,6 +76,9 @@ public class ThemeManager {
@Autowired
private FeignClientForFatools feignClientForFatools;
@Autowired
private RedisCache redisCache;
public ThemeFullSearchResp themeFullSearch(String keyword, Integer pageNo, Integer pageSize, List<String> excludeIds, String userId) {
Integer from = (pageNo - 1) * pageSize;
ThemeFullSearchResp resp = new ThemeFullSearchResp();
......@@ -95,10 +101,11 @@ public class ThemeManager {
return resp;
}
@Transactional
/**
* 发表主题(可修改)
*/
@Transactional
public CreateThemeResp publishTheme(CreateThemeReq req, String userId) {
//直播类型做转播检查
List<ThemeContentReq> contents = req.getContent();
......@@ -135,6 +142,8 @@ public class ThemeManager {
log.error("error in save theme to ES. themeId:{}, error:{}", themeEntity.getThemeId(), ExceptionUtils.getStackTrace(e));
}
redisCache.evict(StringUtils.joinWith("_", CACHE_THEME_ID, themeEntity.getThemeId()));
return CreateThemeResp.builder().themeId(themeEntity.getThemeId()).build();
}
......
......@@ -37,13 +37,10 @@ public class VisitSummaryManager {
KafkaDurationUptMsg msg = JSON.parseObject(message, KafkaDurationUptMsg.class);
// ident在每次进入新页面 & 回退 的时候都会随机生成一个,所以用ident做唯一key即可。
VisitSummaryEntity vs = ConvertUtil.convertFromKafka(msg);
visitSummaryService.insertOrUpdateDur(vs);
}
public void addTopicPageView(String topicId) {
String userId = userHolder.getUserId();
visitSummaryService.addPageView(userId, topicId, VisitTypeEnum.TOPIC_PAGE_VIEW);
......
package com.tanpu.community.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.core.type.TypeReference;
import com.tanpu.common.exception.BizException;
import com.tanpu.common.redis.RedisKeyHelper;
import com.tanpu.common.uuid.UuidGenHelper;
import com.tanpu.community.api.enums.CommentTypeEnum;
import com.tanpu.community.api.enums.DeleteTagEnum;
import com.tanpu.community.api.enums.ReportStatusEnum;
import com.tanpu.community.cache.RedisCache;
import com.tanpu.community.dao.entity.community.CommentEntity;
import com.tanpu.community.dao.mapper.community.CommentMapper;
import org.apache.commons.collections4.CollectionUtils;
......@@ -19,6 +22,9 @@ import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import static com.tanpu.community.api.constants.RedisKeyConstant.CACHE_COMMENT_THEMEID;
import static com.tanpu.community.api.constants.RedisKeyConstant.CAHCE_COMMENT_ID;
@Service
public class CommentService {
......@@ -28,6 +34,9 @@ public class CommentService {
@Autowired
private UuidGenHelper uuidGenHelper;
@Autowired
private RedisCache redisCache;
@Transactional
public void insertComment(CommentEntity commentEntity) {
commentEntity.setCommentId(uuidGenHelper.getUuidStr());
......@@ -64,17 +73,14 @@ public class CommentService {
}
public List<CommentEntity> selectByThemeIdAndParentId(String themeId, String parentId) {
LambdaQueryWrapper<CommentEntity> queryWrapper = new LambdaQueryWrapper<CommentEntity>()
.eq(CommentEntity::getThemeId, themeId)
.eq(CommentEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode())
.orderByDesc(CommentEntity::getCreateTime);
//二级评论,暂未开放,注意“”与null的区别
// if (parentId == null) {
// queryWrapper.isNull(CommentEntity::getParentId);
// } else {
// queryWrapper.eq(CommentEntity::getParentId, parentId);
// }
return commentMapper.selectList(queryWrapper);
return redisCache.getList(StringUtils.joinWith("_", CACHE_COMMENT_THEMEID, themeId, parentId),
60, () -> {
LambdaQueryWrapper<CommentEntity> queryWrapper = new LambdaQueryWrapper<CommentEntity>()
.eq(CommentEntity::getThemeId, themeId)
.eq(CommentEntity::getDeleteTag, DeleteTagEnum.NOT_DELETED.getCode())
.orderByDesc(CommentEntity::getCreateTime);
return commentMapper.selectList(queryWrapper);
}, new TypeReference<List<CommentEntity>>() {});
}
......@@ -110,5 +116,6 @@ public class CommentService {
}
commentEntity.setReportStatus(ReportStatusEnum.REPORTED.getCode());
commentMapper.updateById(commentEntity);
redisCache.evict(StringUtils.joinWith("_", CAHCE_COMMENT_ID, commentId));
}
}
......@@ -15,7 +15,6 @@ import java.util.List;
public class HomePageService {
@Resource
private HomePageMapper homePageMapper;
......
......@@ -3,9 +3,11 @@ package com.tanpu.community.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.tanpu.common.exception.BizException;
import com.tanpu.common.redis.RedisHelper;
import com.tanpu.common.uuid.UuidGenHelper;
import com.tanpu.community.api.enums.DeleteTagEnum;
import com.tanpu.community.api.enums.ReportStatusEnum;
import com.tanpu.community.cache.RedisCache;
import com.tanpu.community.dao.entity.community.ThemeEntity;
import com.tanpu.community.dao.mapper.community.ThemeMapper;
import org.apache.commons.collections4.CollectionUtils;
......@@ -16,11 +18,14 @@ import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import static com.tanpu.community.api.constants.RedisKeyConstant.THEME_APPEAR_IN_SEARCH_LIST;
@Service
public class ThemeService {
......@@ -30,6 +35,9 @@ public class ThemeService {
@Autowired
private UuidGenHelper uuidGenHelper;
@Autowired
private RedisHelper redisHelper;
@Transactional
public void insertTheme(ThemeEntity themeEntity) {
themeEntity.setThemeId(uuidGenHelper.getUuidStr());
......
......@@ -6,6 +6,8 @@ import com.tanpu.community.dao.entity.community.VisitSummaryEntity;
import com.tanpu.community.dao.mapper.community.VisitSummaryMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
......@@ -20,6 +22,23 @@ public class VisitSummaryService {
@Resource
private VisitSummaryMapper visitSummaryMapper;
public List<String> filterUserNotVisited(String userId, List<String> refIds) {
if (refIds.isEmpty()) {
return refIds;
}
List<String> visited = visitSummaryMapper.selectRefIdByUserId(userId, StringUtils.joinWith(",", refIds));
return ListUtils.subtract(refIds, visited);
}
@Transactional
public void insertOrUpdateDur(VisitSummaryEntity vs) {
if (visitSummaryMapper.selectByIdent(vs.getIdent()) == null) {
visitSummaryMapper.insert(vs);
} else {
visitSummaryMapper.updateDurByIdent(vs.getDuration(), vs.getIdent());
}
}
@Transactional
public void addPageView(String userId, String targetId, VisitTypeEnum type) {
visitSummaryMapper.insert(VisitSummaryEntity.builder()
......@@ -54,11 +73,6 @@ public class VisitSummaryService {
.eq(VisitSummaryEntity::getRefType, VisitTypeEnum.THEME_PAGE_VIEW.getCode()));
}
// 更新访问时长
public void updateDurByIdent(String ident, Integer dur) {
// visitSummaryMapper.updateDurByIdent(ident, dur);
}
public LocalDateTime queryLatestViewFollow(String userId) {
List<VisitSummaryEntity> visitSummaryEntities = visitSummaryMapper.selectList(new LambdaQueryWrapper<VisitSummaryEntity>()
.eq(VisitSummaryEntity::getVisitorId, userId)
......
package com.tanpu.community.util;
import com.tanpu.common.api.CommonResp;
import com.tanpu.common.api.*;
import com.tanpu.common.constant.ErrorCodeConstant;
import com.tencentcloudapi.cms.v20190321.CmsClient;
import com.tencentcloudapi.cms.v20190321.models.ImageModerationRequest;
......
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