1. add article timed release

2. fix article reset cache
main
Revers 4 months ago
parent 694250d313
commit 83d70a3e2e

@ -0,0 +1,43 @@
package com.luoo.music.config;
import com.luoo.music.service.CMSArticleService;
import com.luoo.music.util.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import java.time.LocalDateTime;
import java.util.Map;
/**
* @author Revers.
* @date 2024/05/27 20:32
* 广
**/
@Configuration
//@EnableScheduling
public class TimeReleaseScheduleTask {
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private CMSArticleService cmsArticleService;
//每分钟的第30秒执行一次
@Scheduled(cron = "30 * * * * ?")
private void timeRelease() {
Map<String, LocalDateTime> schedlueMap = redisTemplate.opsForHash().entries(Constants.REDIS_KEY_ARTICLE_SCHEDULED);
if(schedlueMap != null && schedlueMap.size() > 0){
for (String key : schedlueMap.keySet()){
LocalDateTime scheduleTime = schedlueMap.get(key);
if(scheduleTime.isBefore(LocalDateTime.now())){
cmsArticleService.releaseArticle(key);
}
}
}
}
}

@ -33,7 +33,7 @@ public class ArticleRespDTO implements Serializable {
private String userId;
@ApiModelProperty(value = "发布作者昵称")
private String userName;
@ApiModelProperty(value = "编辑日期,格式为: yyyy-MM-dd HH:mm:ss")
@ApiModelProperty(value = "发布时间,格式为: yyyy-MM-dd HH:mm:ss")
private String date;
@ApiModelProperty(value = "是否定时发布 否0 是1")
@ -70,13 +70,13 @@ public class ArticleRespDTO implements Serializable {
response.setContent(article.getContent());
response.setUserId(article.getUserId());
response.setUserName(article.getUserName());
response.setDate(article.getUpdateTime().format(DateTimeFormatter.ISO_DATE_TIME));
response.setDate(article.getPubTime().format(DateTimeFormatter.ISO_DATE_TIME));
response.setTotalCommentReply(article.getComment());
response.setVistisNum(article.getVisits());
response.setIsScheduled(article.getIsScheduled());
response.setPubTime(article.getPubTime().format(DateTimeFormatter.ISO_DATE_TIME));
response.setAllowCommit(article.getAllowCommit());
response.setAutoPush("1".equals(article.getAutoPush())?true:false);
response.setAutoPush("1".equals(article.getAutoPush()));
response.setSongId(article.getSongId());
return response;

@ -1,17 +0,0 @@
package com.luoo.music.service;
import com.luoo.music.dao.ArticleDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @author Revers.
* @date 2024/02/21 16:32
**/
@Service
public class ArticleService {
@Autowired
private ArticleDao articleDao;
}

@ -77,11 +77,12 @@ public class CMSAdvertisementService {
ad.setIsDeleted("0");
UserInfo userInfo = userClient.queryUserInfoById(paramAdd.getUserId());
if (!Objects.isNull(userInfo)) {
ad.setUserId(userInfo.getId());
ad.setUserName(userInfo.getName());
ad.setUserType(userInfo.getType());
}
if (Objects.isNull(userInfo))
return Result.failed("发布人ID无效");
ad.setUserId(userInfo.getId());
ad.setUserName(userInfo.getName());
ad.setUserType(userInfo.getType());
Example<Advertisement> example = Example.of(ad, ExampleMatcher.matching()
@ -92,10 +93,8 @@ public class CMSAdvertisementService {
.withIgnorePaths("updateTime")
);
if(!adDao.exists(example)) {
adDao.save(ad);
cleanAdCache(ad.getId());
}
adDao.save(ad);
cleanAdCache(ad.getId());
return Result.success();
}
@ -233,7 +232,6 @@ public class CMSAdvertisementService {
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();
@ -255,8 +253,6 @@ public class CMSAdvertisementService {
}
return result;
}
private String getAdFromCache(String id){
return (String) cacheChannel.get(constants.Constants.J2CACHE_REGION_ADVERTISEMENT, id, key -> getAdById(id), false).getValue();
}
@ -270,5 +266,4 @@ public class CMSAdvertisementService {
cacheChannel.clear(constants.Constants.J2CACHE_REGION_ADVERTISEMENT_PAGE);
}
}

@ -16,15 +16,13 @@ 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;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.*;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import util.IdWorker;
@ -34,6 +32,7 @@ import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.*;
/**
@ -61,6 +60,9 @@ public class CMSArticleService {
@Autowired
private CacheChannel cacheChannel;
@Autowired
private RedisTemplate redisTemplate;
public Result add(ArticleAddModel paramAdd) {
String image = null;
@ -73,25 +75,6 @@ public class CMSArticleService {
}
}
Article article = buildArticle(paramAdd,image);
Example<Article> example = Example.of(article, ExampleMatcher.matching()
.withIgnorePaths("id")
.withIgnorePaths("image")
.withIgnorePaths("pubTime")
.withIgnorePaths("createTime")
.withIgnorePaths("updateTime")
);
if(!articleDao.exists(example)) {
articleDao.save(article);
cleanArticleCache(article.getId());
}
return Result.success();
}
private Article buildArticle(ArticleAddModel paramAdd,String image){
Article article = new Article();
article.setId(String.valueOf(idWorker.nextId()));
article.setTitle(paramAdd.getTitle());
@ -105,43 +88,53 @@ public class CMSArticleService {
UserInfo userInfo = userClient.queryUserInfoById(paramAdd.getUserId());
if (!Objects.isNull(userInfo)) {
article.setUserId(userInfo.getId());
article.setUserName(userInfo.getName());
article.setUserType(userInfo.getType());
}
if (Objects.isNull(userInfo))
return Result.failed("发布人ID无效");
article.setUserId(userInfo.getId());
article.setUserName(userInfo.getName());
article.setUserType(userInfo.getType());
article.setAllowCommit(paramAdd.getAllowCommit());
article.setAutoPush(paramAdd.getAutoPush()?"1":"0");
article.setIsPublish("1");
article.setPubTime(LocalDateTime.now());
article.setIsScheduled(paramAdd.getIsScheduled());
/* String pubTimeStr = paramAdd.getPubTime();
LocalDateTime pubTime = LocalDateTime.now();
if ("1".equals(paramAdd.getIsScheduled())) { // 定时发布
if (StringUtils.isNotBlank(pubTimeStr)) {// 定时发布确定发布时间
pubTime = LocalDateTime.parse(pubTimeStr, Constants.formatter);
if(pubTime.isBefore(LocalDateTime.now())){ //发布时间比现在早
article.setState("1");
article.setIsPublish("1");
article.setPubTime(LocalDateTime.now());
}else {//发布时间比现在晚
article.setState("0");
article.setIsPublish("0");
article.setPubTime(pubTime);
}
}else { // 定时发布未确定发布时间
article.setState("1");
if(paramAdd.getAutoPush()){
article.setAutoPush("1");
article.setIsPublish("1");
article.setPubTime(LocalDateTime.now());
article.setIsScheduled(paramAdd.getIsScheduled());
}else {
if (paramAdd.getPubTime() == null)
return Result.failed("定时发布但发布时间为空");
if(paramAdd.getPubTime().isBefore(LocalDateTime.now())){
article.setAutoPush("0");
article.setIsPublish("1");
article.setPubTime(LocalDateTime.now());
article.setIsScheduled("1");
}else {
article.setAutoPush("0");
article.setIsPublish("0");
article.setPubTime(paramAdd.getPubTime());
article.setIsScheduled("0");
redisTemplate.opsForHash().put(Constants.REDIS_KEY_ARTICLE_SCHEDULED,article.getId(),paramAdd.getPubTime());
}
} else { //非定时发布
article.setState("1");
article.setIsPublish("1");
article.setPubTime(LocalDateTime.now());
}*/
return article;
}
Example<Article> example = Example.of(article, ExampleMatcher.matching()
.withIgnorePaths("id")
.withIgnorePaths("image")
.withIgnorePaths("pubTime")
.withIgnorePaths("createTime")
.withIgnorePaths("updateTime")
);
if(!articleDao.exists(example)) {
articleDao.save(article);
cleanArticleCache(article.getId());
}else {
redisTemplate.opsForHash().delete(Constants.REDIS_KEY_ARTICLE_SCHEDULED,article.getId());
}
return Result.success();
}
private String moveArticleImage(String srcKey, String image) {
@ -163,6 +156,7 @@ public class CMSArticleService {
article.setIsDeleted("1");
articleDao.save(article);
cleanArticleCache(article.getId());
redisTemplate.opsForHash().delete(Constants.REDIS_KEY_ARTICLE_SCHEDULED,article.getId());
return Result.success();
}
return Result.failed("文章不存在");
@ -199,24 +193,38 @@ public class CMSArticleService {
}
}
article.setTitle(param.getTitle());
article.setType(param.getType());
article.setSummary(contentToSummary(param.getContent()));
article.setContent(param.getContent());
article.setTitle(param.getTitle());
article.setType(param.getType());
article.setSummary(contentToSummary(param.getContent()));
article.setContent(param.getContent());
article.setAllowCommit(param.getAllowCommit());
article.setAllowCommit(param.getAllowCommit());
article.setAutoPush(param.getAutoPush()?"1":"0");
article.setIsScheduled(param.getIsScheduled());
redisTemplate.opsForHash().delete(Constants.REDIS_KEY_ARTICLE_SCHEDULED,article.getId());
if(param.getAutoPush()){
article.setAutoPush("1");
article.setIsPublish("1");
article.setPubTime(LocalDateTime.now());
article.setIsScheduled("1");
}else {
if (param.getPubTime() == null)
return Result.failed("定时发布但发布时间为空");
article.setPubTime(param.getPubTime());
if(param.getPubTime().isBefore(LocalDateTime.now())){
article.setAutoPush("0");
article.setIsPublish("1");
article.setPubTime(LocalDateTime.now());
article.setIsScheduled("1");
}else {
article.setAutoPush("0");
article.setIsPublish("0");
article.setPubTime(param.getPubTime());
article.setIsScheduled("0");
redisTemplate.opsForHash().put(Constants.REDIS_KEY_ARTICLE_SCHEDULED,article.getId(),param.getPubTime());
}
}
articleDao.save(article);
cleanArticleCache(article.getId());
//TODO: 发布方式
return Result.success();
}
@ -270,12 +278,20 @@ public class CMSArticleService {
return Result.success(new PageResult<>(Long.valueOf(pageArticleFromCache.size()), pageArticleFromCache));
}
private Specification<Article> buildSearchSpecification() {
//isFront : true 前台数据只显示Publish的
//isFront false 后台数据,全都显示
private Specification<Article> buildSearchSpecification(boolean isFrost) {
return (Root<Article> root, CriteriaQuery<?> query, CriteriaBuilder builder) -> {
List<Predicate> predicateList = new ArrayList<Predicate>();
Predicate isDeleted = builder.equal(root.get("isDeleted"), "0");
Predicate isPublish = builder.equal(root.get("isPublish"), "1");
predicateList.add(builder.and(isDeleted, isPublish));
if(isFrost){
Predicate isPublish = builder.equal(root.get("isPublish"), "1");
predicateList.add(builder.and(isDeleted, isPublish));
}else {
predicateList.add(builder.and(isDeleted));
}
return builder.and(predicateList.toArray(new Predicate[predicateList.size()]));
};
}
@ -293,14 +309,23 @@ public class CMSArticleService {
return cleanedString.length() >=150 ? cleanedString.substring(0,150):cleanedString;
}
public void releaseArticle(String articleId){
Article article = JSON.parseObject(getArticleFromCache(articleId), Article.class);
if (article != null) {
article.setIsPublish("1");
article.setIsScheduled("1");
}
articleDao.save(article);
cleanArticleCache(article.getId());
redisTemplate.opsForHash().delete(Constants.REDIS_KEY_ARTICLE_SCHEDULED,article.getId());
}
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<ArticleDTO> getPageArticleDTOFromCache(int page , int size){
return (List<ArticleDTO>) cacheChannel.get(constants.Constants.J2CACHE_REGION_ARTICLE_DTO_PAGE, page+","+size, key -> getPageArticleDTO(page,size), false).getValue();
}
@ -308,8 +333,8 @@ public class CMSArticleService {
private List<ArticleRespDTO> getPageArticle(int page , int size){
List<ArticleRespDTO> result = new ArrayList<>();
PageRequest pageRequest = PageRequest.of(page - 1, size);
Specification<Article> articleSpecification = buildSearchSpecification();
PageRequest pageRequest = PageRequest.of(page - 1, size, Sort.by("pubTime").descending());
Specification<Article> articleSpecification = buildSearchSpecification(false);
Page<Article> ArticlePage = articleDao.findAll(articleSpecification,pageRequest);
List<Article> content = ArticlePage.getContent();
@ -325,8 +350,8 @@ public class CMSArticleService {
public List<ArticleDTO> getPageArticleDTO(int page,int size) {
List<ArticleDTO> result = new ArrayList<>();
PageRequest pageRequest = PageRequest.of(page - 1, size);
Specification<Article> articleSpecification = buildSearchSpecification();
PageRequest pageRequest = PageRequest.of(page - 1, size, Sort.by("pubTime").descending());
Specification<Article> articleSpecification = buildSearchSpecification(true);
Page<Article> ArticlePage = articleDao.findAll(articleSpecification,pageRequest);
List<Article> content = ArticlePage.getContent();
@ -337,7 +362,6 @@ public class CMSArticleService {
}
}
return result;
}
private String getArticleFromCache(String id){
@ -351,5 +375,6 @@ public class CMSArticleService {
private void cleanArticleCache(String id){
cacheChannel.evict(constants.Constants.J2CACHE_REGION_ARTICLE,id);
cacheChannel.clear(constants.Constants.J2CACHE_REGION_ARTICLE_PAGE);
cacheChannel.clear(constants.Constants.J2CACHE_REGION_ARTICLE_DTO_PAGE);
}
}

@ -40,4 +40,7 @@ public class Constants {
public static final String COMMA = ",";
// _ 下划线
public static final String UNDERSCORE = "_";
public static final String REDIS_KEY_ARTICLE_SCHEDULED = "redis_key_article_scheduled";
}

Loading…
Cancel
Save