feat(foundations):完善区域相关缓存更新的功能

This commit is contained in:
JIAN
2024-08-25 16:41:55 +08:00
parent 0d694db8b8
commit 179bc9560f
4 changed files with 68 additions and 35 deletions

View File

@@ -107,7 +107,6 @@ public class RegionController {
@PutMapping("/refreshRegionRelateCaches/{id}")
@ApiOperation("刷新区域相关缓存")
public void refreshRegionRelateCaches(@PathVariable("id") Long id) {
//todo
// homeService.refreshRegionRelateCaches(id);
regionService.refreshRegionRelateCaches(id);
}
}
}

View File

@@ -1,5 +1,6 @@
package com.jzo2o.foundations.handler;
import com.jzo2o.api.foundations.dto.response.RegionSimpleResDTO;
import com.jzo2o.foundations.constants.RedisConstants;
import com.jzo2o.foundations.service.IRegionService;
import com.xxl.job.core.handler.annotation.XxlJob;
@@ -8,6 +9,7 @@ import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.List;
/**
* xxl-job定时任务执行器
@@ -15,21 +17,25 @@ import javax.annotation.Resource;
*/
@Slf4j
@Component
@SuppressWarnings("unused")
public class SpringCacheSyncHandler {
@Resource
private RedisTemplate<String, Object> redisTemplate;
@Resource
private IRegionService regionService;
@XxlJob("activeRegionCacheSync")
public void activeRegionCacheSync() {
log.info(">>>>>>>>开始进行缓存同步,更新已启用区域");
// 清理缓存
String key = RedisConstants.CacheName.JZ_CACHE + "::ACTIVE_REGIONS";
redisTemplate.delete(key);
// 添加缓存
regionService.queryActiveRegionListCache();
log.info(">>>>>>>>开始进行缓存同步,更新已启用区域");
// 清理开通服务区域缓存
redisTemplate.delete(RedisConstants.CacheName.JZ_CACHE + "::ACTIVE_REGIONS");
// 更新开通服务区域缓存
List<RegionSimpleResDTO> regionList = regionService.queryActiveRegionListCache();
// 更新每个区域对应的缓存
regionList.forEach(region -> regionService.refreshRegionRelateCaches(region.getId()));
log.info(">>>>>>>>缓存同步结束,完成更新已启用区域");
}
@Resource
private RedisTemplate<String, Object> redisTemplate;
@Resource
private IRegionService regionService;
}

View File

@@ -6,7 +6,6 @@ import com.jzo2o.common.model.PageResult;
import com.jzo2o.foundations.model.domain.Region;
import com.jzo2o.foundations.model.dto.request.RegionPageQueryReqDTO;
import com.jzo2o.foundations.model.dto.request.RegionUpsertReqDTO;
import com.jzo2o.foundations.model.dto.response.RegionDisplayResDTO;
import com.jzo2o.foundations.model.dto.response.RegionResDTO;
import java.util.List;
@@ -61,8 +60,9 @@ public interface IRegionService extends IService<Region> {
* 区域启用
*
* @param id 区域id
* @return 用于更新缓存信息
*/
void active(Long id);
List<RegionSimpleResDTO> active(Long id);
/**
* 区域禁用
@@ -78,4 +78,9 @@ public interface IRegionService extends IService<Region> {
*/
List<RegionSimpleResDTO> queryActiveRegionListCache();
}
/**
* 刷新区域相关的缓存(首页图标、热门服务、服务类型)
* @param id 区域id
*/
void refreshRegionRelateCaches(Long id);
}

View File

@@ -23,8 +23,11 @@ import com.jzo2o.foundations.model.dto.request.RegionUpsertReqDTO;
import com.jzo2o.foundations.model.dto.response.RegionResDTO;
import com.jzo2o.foundations.service.IConfigRegionService;
import com.jzo2o.foundations.service.IRegionService;
import com.jzo2o.foundations.service.IServeService;
import com.jzo2o.foundations.service.IServeTypeService;
import com.jzo2o.mysql.utils.PageUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
@@ -47,6 +50,10 @@ public class RegionServiceImpl extends ServiceImpl<RegionMapper, Region> impleme
private CityDirectoryMapper cityDirectoryMapper;
@Resource
private ServeMapper serveMapper;
@Resource
private IServeService serveService;
@Resource
private IServeTypeService serveTypeService;
/**
* 区域新增
@@ -151,18 +158,18 @@ public class RegionServiceImpl extends ServiceImpl<RegionMapper, Region> impleme
@CacheEvict(value = RedisConstants.CacheName.JZ_CACHE, key = "'ACTIVE_REGIONS'", beforeInvocation = true),
@CacheEvict(value = RedisConstants.CacheName.SERVE_ICON, key = "#id", beforeInvocation = true),
@CacheEvict(value = RedisConstants.CacheName.HOT_SERVE, key = "#id", beforeInvocation = true),
@CacheEvict(value = RedisConstants.CacheName.SERVE_TYPE, key = "#id", beforeInvocation = true)
})
public void active(Long id) {
//区域信息
@CacheEvict(value = RedisConstants.CacheName.SERVE_TYPE, key = "#id", beforeInvocation = true)},
put = @CachePut(value = RedisConstants.CacheName.JZ_CACHE, key = "'ACTIVE_REGIONS'"))
public List<RegionSimpleResDTO> active(Long id) {
// 区域信息
Region region = baseMapper.selectById(id);
//启用状态
// 启用状态
Integer activeStatus = region.getActiveStatus();
//草稿或禁用状态方可启用
// 草稿或禁用状态方可启用
if (!(FoundationStatusEnum.INIT.getStatus() == activeStatus || FoundationStatusEnum.DISABLE.getStatus() == activeStatus)) {
throw new ForbiddenOperationException("草稿或禁用状态方可启用");
}
//如果需要启用区域,需要校验该区域下是否有上架的服务
// 如果需要启用区域,需要校验该区域下是否有上架的服务
LambdaQueryWrapper<Serve> queryWrapper = Wrappers.<Serve>lambdaQuery()
.eq(Serve::getRegionId, id)
.eq(Serve::getSaleStatus, FoundationStatusEnum.ENABLE.getStatus());
@@ -170,14 +177,16 @@ public class RegionServiceImpl extends ServiceImpl<RegionMapper, Region> impleme
throw new ForbiddenOperationException("该区域没有上架的服务不可启用");
}
//更新启用状态
// 更新启用状态
LambdaUpdateWrapper<Region> updateWrapper = Wrappers.<Region>lambdaUpdate()
.eq(Region::getId, id)
.set(Region::getActiveStatus, FoundationStatusEnum.ENABLE.getStatus());
update(updateWrapper);
//3.如果是启用操作,刷新缓存:启用区域列表、首页图标、热门服务、服务类型
// todo
// 刷新区域相关缓存
this.refreshRegionRelateCaches(id);
// 通过返回值更新开通服务区域缓存
return queryActiveRegionList();
}
/**
@@ -190,19 +199,18 @@ public class RegionServiceImpl extends ServiceImpl<RegionMapper, Region> impleme
@CacheEvict(value = RedisConstants.CacheName.JZ_CACHE, key = "'ACTIVE_REGIONS'", beforeInvocation = true),
@CacheEvict(value = RedisConstants.CacheName.SERVE_ICON, key = "#id", beforeInvocation = true),
@CacheEvict(value = RedisConstants.CacheName.HOT_SERVE, key = "#id", beforeInvocation = true),
@CacheEvict(value = RedisConstants.CacheName.SERVE_TYPE, key = "#id", beforeInvocation = true)
})
@CacheEvict(value = RedisConstants.CacheName.SERVE_TYPE, key = "#id", beforeInvocation = true)})
public void deactivate(Long id) {
//区域信息
// 区域信息
Region region = baseMapper.selectById(id);
//启用状态
// 启用状态
Integer activeStatus = region.getActiveStatus();
//启用状态方可禁用
// 启用状态方可禁用
if (!(FoundationStatusEnum.ENABLE.getStatus() == activeStatus)) {
throw new ForbiddenOperationException("启用状态方可禁用");
}
//如果禁用区域下有上架的服务则无法禁用
// 如果禁用区域下有上架的服务则无法禁用
LambdaQueryWrapper<Serve> queryWrapper = Wrappers.<Serve>lambdaQuery()
.eq(Serve::getRegionId, id)
.eq(Serve::getSaleStatus, FoundationStatusEnum.ENABLE.getStatus());
@@ -210,7 +218,7 @@ public class RegionServiceImpl extends ServiceImpl<RegionMapper, Region> impleme
throw new ForbiddenOperationException("该区域存在上架的服务不可禁用");
}
//更新禁用状态
// 更新禁用状态
LambdaUpdateWrapper<Region> updateWrapper = Wrappers.<Region>lambdaUpdate()
.eq(Region::getId, id)
.set(Region::getActiveStatus, FoundationStatusEnum.DISABLE.getStatus());
@@ -228,4 +236,19 @@ public class RegionServiceImpl extends ServiceImpl<RegionMapper, Region> impleme
return queryActiveRegionList();
}
@Override
@Caching(evict = {
@CacheEvict(value = RedisConstants.CacheName.SERVE_ICON, key = "#id", beforeInvocation = true),
@CacheEvict(value = RedisConstants.CacheName.HOT_SERVE, key = "#id", beforeInvocation = true),
@CacheEvict(value = RedisConstants.CacheName.SERVE_TYPE, key = "#id", beforeInvocation = true)})
public void refreshRegionRelateCaches(Long id) {
// 更新每个区域对应的首页服务列表
serveService.getFirstPageServeList(id);
// 更新每个区域对应的服务类型列表
serveTypeService.getServeTypeList(id);
// 更新每个区域对应的热门服务列表
serveService.getHotServeList(id);
}
}