From 56821c988e0255a647630300ad74c8579fc69f44 Mon Sep 17 00:00:00 2001 From: Revers <0x00stone@gmail.com> Date: Mon, 8 Apr 2024 00:48:28 +0800 Subject: [PATCH] fix article and add cache --- .../src/main/java/constants/Constants.java | 2 + .../dto/response/cms/ArticleRespDTO.java | 6 +- .../luoo/music/service/CMSArticleService.java | 130 +++++++++++------- luoo_music/src/main/resources/bootstrap.yml | 2 + 4 files changed, 86 insertions(+), 54 deletions(-) diff --git a/luoo_common/src/main/java/constants/Constants.java b/luoo_common/src/main/java/constants/Constants.java index ab1c4ee..63b396e 100644 --- a/luoo_common/src/main/java/constants/Constants.java +++ b/luoo_common/src/main/java/constants/Constants.java @@ -28,6 +28,8 @@ public class Constants { public static final String J2CACHE_REGION_SEARCH_AUTO_COMPLETE = "search_auto_complete"; public static final String J2CACHE_REGION_JOURNAL_HOT_COMMENTS = "journal_hot_comments"; + public static final String J2CACHE_REGION_ARTICLE = "article"; + public static final String J2CACHE_REGION_ARTICLE_PAGE = "article_page"; public static final String RABBIT_MESSAGE_CLEAN_JOURANL_QUERY_PAGE = "clean_journal_query_page"; diff --git a/luoo_music/src/main/java/com/luoo/music/dto/response/cms/ArticleRespDTO.java b/luoo_music/src/main/java/com/luoo/music/dto/response/cms/ArticleRespDTO.java index 0d6379c..7cf1f6e 100644 --- a/luoo_music/src/main/java/com/luoo/music/dto/response/cms/ArticleRespDTO.java +++ b/luoo_music/src/main/java/com/luoo/music/dto/response/cms/ArticleRespDTO.java @@ -45,8 +45,8 @@ public class ArticleRespDTO implements Serializable { @ApiModelProperty(value = "是否允许评论 否:0 是:1") private String allowCommit; - @ApiModelProperty(value = "是否自动推送 否:0 是:1") - private String autoPush; + @ApiModelProperty(value = "是否自动推送 否:false 是:true") + private Boolean autoPush; @ApiModelProperty(value = "总评论数,大于99,显示99+") private Long totalCommentReply; @@ -76,7 +76,7 @@ public class ArticleRespDTO implements Serializable { response.setIsScheduled(article.getIsScheduled()); response.setPubTime(article.getPubTime().format(DateTimeFormatter.ISO_DATE_TIME)); response.setAllowCommit(article.getAllowCommit()); - response.setAutoPush(article.getAutoPush()); + response.setAutoPush("1".equals(article.getAutoPush())?true:false); response.setSongId(article.getSongId()); return response; diff --git a/luoo_music/src/main/java/com/luoo/music/service/CMSArticleService.java b/luoo_music/src/main/java/com/luoo/music/service/CMSArticleService.java index ebc327f..dc3e047 100644 --- a/luoo_music/src/main/java/com/luoo/music/service/CMSArticleService.java +++ b/luoo_music/src/main/java/com/luoo/music/service/CMSArticleService.java @@ -2,6 +2,7 @@ package com.luoo.music.service; import api.PageResult; import api.Result; +import com.alibaba.fastjson.JSON; import com.luoo.music.client.UserClient; import com.luoo.music.dao.*; import com.luoo.music.dto.request.cms.ArticleAddModel; @@ -11,6 +12,7 @@ import com.luoo.music.pojo.Article; import com.luoo.music.pojo.Comment; import com.luoo.music.pojo.UserInfo; import com.luoo.music.util.Constants; +import net.oschina.j2cache.CacheChannel; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Example; @@ -55,6 +57,9 @@ public class CMSArticleService { @Autowired private CommentDao commentDao; + @Autowired + private CacheChannel cacheChannel; + public Result add(ArticleAddModel paramAdd) { String image = null; @@ -79,6 +84,7 @@ public class CMSArticleService { if(!articleDao.exists(example)) { articleDao.save(article); + cleanArticleCache(article.getId()); } return Result.success(); @@ -151,31 +157,32 @@ public class CMSArticleService { } public Result deleteById(String id){ - Article article = articleDao.findById(id).get(); + Article article = JSON.parseObject(getArticleFromCache(id), Article.class); if (!Objects.isNull(article)) { article.setIsDeleted("1"); articleDao.save(article); + cleanArticleCache(article.getId()); return Result.success(); } return Result.failed("文章不存在"); } public Result visitAdd(String id){ - Article article = articleDao.findById(id).get(); + Article article = JSON.parseObject(getArticleFromCache(id), Article.class); if (!Objects.isNull(article)) { article.setVisits(article.getVisits()+1); articleDao.save(article); + cleanArticleCache(id); return Result.success(); } return Result.failed("文章不存在"); } public Result update(String id,ArticleAddModel param){ - Optional
optional = articleDao.findById(id); - if (!optional.isPresent()) { - return Result.failed("找不到期刊: " + id); + Article article = JSON.parseObject(getArticleFromCache(id), Article.class); + if (article == null) { + return Result.failed("找不到文章: " + id); } - Article article = optional.get(); //如果图片路径存在 temp/ 则为新图片 if (StringUtils.isNotBlank(param.getImage()) && param.getImage().contains(Constants.TEMP_KEY_PREFIX)) { @@ -207,70 +214,55 @@ public class CMSArticleService { article.setPubTime(param.getPubTime()); articleDao.save(article); + cleanArticleCache(article.getId()); //TODO: 发布方式 return Result.success(); } public Result findOne(String id) { - Optional
optional=articleDao.findById(id); - if(!optional.isPresent()) { - return Result.failed("无法找到文章: " + id); - } - Article article = optional.get(); + Article article = JSON.parseObject(getArticleFromCache(id), Article.class); if("1".equals(article.getIsDeleted())){ return Result.success(null); - }else { - ArticleRespDTO response = ArticleRespDTO.convertPojo(article); + } + + ArticleRespDTO response = ArticleRespDTO.convertPojo(article); - /** - * 获取文章总评论数 一级评论数加上父评论的总回复数 - */ + /** + * 获取文章总评论数 一级评论数加上父评论的总回复数 + */ - String totalString = "0"; + String totalString = "0"; - totalString = "0"; - Criteria criteria = Criteria.where("journalId").is(article.getId()); - Aggregation agg = Aggregation.newAggregation(Aggregation.match(criteria), // 匹配条件 - Aggregation.group().sum("commentCount").as("totalComment")); - AggregationResults results = mongoTemplate.aggregate(agg, "comment", TotalCommentVo.class); - TotalCommentVo totalCommentVo = results.getUniqueMappedResult(); + totalString = "0"; + Criteria criteria = Criteria.where("journalId").is(article.getId()); + Aggregation agg = Aggregation.newAggregation(Aggregation.match(criteria), // 匹配条件 + Aggregation.group().sum("commentCount").as("totalComment")); + AggregationResults results = mongoTemplate.aggregate(agg, "comment", TotalCommentVo.class); + TotalCommentVo totalCommentVo = results.getUniqueMappedResult(); // commentDTO.setTotalCommentReply("0"); - List list = commentDao.findByJournalId(article.getId()); - int total = 0; - if (null != list && list.size() > 0) { - total = list.size(); - } - if (null != totalCommentVo) { - total = total + totalCommentVo.getTotalComment(); - totalString = total + ""; - if (total > 99) { - totalString = "99+"; - } + List list = commentDao.findByJournalId(article.getId()); + int total = 0; + if (null != list && list.size() > 0) { + total = list.size(); + } + if (null != totalCommentVo) { + total = total + totalCommentVo.getTotalComment(); + totalString = total + ""; + if (total > 99) { + totalString = "99+"; } - response.setTotalCommentReply(Long.valueOf(total)); + } + response.setTotalCommentReply(Long.valueOf(total)); - return Result.success(response); - } + return Result.success(response); } public Result> search(int page, int size) { - List result = new ArrayList<>(); - PageRequest pageRequest = PageRequest.of(page - 1, size); - Specification
articleSpecification = buildSearchSpecification(); - Page
ArticlePage = articleDao.findAll(articleSpecification,pageRequest); - - long totalElements = ArticlePage.getTotalElements(); - List
content = ArticlePage.getContent(); - if (!CollectionUtils.isEmpty(content)) { - for (Article item : content) { - ArticleRespDTO response = ArticleRespDTO.convertPojo(item); - result.add(response); - } - } - return Result.success(new PageResult<>(totalElements, result)); + List pageArticleFromCache = getPageArticleFromCache(page, size); + return Result.success(new PageResult<>(Long.valueOf(pageArticleFromCache.size()), pageArticleFromCache)); } private Specification
buildSearchSpecification() { return (Root
root, CriteriaQuery query, CriteriaBuilder builder) -> { @@ -294,4 +286,40 @@ public class CMSArticleService { String cleanedString = cleanedStringBuilder.toString(); return cleanedString.length() >=150 ? cleanedString.substring(0,150):cleanedString; } + + + private List getPageArticleFromCache(int page , int size){ + + return (List) cacheChannel.get(constants.Constants.J2CACHE_REGION_ARTICLE_PAGE, page+","+size, key -> getPageArticle(page,size), false).getValue(); + } + + private List getPageArticle(int page , int size){ + + List result = new ArrayList<>(); + PageRequest pageRequest = PageRequest.of(page - 1, size); + Specification
articleSpecification = buildSearchSpecification(); + Page
ArticlePage = articleDao.findAll(articleSpecification,pageRequest); + + List
content = ArticlePage.getContent(); + if (!CollectionUtils.isEmpty(content)) { + for (Article item : content) { + ArticleRespDTO response = ArticleRespDTO.convertPojo(item); + result.add(response); + } + } + return result; + } + + private String getArticleFromCache(String id){ + return (String) cacheChannel.get(constants.Constants.J2CACHE_REGION_ARTICLE, id, key -> getArticleById(id), false).getValue(); + } + + private String getArticleById(String id){ + return JSON.toJSONString(articleDao.findById(id).get()); + } + + private void cleanArticleCache(String id){ + cacheChannel.evict(constants.Constants.J2CACHE_REGION_ARTICLE,id); + cacheChannel.clear(constants.Constants.J2CACHE_REGION_ARTICLE_PAGE); + } } diff --git a/luoo_music/src/main/resources/bootstrap.yml b/luoo_music/src/main/resources/bootstrap.yml index 76aac3e..75bc891 100644 --- a/luoo_music/src/main/resources/bootstrap.yml +++ b/luoo_music/src/main/resources/bootstrap.yml @@ -43,6 +43,8 @@ caffeine: journal_song_list: 2000, 365d search_auto_complete: 200000, 365d user_info: 20000, 365d + article: 2000, 365d + article_page: 2000, 365d #caffeine: # properties: caffeine.properties ---