fix article and add cache

main
Revers 8 months ago
parent 931a177355
commit 56821c988e

@ -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_SEARCH_AUTO_COMPLETE = "search_auto_complete";
public static final String J2CACHE_REGION_JOURNAL_HOT_COMMENTS = "journal_hot_comments"; 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"; public static final String RABBIT_MESSAGE_CLEAN_JOURANL_QUERY_PAGE = "clean_journal_query_page";

@ -45,8 +45,8 @@ public class ArticleRespDTO implements Serializable {
@ApiModelProperty(value = "是否允许评论 否0 是1") @ApiModelProperty(value = "是否允许评论 否0 是1")
private String allowCommit; private String allowCommit;
@ApiModelProperty(value = "是否自动推送 否:0 是1") @ApiModelProperty(value = "是否自动推送 否:false 是true")
private String autoPush; private Boolean autoPush;
@ApiModelProperty(value = "总评论数,大于99显示99+") @ApiModelProperty(value = "总评论数,大于99显示99+")
private Long totalCommentReply; private Long totalCommentReply;
@ -76,7 +76,7 @@ public class ArticleRespDTO implements Serializable {
response.setIsScheduled(article.getIsScheduled()); response.setIsScheduled(article.getIsScheduled());
response.setPubTime(article.getPubTime().format(DateTimeFormatter.ISO_DATE_TIME)); response.setPubTime(article.getPubTime().format(DateTimeFormatter.ISO_DATE_TIME));
response.setAllowCommit(article.getAllowCommit()); response.setAllowCommit(article.getAllowCommit());
response.setAutoPush(article.getAutoPush()); response.setAutoPush("1".equals(article.getAutoPush())?true:false);
response.setSongId(article.getSongId()); response.setSongId(article.getSongId());
return response; return response;

@ -2,6 +2,7 @@ package com.luoo.music.service;
import api.PageResult; import api.PageResult;
import api.Result; import api.Result;
import com.alibaba.fastjson.JSON;
import com.luoo.music.client.UserClient; import com.luoo.music.client.UserClient;
import com.luoo.music.dao.*; import com.luoo.music.dao.*;
import com.luoo.music.dto.request.cms.ArticleAddModel; 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.Comment;
import com.luoo.music.pojo.UserInfo; import com.luoo.music.pojo.UserInfo;
import com.luoo.music.util.Constants; import com.luoo.music.util.Constants;
import net.oschina.j2cache.CacheChannel;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example; import org.springframework.data.domain.Example;
@ -55,6 +57,9 @@ public class CMSArticleService {
@Autowired @Autowired
private CommentDao commentDao; private CommentDao commentDao;
@Autowired
private CacheChannel cacheChannel;
public Result add(ArticleAddModel paramAdd) { public Result add(ArticleAddModel paramAdd) {
String image = null; String image = null;
@ -79,6 +84,7 @@ public class CMSArticleService {
if(!articleDao.exists(example)) { if(!articleDao.exists(example)) {
articleDao.save(article); articleDao.save(article);
cleanArticleCache(article.getId());
} }
return Result.success(); return Result.success();
@ -151,31 +157,32 @@ public class CMSArticleService {
} }
public Result deleteById(String id){ public Result deleteById(String id){
Article article = articleDao.findById(id).get(); Article article = JSON.parseObject(getArticleFromCache(id), Article.class);
if (!Objects.isNull(article)) { if (!Objects.isNull(article)) {
article.setIsDeleted("1"); article.setIsDeleted("1");
articleDao.save(article); articleDao.save(article);
cleanArticleCache(article.getId());
return Result.success(); return Result.success();
} }
return Result.failed("文章不存在"); return Result.failed("文章不存在");
} }
public Result visitAdd(String id){ public Result visitAdd(String id){
Article article = articleDao.findById(id).get(); Article article = JSON.parseObject(getArticleFromCache(id), Article.class);
if (!Objects.isNull(article)) { if (!Objects.isNull(article)) {
article.setVisits(article.getVisits()+1); article.setVisits(article.getVisits()+1);
articleDao.save(article); articleDao.save(article);
cleanArticleCache(id);
return Result.success(); return Result.success();
} }
return Result.failed("文章不存在"); return Result.failed("文章不存在");
} }
public Result update(String id,ArticleAddModel param){ public Result update(String id,ArticleAddModel param){
Optional<Article> optional = articleDao.findById(id); Article article = JSON.parseObject(getArticleFromCache(id), Article.class);
if (!optional.isPresent()) { if (article == null) {
return Result.failed("找不到期刊: " + id); return Result.failed("找不到文章: " + id);
} }
Article article = optional.get();
//如果图片路径存在 temp/ 则为新图片 //如果图片路径存在 temp/ 则为新图片
if (StringUtils.isNotBlank(param.getImage()) && param.getImage().contains(Constants.TEMP_KEY_PREFIX)) { if (StringUtils.isNotBlank(param.getImage()) && param.getImage().contains(Constants.TEMP_KEY_PREFIX)) {
@ -207,70 +214,55 @@ public class CMSArticleService {
article.setPubTime(param.getPubTime()); article.setPubTime(param.getPubTime());
articleDao.save(article); articleDao.save(article);
cleanArticleCache(article.getId());
//TODO: 发布方式 //TODO: 发布方式
return Result.success(); return Result.success();
} }
public Result<ArticleRespDTO> findOne(String id) { public Result<ArticleRespDTO> findOne(String id) {
Optional<Article> optional=articleDao.findById(id); Article article = JSON.parseObject(getArticleFromCache(id), Article.class);
if(!optional.isPresent()) {
return Result.failed("无法找到文章: " + id);
}
Article article = optional.get();
if("1".equals(article.getIsDeleted())){ if("1".equals(article.getIsDeleted())){
return Result.success(null); return Result.success(null);
}else { }
ArticleRespDTO response = ArticleRespDTO.convertPojo(article);
ArticleRespDTO response = ArticleRespDTO.convertPojo(article);
/** /**
* *
*/ */
String totalString = "0"; String totalString = "0";
totalString = "0"; totalString = "0";
Criteria criteria = Criteria.where("journalId").is(article.getId()); Criteria criteria = Criteria.where("journalId").is(article.getId());
Aggregation agg = Aggregation.newAggregation(Aggregation.match(criteria), // 匹配条件 Aggregation agg = Aggregation.newAggregation(Aggregation.match(criteria), // 匹配条件
Aggregation.group().sum("commentCount").as("totalComment")); Aggregation.group().sum("commentCount").as("totalComment"));
AggregationResults<TotalCommentVo> results = mongoTemplate.aggregate(agg, "comment", TotalCommentVo.class); AggregationResults<TotalCommentVo> results = mongoTemplate.aggregate(agg, "comment", TotalCommentVo.class);
TotalCommentVo totalCommentVo = results.getUniqueMappedResult(); TotalCommentVo totalCommentVo = results.getUniqueMappedResult();
// commentDTO.setTotalCommentReply("0"); // commentDTO.setTotalCommentReply("0");
List<Comment> list = commentDao.findByJournalId(article.getId()); List<Comment> list = commentDao.findByJournalId(article.getId());
int total = 0; int total = 0;
if (null != list && list.size() > 0) { if (null != list && list.size() > 0) {
total = list.size(); total = list.size();
} }
if (null != totalCommentVo) { if (null != totalCommentVo) {
total = total + totalCommentVo.getTotalComment(); total = total + totalCommentVo.getTotalComment();
totalString = total + ""; totalString = total + "";
if (total > 99) { if (total > 99) {
totalString = "99+"; totalString = "99+";
}
} }
response.setTotalCommentReply(Long.valueOf(total)); }
response.setTotalCommentReply(Long.valueOf(total));
return Result.success(response); return Result.success(response);
}
} }
public Result<PageResult<ArticleRespDTO>> search(int page, int size) { public Result<PageResult<ArticleRespDTO>> search(int page, int size) {
List<ArticleRespDTO> result = new ArrayList<>(); List<ArticleRespDTO> pageArticleFromCache = getPageArticleFromCache(page, size);
PageRequest pageRequest = PageRequest.of(page - 1, size); return Result.success(new PageResult<>(Long.valueOf(pageArticleFromCache.size()), pageArticleFromCache));
Specification<Article> articleSpecification = buildSearchSpecification();
Page<Article> ArticlePage = articleDao.findAll(articleSpecification,pageRequest);
long totalElements = ArticlePage.getTotalElements();
List<Article> 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));
} }
private Specification<Article> buildSearchSpecification() { private Specification<Article> buildSearchSpecification() {
return (Root<Article> root, CriteriaQuery<?> query, CriteriaBuilder builder) -> { return (Root<Article> root, CriteriaQuery<?> query, CriteriaBuilder builder) -> {
@ -294,4 +286,40 @@ public class CMSArticleService {
String cleanedString = cleanedStringBuilder.toString(); String cleanedString = cleanedStringBuilder.toString();
return cleanedString.length() >=150 ? cleanedString.substring(0,150):cleanedString; return cleanedString.length() >=150 ? cleanedString.substring(0,150):cleanedString;
} }
private List<ArticleRespDTO> getPageArticleFromCache(int page , int size){
return (List<ArticleRespDTO>) cacheChannel.get(constants.Constants.J2CACHE_REGION_ARTICLE_PAGE, page+","+size, key -> getPageArticle(page,size), false).getValue();
}
private List<ArticleRespDTO> getPageArticle(int page , int size){
List<ArticleRespDTO> result = new ArrayList<>();
PageRequest pageRequest = PageRequest.of(page - 1, size);
Specification<Article> articleSpecification = buildSearchSpecification();
Page<Article> ArticlePage = articleDao.findAll(articleSpecification,pageRequest);
List<Article> 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);
}
} }

@ -43,6 +43,8 @@ caffeine:
journal_song_list: 2000, 365d journal_song_list: 2000, 365d
search_auto_complete: 200000, 365d search_auto_complete: 200000, 365d
user_info: 20000, 365d user_info: 20000, 365d
article: 2000, 365d
article_page: 2000, 365d
#caffeine: #caffeine:
# properties: caffeine.properties # properties: caffeine.properties
--- ---

Loading…
Cancel
Save