Compare commits

..

No commits in common. '49a0d4cb4ebd438edc02391689782cce2cfa8013' and '931a177355aed7bdaf632d372918d7f82ff9ba7f' have entirely different histories.

@ -28,10 +28,6 @@ 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 J2CACHE_REGION_ADVERTISEMENT = "advertisement";
public static final String J2CACHE_REGION_ADVERTISEMENT_PAGE = "advertisement_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";

@ -1,20 +1,19 @@
package com.luoo.music.dto.response.cms; package com.luoo.music.dto.response.cms;
import com.luoo.music.pojo.Advertisement; import com.luoo.music.pojo.Advertisement;
import com.luoo.music.pojo.Article;
import com.luoo.music.util.Constants; import com.luoo.music.util.Constants;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import lombok.Data; import lombok.Data;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import java.io.Serializable;
/** /**
* @author Revers. * @author Revers.
* @date 2024/02/23 21:33 * @date 2024/02/23 21:33
**/ **/
@Data @Data
public class AdvertisementRespDTO implements Serializable { public class AdvertisementRespDTO {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "ID") @ApiModelProperty(value = "ID")
private String id; private String id;

@ -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 = "是否自动推送 否:false 是true") @ApiModelProperty(value = "是否自动推送 否:0 是1")
private Boolean autoPush; private String 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("1".equals(article.getAutoPush())?true:false); response.setAutoPush(article.getAutoPush());
response.setSongId(article.getSongId()); response.setSongId(article.getSongId());
return response; return response;

@ -2,7 +2,6 @@ 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.AdvertisementDao; import com.luoo.music.dao.AdvertisementDao;
import com.luoo.music.dto.request.cms.AdvertisementReqModel; import com.luoo.music.dto.request.cms.AdvertisementReqModel;
@ -10,7 +9,6 @@ import com.luoo.music.dto.response.cms.AdvertisementRespDTO;
import com.luoo.music.pojo.Advertisement; import com.luoo.music.pojo.Advertisement;
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;
@ -30,6 +28,7 @@ import java.time.LocalDateTime;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
/** /**
* @author Revers. * @author Revers.
@ -47,9 +46,6 @@ public class CMSAdvertisementService {
@Autowired @Autowired
private UserClient userClient; private UserClient userClient;
@Autowired
private CacheChannel cacheChannel;
public Result add(AdvertisementReqModel paramAdd) { public Result add(AdvertisementReqModel paramAdd) {
String image = null; String image = null;
if (StringUtils.isNotBlank(paramAdd.getImage())) { if (StringUtils.isNotBlank(paramAdd.getImage())) {
@ -94,7 +90,6 @@ public class CMSAdvertisementService {
if(!adDao.exists(example)) { if(!adDao.exists(example)) {
adDao.save(ad); adDao.save(ad);
cleanAdCache(ad.getId());
} }
return Result.success(); return Result.success();
@ -114,23 +109,22 @@ public class CMSAdvertisementService {
} }
public Result deleteById(String id){ public Result deleteById(String id){
Advertisement ad = JSON.parseObject(getAdFromCache(id), Advertisement.class); Advertisement ad = adDao.findById(id).get();
if (!Objects.isNull(ad)) { if (!Objects.isNull(ad)) {
ad.setIsDeleted("1"); ad.setIsDeleted("1");
adDao.save(ad); adDao.save(ad);
cleanAdCache(ad.getId());
return Result.success(); return Result.success();
} }
return Result.failed("广告不存在"); return Result.failed("广告不存在");
} }
public Result update(String id,AdvertisementReqModel param){ public Result update(String id,AdvertisementReqModel param){
Advertisement ad = JSON.parseObject(getAdFromCache(id), Advertisement.class); Optional<Advertisement> optional = adDao.findById(id);
if (ad == null) { if (!optional.isPresent()) {
return Result.failed("找不到广告: " + id); return Result.failed("找不到广告: " + id);
} }
Advertisement ad = 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)) {
@ -157,16 +151,16 @@ public class CMSAdvertisementService {
ad.setSummary(contentToSummary(param.getContent())); ad.setSummary(contentToSummary(param.getContent()));
adDao.save(ad); adDao.save(ad);
cleanAdCache(ad.getId());
return Result.success(); return Result.success();
} }
public Result<AdvertisementRespDTO> findOne(String id) { public Result<AdvertisementRespDTO> findOne(String id) {
Advertisement ad = JSON.parseObject(getAdFromCache(id), Advertisement.class); Optional<Advertisement> optional=adDao.findById(id);
if(ad == null) { if(!optional.isPresent()) {
return Result.failed("无法找到广告: " + id); return Result.failed("无法找到广告: " + id);
} }
Advertisement ad = optional.get();
if("1".equals(ad.getIsDeleted())){ if("1".equals(ad.getIsDeleted())){
return Result.success(null); return Result.success(null);
@ -181,8 +175,20 @@ public class CMSAdvertisementService {
} }
public Result<PageResult<AdvertisementRespDTO>> search(int page, int size,String location) { public Result<PageResult<AdvertisementRespDTO>> search(int page, int size,String location) {
List<AdvertisementRespDTO> pageAdFromCache = getPageAdFromCache(page, size, location); List<AdvertisementRespDTO> result = new ArrayList<>();
return Result.success(new PageResult<>(Long.valueOf(pageAdFromCache.size()), pageAdFromCache)); PageRequest pageRequest = PageRequest.of(page - 1, size);
Specification<Advertisement> AdSpecification = buildSearchSpecification(location);
Page<Advertisement> AdPage = adDao.findAll(AdSpecification,pageRequest);
long totalElements = AdPage.getTotalElements();
List<Advertisement> content = AdPage.getContent();
if (!CollectionUtils.isEmpty(content)) {
for (Advertisement item : content) {
AdvertisementRespDTO response = AdvertisementRespDTO.convertPojo(item);
result.add(response);
}
}
return Result.success(new PageResult<>(totalElements, result));
} }
private Specification<Advertisement> buildSearchSpecification(String location) { private Specification<Advertisement> buildSearchSpecification(String location) {
return (Root<Advertisement> root, CriteriaQuery<?> query, CriteriaBuilder builder) -> { return (Root<Advertisement> root, CriteriaQuery<?> query, CriteriaBuilder builder) -> {
@ -222,53 +228,13 @@ public class CMSAdvertisementService {
} }
public Result visitAdd(String id){ public Result visitAdd(String id){
Advertisement ad = JSON.parseObject(getAdFromCache(id), Advertisement.class); Advertisement advertisement = adDao.findById(id).get();
if (!Objects.isNull(advertisement)) {
if (!Objects.isNull(ad)) { advertisement.setVisits(advertisement.getVisits()+1);
ad.setVisits(ad.getVisits()+1); adDao.save(advertisement);
adDao.save(ad);
cleanAdCache(ad.getId());
return Result.success(); return Result.success();
} }
return Result.failed("广告不存在"); return Result.failed("文章不存在");
}
private List<AdvertisementRespDTO> getPageAdFromCache(int page , int size,String location){
return (List<AdvertisementRespDTO>) cacheChannel.get(constants.Constants.J2CACHE_REGION_ADVERTISEMENT_PAGE, page+","+size, key -> getPageAd(page,size,location), false).getValue();
}
private List<AdvertisementRespDTO> getPageAd(int page , int size,String location){
List<AdvertisementRespDTO> result = new ArrayList<>();
PageRequest pageRequest = PageRequest.of(page - 1, size);
Specification<Advertisement> AdSpecification = buildSearchSpecification(location);
Page<Advertisement> AdPage = adDao.findAll(AdSpecification,pageRequest);
List<Advertisement> content = AdPage.getContent();
if (!CollectionUtils.isEmpty(content)) {
for (Advertisement item : content) {
AdvertisementRespDTO response = AdvertisementRespDTO.convertPojo(item);
result.add(response);
}
}
return result;
}
private String getAdFromCache(String id){
return (String) cacheChannel.get(constants.Constants.J2CACHE_REGION_ADVERTISEMENT, id, key -> getAdById(id), false).getValue();
} }
private String getAdById(String id){
return JSON.toJSONString(adDao.findById(id).get());
}
private void cleanAdCache(String id){
cacheChannel.evict(constants.Constants.J2CACHE_REGION_ADVERTISEMENT,id);
cacheChannel.clear(constants.Constants.J2CACHE_REGION_ADVERTISEMENT_PAGE);
}
} }

@ -2,7 +2,6 @@ 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;
@ -12,7 +11,6 @@ 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;
@ -57,9 +55,6 @@ 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;
@ -84,7 +79,6 @@ 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();
@ -157,32 +151,31 @@ public class CMSArticleService {
} }
public Result deleteById(String id){ public Result deleteById(String id){
Article article = JSON.parseObject(getArticleFromCache(id), Article.class); Article article = articleDao.findById(id).get();
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 = JSON.parseObject(getArticleFromCache(id), Article.class); Article article = articleDao.findById(id).get();
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){
Article article = JSON.parseObject(getArticleFromCache(id), Article.class); Optional<Article> optional = articleDao.findById(id);
if (article == null) { if (!optional.isPresent()) {
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)) {
@ -214,55 +207,70 @@ 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) {
Article article = JSON.parseObject(getArticleFromCache(id), Article.class); Optional<Article> optional=articleDao.findById(id);
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) {
total = total + totalCommentVo.getTotalComment();
totalString = total + "";
if (total > 99) {
totalString = "99+";
} }
} if (null != totalCommentVo) {
response.setTotalCommentReply(Long.valueOf(total)); total = total + totalCommentVo.getTotalComment();
totalString = total + "";
if (total > 99) {
totalString = "99+";
}
}
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> pageArticleFromCache = getPageArticleFromCache(page, size); List<ArticleRespDTO> result = new ArrayList<>();
return Result.success(new PageResult<>(Long.valueOf(pageArticleFromCache.size()), pageArticleFromCache)); PageRequest pageRequest = PageRequest.of(page - 1, size);
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) -> {
@ -286,40 +294,4 @@ 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,10 +43,6 @@ 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
advertisement: 2000, 365d
advertisement_page: 2000, 365d
#caffeine: #caffeine:
# properties: caffeine.properties # properties: caffeine.properties
--- ---

Loading…
Cancel
Save