优化缓存初始加载

This commit is contained in:
zhanghan11 2024-04-11 10:22:02 +08:00
parent 6ab8921e25
commit 95d5934a96
5 changed files with 49 additions and 7 deletions

View File

@ -120,7 +120,7 @@ public class IPCPlcDataSyncThread implements Runnable {
String format = simpleDateFormat.format(new Date()); String format = simpleDateFormat.format(new Date());
fields.put("insertTime", format); fields.put("insertTime", format);
// 根据运行状态获取规则 // 根据运行状态获取规则
List<IpcAlarmRulesConfig> rulesConfigList = iIpcAlarmRulesConfigService.selectIpcAlarmRulesConfigList(tags.get("part")); List<IpcAlarmRulesConfig> rulesConfigList = iIpcAlarmRulesConfigService.selectIpcAlarmRulesConfigListByCache(tags.get("part"));
if (IpcConstant.RUNNING_STATUS_RUNNING.equals(status)) { if (IpcConstant.RUNNING_STATUS_RUNNING.equals(status)) {
rulesConfigList = rulesConfigList.stream().filter(ipcAlarmRulesConfig -> ipcAlarmRulesConfig.getType().equals(IpcConstant.RULE_TYPE_IN_RUNNING)).collect(Collectors.toList()); rulesConfigList = rulesConfigList.stream().filter(ipcAlarmRulesConfig -> ipcAlarmRulesConfig.getType().equals(IpcConstant.RULE_TYPE_IN_RUNNING)).collect(Collectors.toList());
} else { } else {

View File

@ -1,6 +1,7 @@
package com.inspur.ipc.service; package com.inspur.ipc.service;
import java.util.List; import java.util.List;
import com.inspur.ipc.domain.IpcAlarmRulesConfig; import com.inspur.ipc.domain.IpcAlarmRulesConfig;
/** /**
@ -9,8 +10,7 @@ import com.inspur.ipc.domain.IpcAlarmRulesConfig;
* @author inspur * @author inspur
* @date 2024-03-30 * @date 2024-03-30
*/ */
public interface IIpcAlarmRulesConfigService public interface IIpcAlarmRulesConfigService {
{
/** /**
* 查询告警规则配置 * 查询告警规则配置
* *
@ -58,11 +58,17 @@ public interface IIpcAlarmRulesConfigService
* @return 结果 * @return 结果
*/ */
public int deleteIpcAlarmRulesConfigById(Long id); public int deleteIpcAlarmRulesConfigById(Long id);
/**
* 更新 redis 缓存
*/
void updateRedisCache();
/** /**
* 获取告警规则缓存 * 获取告警规则缓存
* *
* @param part 监测部位 * @param part 监测部位
* @return 告警规则配置集合 * @return 告警规则配置集合
*/ */
public List<IpcAlarmRulesConfig> selectIpcAlarmRulesConfigList(String part); public List<IpcAlarmRulesConfig> selectIpcAlarmRulesConfigListByCache(String part);
} }

View File

@ -100,6 +100,7 @@ public class IpcAlarmRulesConfigServiceImpl implements IIpcAlarmRulesConfigServi
/** /**
* 更新 redis 缓存 * 更新 redis 缓存
*/ */
@Override
public void updateRedisCache() { public void updateRedisCache() {
// 获取监测部位 // 获取监测部位
List<IpcMonitorField> partList = iIpcMonitorFieldService.selectFieldDataByType(CacheConstants.MONITOR_PART_KEY); List<IpcMonitorField> partList = iIpcMonitorFieldService.selectFieldDataByType(CacheConstants.MONITOR_PART_KEY);
@ -117,7 +118,7 @@ public class IpcAlarmRulesConfigServiceImpl implements IIpcAlarmRulesConfigServi
* @return 告警规则配置集合 * @return 告警规则配置集合
*/ */
@Override @Override
public List<IpcAlarmRulesConfig> selectIpcAlarmRulesConfigList(String part) { public List<IpcAlarmRulesConfig> selectIpcAlarmRulesConfigListByCache(String part) {
List<IpcAlarmRulesConfig> list = redisCache.getCacheObject(CacheConstants.IPC_PREFIX+CacheConstants.RULE_CONFIG + part); List<IpcAlarmRulesConfig> list = redisCache.getCacheObject(CacheConstants.IPC_PREFIX+CacheConstants.RULE_CONFIG + part);
if (list == null || list.isEmpty()) { if (list == null || list.isEmpty()) {
this.updateRedisCache(); this.updateRedisCache();

View File

@ -0,0 +1,35 @@
package com.inspur.ipc.utils;
import com.inspur.common.constant.CacheConstants;
import com.inspur.common.utils.spring.SpringUtils;
import com.inspur.ipc.service.IIpcAlarmRecordService;
import com.inspur.ipc.service.IIpcAlarmRulesConfigService;
import com.inspur.ipc.service.IIpcMonitorFieldService;
import com.inspur.ipc.service.IIpcRunningStatusRecordService;
import com.inspur.system.service.influx.InfluxDBService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
@Configuration
public class IpcRedisCacheConfiguration {
@Autowired
IIpcMonitorFieldService iIpcMonitorFieldService;
@Autowired
IIpcAlarmRulesConfigService iIpcAlarmRulesConfigService;
@Autowired
IIpcRunningStatusRecordService iIpcRunningStatusRecordService;
/**
* 预加载缓存
*/
@PostConstruct
public void preloadedRedisCache(){
iIpcMonitorFieldService.selectFieldDataByType(CacheConstants.MONITOR_PART_KEY);
iIpcMonitorFieldService.selectFieldDataByType(CacheConstants.SENSOR_MONITOR_PARAMS_KEY);
iIpcMonitorFieldService.selectFieldDataByType(CacheConstants.PLC_MONITOR_PARAMS_KEY);
iIpcAlarmRulesConfigService.updateRedisCache();
iIpcRunningStatusRecordService.selectLatestStatus();
}
}

View File

@ -51,7 +51,7 @@ public class RuleUtils {
public List<IpcAlarmRecord> plcDataDeal(Map<String, Object> map, String tag, String type, List<IpcMonitorField> paramList) { public List<IpcAlarmRecord> plcDataDeal(Map<String, Object> map, String tag, String type, List<IpcMonitorField> paramList) {
List<IpcAlarmRecord> returnList = new ArrayList<>(); List<IpcAlarmRecord> returnList = new ArrayList<>();
// 获取判断规则 // 获取判断规则
List<IpcAlarmRulesConfig> rulesConfigList = iIpcAlarmRulesConfigService.selectIpcAlarmRulesConfigList(tag); List<IpcAlarmRulesConfig> rulesConfigList = iIpcAlarmRulesConfigService.selectIpcAlarmRulesConfigListByCache(tag);
try { try {
// 创建ScriptEngineManager和ScriptEngine实例 // 创建ScriptEngineManager和ScriptEngine实例
ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngineManager manager = new ScriptEngineManager();
@ -95,7 +95,7 @@ public class RuleUtils {
public List<IpcAlarmRecord> sensorDataDeal(Map<String, Object> map, String tag, String type, List<IpcMonitorField> paramList) { public List<IpcAlarmRecord> sensorDataDeal(Map<String, Object> map, String tag, String type, List<IpcMonitorField> paramList) {
List<IpcAlarmRecord> returnList = new ArrayList<>(); List<IpcAlarmRecord> returnList = new ArrayList<>();
// 获取判断规则 // 获取判断规则
List<IpcAlarmRulesConfig> rulesConfigList = iIpcAlarmRulesConfigService.selectIpcAlarmRulesConfigList(tag); List<IpcAlarmRulesConfig> rulesConfigList = iIpcAlarmRulesConfigService.selectIpcAlarmRulesConfigListByCache(tag);
try { try {
// 创建ScriptEngineManager和ScriptEngine实例 // 创建ScriptEngineManager和ScriptEngine实例
ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngineManager manager = new ScriptEngineManager();