feat(cache): 优化设备模板缓存策略并添加分布式缓存清理功能
- 引入Caffeine本地缓存替代Redis缓存提高性能 - 添加Redis Pub/Sub机制实现多节点缓存同步清理 - 在CsDeviceServiceImpl中注释掉原有的Redis缓存清理逻辑 - 在CsDevModelServiceImpl中添加缓存清理消息发送功能 - 新增ModelCacheClearListener监听器处理缓存清理通知 - 实现IStatService接口的clearModelCache方法 - 添加getModelData方法实现本地缓存优先的数据获取策略 - 配置最大50000条缓存数据,60分钟过期时间
This commit is contained in:
@@ -20,7 +20,6 @@ import com.njcn.access.pojo.po.CsLineModel;
|
||||
import com.njcn.access.service.*;
|
||||
import com.njcn.access.utils.CRC32Utils;
|
||||
import com.njcn.access.utils.JsonUtil;
|
||||
import com.njcn.common.pojo.dto.DeviceLogDTO;
|
||||
import com.njcn.common.pojo.exception.BusinessException;
|
||||
import com.njcn.csdevice.api.CsLineFeignClient;
|
||||
import com.njcn.csdevice.api.DevModelFeignClient;
|
||||
@@ -48,6 +47,7 @@ import com.njcn.system.pojo.vo.DictTreeVO;
|
||||
import com.njcn.web.utils.RequestUtil;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
@@ -88,6 +88,7 @@ public class CsDevModelServiceImpl implements ICsDevModelService {
|
||||
private final DevModelRelationFeignClient devModelRelationFeignClient;
|
||||
private final CsLineFeignClient csLineFeignClient;
|
||||
private final LogMessageTemplate logMessageTemplate;
|
||||
private final StringRedisTemplate stringRedisTemplate;
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = {Exception.class})
|
||||
@@ -143,7 +144,9 @@ public class CsDevModelServiceImpl implements ICsDevModelService {
|
||||
}
|
||||
}
|
||||
//5.清空模板缓存
|
||||
redisUtil.deleteKeysByString(AppRedisKey.DEV_MODEL);
|
||||
//redisUtil.deleteKeysByString(AppRedisKey.DEV_MODEL);
|
||||
//6.通知其他服务节点清理本地缓存
|
||||
stringRedisTemplate.convertAndSend("model_cache_clear", "clear");
|
||||
logMessageTemplate.sendMember(logDto);
|
||||
} catch (Exception e) {
|
||||
logDto.setResult(0);
|
||||
|
||||
@@ -42,6 +42,7 @@ import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@@ -89,6 +90,7 @@ public class CsDeviceServiceImpl implements ICsDeviceService {
|
||||
private final DeviceMessageFeignClient deviceMessageFeignClient;
|
||||
private final CsHarmonicPlanLineFeignClient csHarmonicPlanLineFeignClient;
|
||||
private final LogMessageTemplate logMessageTemplate;
|
||||
private final StringRedisTemplate stringRedisTemplate;
|
||||
|
||||
|
||||
@Override
|
||||
@@ -766,7 +768,7 @@ public class CsDeviceServiceImpl implements ICsDeviceService {
|
||||
try {
|
||||
//删除缓存数据
|
||||
redisUtil.delete(AppRedisKey.MODEL + nDid);
|
||||
redisUtil.deleteKeysByString(AppRedisKey.DEV_MODEL);
|
||||
//redisUtil.deleteKeysByString(AppRedisKey.DEV_MODEL);
|
||||
//询问装置当前所用模板
|
||||
publisher.send("/Pfm/DevCmd/"+version+"/"+nDid,new Gson().toJson(getJson(mid,TypeEnum.TYPE_3.getCode())),1,false);
|
||||
//接收到模板,判断模板是否存在,替换模板,发起接入
|
||||
@@ -837,7 +839,9 @@ public class CsDeviceServiceImpl implements ICsDeviceService {
|
||||
Map<Integer,String> modelMap = new HashMap<>();
|
||||
//删除缓存数据
|
||||
redisUtil.delete(AppRedisKey.MODEL + nDid);
|
||||
redisUtil.deleteKeysByString(AppRedisKey.DEV_MODEL);
|
||||
//redisUtil.deleteKeysByString(AppRedisKey.DEV_MODEL);
|
||||
//通知其他服务节点清理本地缓存
|
||||
stringRedisTemplate.convertAndSend("model_cache_clear", "clear");
|
||||
//询问装置当前所用模板
|
||||
publisher.send("/Pfm/DevCmd/"+version+"/"+nDid,new Gson().toJson(getJson(mid,TypeEnum.TYPE_3.getCode())),1,false);
|
||||
//接收到模板,判断模板是否存在,替换模板,发起接入
|
||||
@@ -899,7 +903,7 @@ public class CsDeviceServiceImpl implements ICsDeviceService {
|
||||
|
||||
Map<Integer, String> modelMap = new HashMap<>();
|
||||
redisUtil.delete(AppRedisKey.MODEL + nDid);
|
||||
redisUtil.deleteKeysByString(AppRedisKey.DEV_MODEL);
|
||||
//redisUtil.deleteKeysByString(AppRedisKey.DEV_MODEL);
|
||||
publisher.send("/Pfm/DevCmd/"+version+"/"+nDid, new Gson().toJson(getJson(mid,TypeEnum.TYPE_3.getCode())), 1, false);
|
||||
try {
|
||||
Thread.sleep(2000);
|
||||
|
||||
Reference in New Issue
Block a user