From 577fd25f8ea178508084acaa6819b7e24b6eebd0 Mon Sep 17 00:00:00 2001 From: lzh Date: Sun, 25 Jan 2026 09:49:29 +0800 Subject: [PATCH] =?UTF-8?q?refactor(iot):=20=E6=B8=85=E7=90=86=E4=BF=9D?= =?UTF-8?q?=E6=B4=81=E9=85=8D=E7=BD=AE=E6=9C=8D=E5=8A=A1=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E5=86=97=E4=BD=99=E7=BC=93=E5=AD=98=E8=AE=BE=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 删除未使用的方法和缓存 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 --- .../CleanOrderIntegrationConfigService.java | 11 --- ...leanOrderIntegrationConfigServiceImpl.java | 67 +------------------ 2 files changed, 2 insertions(+), 76 deletions(-) diff --git a/viewsh-module-iot/viewsh-module-iot-server/src/main/java/com/viewsh/module/iot/service/integration/clean/CleanOrderIntegrationConfigService.java b/viewsh-module-iot/viewsh-module-iot-server/src/main/java/com/viewsh/module/iot/service/integration/clean/CleanOrderIntegrationConfigService.java index 760bf10..51007de 100644 --- a/viewsh-module-iot/viewsh-module-iot-server/src/main/java/com/viewsh/module/iot/service/integration/clean/CleanOrderIntegrationConfigService.java +++ b/viewsh-module-iot/viewsh-module-iot-server/src/main/java/com/viewsh/module/iot/service/integration/clean/CleanOrderIntegrationConfigService.java @@ -12,17 +12,6 @@ import com.viewsh.module.iot.dal.dataobject.integration.clean.CleanOrderIntegrat */ public interface CleanOrderIntegrationConfigService { - /** - * 根据设备ID查询配置(带缓存) - *

- * 优先从 Redis 缓存读取,缓存未命中时从数据库查询并写入缓存 - * 缓存 TTL: 5 分钟 - * - * @param deviceId 设备ID - * @return 集成配置,如果不存在或未启用返回 null - */ - CleanOrderIntegrationConfig getConfigByDeviceId(Long deviceId); - /** * 根据设备ID查询配置包装器(包含完整信息) *

diff --git a/viewsh-module-iot/viewsh-module-iot-server/src/main/java/com/viewsh/module/iot/service/integration/clean/CleanOrderIntegrationConfigServiceImpl.java b/viewsh-module-iot/viewsh-module-iot-server/src/main/java/com/viewsh/module/iot/service/integration/clean/CleanOrderIntegrationConfigServiceImpl.java index 4ce922e..d9caf36 100644 --- a/viewsh-module-iot/viewsh-module-iot-server/src/main/java/com/viewsh/module/iot/service/integration/clean/CleanOrderIntegrationConfigServiceImpl.java +++ b/viewsh-module-iot/viewsh-module-iot-server/src/main/java/com/viewsh/module/iot/service/integration/clean/CleanOrderIntegrationConfigServiceImpl.java @@ -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 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 */