refactor(iot): 清理保洁配置服务中的冗余缓存设计
删除未使用的方法和缓存 key: - getConfigByDeviceId() 方法:未被任何地方调用 - CONFIG_DEVICE_KEY_PATTERN:仅用于已删除的方法 - CONFIG_AREA_KEY_PATTERN:定义了但从未使用 - formatDeviceKey() 和 formatAreaKey():对应方法已删除 简化后的设计只保留两个缓存 key: - CONFIG_WRAPPER_KEY_PATTERN:按 deviceId 缓存完整配置 - CONFIG_AREA_TYPE_KEY_PATTERN:按 areaId + relationType 缓存 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -12,17 +12,6 @@ import com.viewsh.module.iot.dal.dataobject.integration.clean.CleanOrderIntegrat
|
||||
*/
|
||||
public interface CleanOrderIntegrationConfigService {
|
||||
|
||||
/**
|
||||
* 根据设备ID查询配置(带缓存)
|
||||
* <p>
|
||||
* 优先从 Redis 缓存读取,缓存未命中时从数据库查询并写入缓存
|
||||
* 缓存 TTL: 5 分钟
|
||||
*
|
||||
* @param deviceId 设备ID
|
||||
* @return 集成配置,如果不存在或未启用返回 null
|
||||
*/
|
||||
CleanOrderIntegrationConfig getConfigByDeviceId(Long deviceId);
|
||||
|
||||
/**
|
||||
* 根据设备ID查询配置包装器(包含完整信息)
|
||||
* <p>
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package com.viewsh.module.iot.service.integration.clean;
|
||||
|
||||
import com.viewsh.framework.common.util.json.JsonUtils;
|
||||
import com.viewsh.module.iot.dal.dataobject.integration.clean.CleanOrderIntegrationConfig;
|
||||
import com.viewsh.module.iot.dal.dataobject.integration.clean.OpsAreaDeviceRelationDO;
|
||||
import com.viewsh.module.iot.dal.mysql.integration.clean.OpsAreaDeviceRelationMapper;
|
||||
import jakarta.annotation.Resource;
|
||||
@@ -25,8 +24,6 @@ public class CleanOrderIntegrationConfigServiceImpl implements CleanOrderIntegra
|
||||
/**
|
||||
* 配置缓存 Key 模式
|
||||
*/
|
||||
private static final String CONFIG_DEVICE_KEY_PATTERN = "iot:clean:config:device:%s";
|
||||
private static final String CONFIG_AREA_KEY_PATTERN = "iot:clean:config:area:%s";
|
||||
private static final String CONFIG_WRAPPER_KEY_PATTERN = "iot:clean:config:wrapper:%s";
|
||||
private static final String CONFIG_AREA_TYPE_KEY_PATTERN = "iot:clean:config:area:%s:type:%s";
|
||||
|
||||
@@ -48,49 +45,6 @@ public class CleanOrderIntegrationConfigServiceImpl implements CleanOrderIntegra
|
||||
@Resource
|
||||
private StringRedisTemplate stringRedisTemplate;
|
||||
|
||||
@Override
|
||||
public CleanOrderIntegrationConfig getConfigByDeviceId(Long deviceId) {
|
||||
log.debug("[CleanOrderConfig] 查询设备配置:deviceId={}", deviceId);
|
||||
|
||||
// 1. 尝试从 Redis 缓存读取
|
||||
String cacheKey = formatDeviceKey(deviceId);
|
||||
String cachedConfig = stringRedisTemplate.opsForValue().get(cacheKey);
|
||||
|
||||
if (cachedConfig != null) {
|
||||
log.debug("[CleanOrderConfig] 命中缓存:deviceId={}", deviceId);
|
||||
return JsonUtils.parseObject(cachedConfig, CleanOrderIntegrationConfig.class);
|
||||
}
|
||||
|
||||
// 2. 从数据库查询
|
||||
OpsAreaDeviceRelationDO relation = relationMapper.selectByDeviceId(deviceId);
|
||||
|
||||
if (relation == null) {
|
||||
log.debug("[CleanOrderConfig] 设备无关联配置:deviceId={}", deviceId);
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!relation.getEnabled()) {
|
||||
log.debug("[CleanOrderConfig] 设备配置已禁用:deviceId={}", deviceId);
|
||||
return null;
|
||||
}
|
||||
|
||||
CleanOrderIntegrationConfig config = relation.getConfigData();
|
||||
|
||||
// 3. 写入缓存
|
||||
if (config != null) {
|
||||
stringRedisTemplate.opsForValue().set(
|
||||
cacheKey,
|
||||
JsonUtils.toJsonString(config),
|
||||
CONFIG_CACHE_TTL_SECONDS,
|
||||
TimeUnit.SECONDS
|
||||
);
|
||||
}
|
||||
|
||||
log.debug("[CleanOrderConfig] 查询到配置:deviceId={}, areaId={}", deviceId, relation.getAreaId());
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<AreaDeviceConfigWrapper> getConfigsByAreaId(Long areaId) {
|
||||
log.debug("[CleanOrderConfig] 查询区域配置:areaId={}", areaId);
|
||||
@@ -204,17 +158,14 @@ public class CleanOrderIntegrationConfigServiceImpl implements CleanOrderIntegra
|
||||
|
||||
@Override
|
||||
public void evictCache(Long deviceId) {
|
||||
// 清除设备相关的所有缓存
|
||||
stringRedisTemplate.delete(formatDeviceKey(deviceId));
|
||||
// 清除设备配置包装器缓存
|
||||
stringRedisTemplate.delete(formatWrapperKey(deviceId));
|
||||
log.info("[CleanOrderConfig] 清除设备配置缓存:deviceId={}", deviceId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void evictAreaCache(Long areaId) {
|
||||
// 清除区域相关的所有缓存(包括各类型)
|
||||
stringRedisTemplate.delete(formatAreaKey(areaId));
|
||||
// 清除常用类型缓存
|
||||
// 清除区域+类型配置缓存
|
||||
stringRedisTemplate.delete(formatAreaTypeKey(areaId, "TRAFFIC_COUNTER"));
|
||||
stringRedisTemplate.delete(formatAreaTypeKey(areaId, "BEACON"));
|
||||
stringRedisTemplate.delete(formatAreaTypeKey(areaId, "BADGE"));
|
||||
@@ -236,13 +187,6 @@ public class CleanOrderIntegrationConfigServiceImpl implements CleanOrderIntegra
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化设备配置缓存 Key
|
||||
*/
|
||||
private static String formatDeviceKey(Long deviceId) {
|
||||
return String.format(CONFIG_DEVICE_KEY_PATTERN, deviceId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化设备配置包装器缓存 Key
|
||||
*/
|
||||
@@ -250,13 +194,6 @@ public class CleanOrderIntegrationConfigServiceImpl implements CleanOrderIntegra
|
||||
return String.format(CONFIG_WRAPPER_KEY_PATTERN, deviceId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化区域配置缓存 Key
|
||||
*/
|
||||
private static String formatAreaKey(Long areaId) {
|
||||
return String.format(CONFIG_AREA_KEY_PATTERN, areaId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化区域+类型配置缓存 Key
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user