add advertisement cache

main
Revers 10 months ago
parent 56821c988e
commit 49a0d4cb4e

@ -30,6 +30,8 @@ public class Constants {
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";

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

@ -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.AdvertisementDao;
import com.luoo.music.dto.request.cms.AdvertisementReqModel;
@ -9,6 +10,7 @@ import com.luoo.music.dto.response.cms.AdvertisementRespDTO;
import com.luoo.music.pojo.Advertisement;
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;
@ -28,7 +30,6 @@ import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
/**
* @author Revers.
@ -46,6 +47,9 @@ public class CMSAdvertisementService {
@Autowired
private UserClient userClient;
@Autowired
private CacheChannel cacheChannel;
public Result add(AdvertisementReqModel paramAdd) {
String image = null;
if (StringUtils.isNotBlank(paramAdd.getImage())) {
@ -90,6 +94,7 @@ public class CMSAdvertisementService {
if(!adDao.exists(example)) {
adDao.save(ad);
cleanAdCache(ad.getId());
}
return Result.success();
@ -109,22 +114,23 @@ public class CMSAdvertisementService {
}
public Result deleteById(String id){
Advertisement ad = adDao.findById(id).get();
Advertisement ad = JSON.parseObject(getAdFromCache(id), Advertisement.class);
if (!Objects.isNull(ad)) {
ad.setIsDeleted("1");
adDao.save(ad);
cleanAdCache(ad.getId());
return Result.success();
}
return Result.failed("广告不存在");
}
public Result update(String id,AdvertisementReqModel param){
Optional<Advertisement> optional = adDao.findById(id);
if (!optional.isPresent()) {
Advertisement ad = JSON.parseObject(getAdFromCache(id), Advertisement.class);
if (ad == null) {
return Result.failed("找不到广告: " + id);
}
Advertisement ad = optional.get();
//如果图片路径存在 temp/ 则为新图片
if (StringUtils.isNotBlank(param.getImage()) && param.getImage().contains(Constants.TEMP_KEY_PREFIX)) {
@ -151,16 +157,16 @@ public class CMSAdvertisementService {
ad.setSummary(contentToSummary(param.getContent()));
adDao.save(ad);
cleanAdCache(ad.getId());
return Result.success();
}
public Result<AdvertisementRespDTO> findOne(String id) {
Optional<Advertisement> optional=adDao.findById(id);
if(!optional.isPresent()) {
Advertisement ad = JSON.parseObject(getAdFromCache(id), Advertisement.class);
if(ad == null) {
return Result.failed("无法找到广告: " + id);
}
Advertisement ad = optional.get();
if("1".equals(ad.getIsDeleted())){
return Result.success(null);
@ -175,20 +181,8 @@ public class CMSAdvertisementService {
}
public Result<PageResult<AdvertisementRespDTO>> search(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);
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));
List<AdvertisementRespDTO> pageAdFromCache = getPageAdFromCache(page, size, location);
return Result.success(new PageResult<>(Long.valueOf(pageAdFromCache.size()), pageAdFromCache));
}
private Specification<Advertisement> buildSearchSpecification(String location) {
return (Root<Advertisement> root, CriteriaQuery<?> query, CriteriaBuilder builder) -> {
@ -228,13 +222,53 @@ public class CMSAdvertisementService {
}
public Result visitAdd(String id){
Advertisement advertisement = adDao.findById(id).get();
if (!Objects.isNull(advertisement)) {
advertisement.setVisits(advertisement.getVisits()+1);
adDao.save(advertisement);
Advertisement ad = JSON.parseObject(getAdFromCache(id), Advertisement.class);
if (!Objects.isNull(ad)) {
ad.setVisits(ad.getVisits()+1);
adDao.save(ad);
cleanAdCache(ad.getId());
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);
}
}

@ -45,6 +45,8 @@ caffeine:
user_info: 20000, 365d
article: 2000, 365d
article_page: 2000, 365d
advertisement: 2000, 365d
advertisement_page: 2000, 365d
#caffeine:
# properties: caffeine.properties
---

Loading…
Cancel
Save