You've already forked FrameTour-BE
refactor(repository): 移除设备和景点相关缓存逻辑
- 删除了 DeviceRepository 和 ScenicRepository 中的缓存相关代码 - 移除了成功结果缓存和错误降级逻辑 -简化了设备和景点信息获取方法,直接调用服务接口返回结果
This commit is contained in:
@@ -31,8 +31,6 @@ public class DeviceRepository {
|
|||||||
public static final String DEVICE_ONLINE_CACHE_KEY = "device:online_status:%s";
|
public static final String DEVICE_ONLINE_CACHE_KEY = "device:online_status:%s";
|
||||||
public static final String DEVICE_CACHE_KEY = "device:%s";
|
public static final String DEVICE_CACHE_KEY = "device:%s";
|
||||||
public static final String DEVICE_CONFIG_CACHE_KEY = "device:%s:config";
|
public static final String DEVICE_CONFIG_CACHE_KEY = "device:%s:config";
|
||||||
public static final String DEVICE_CACHE_SUCCESS_KEY = "device:success:%s";
|
|
||||||
public static final String DEVICE_NO_CACHE_SUCCESS_KEY = "device:success:no:%s";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将DeviceV2DTO转换为DeviceEntity
|
* 将DeviceV2DTO转换为DeviceEntity
|
||||||
@@ -60,52 +58,16 @@ public class DeviceRepository {
|
|||||||
|
|
||||||
public DeviceEntity getDevice(Long deviceId) {
|
public DeviceEntity getDevice(Long deviceId) {
|
||||||
log.debug("获取设备信息, deviceId: {}", deviceId);
|
log.debug("获取设备信息, deviceId: {}", deviceId);
|
||||||
try {
|
DeviceV2DTO deviceDto = deviceIntegrationService.getDevice(deviceId);
|
||||||
DeviceV2DTO deviceDto = deviceIntegrationService.getDevice(deviceId);
|
DeviceEntity device = convertToEntity(deviceDto);
|
||||||
DeviceEntity device = convertToEntity(deviceDto);
|
return device;
|
||||||
if (device != null) {
|
|
||||||
// 存储到常规缓存
|
|
||||||
redisTemplate.opsForValue().set(String.format(DEVICE_CACHE_KEY, deviceId), JacksonUtil.toJSONString(device), 3, TimeUnit.DAYS);
|
|
||||||
// 存储到成功结果缓存,用于失败时降级
|
|
||||||
redisTemplate.opsForValue().set(String.format(DEVICE_CACHE_SUCCESS_KEY, deviceId), JacksonUtil.toJSONString(device), 7, TimeUnit.DAYS);
|
|
||||||
}
|
|
||||||
return device;
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.warn("调用zt-device服务失败, deviceId: {}, 尝试返回缓存结果", deviceId, e);
|
|
||||||
String cachedDevice = redisTemplate.opsForValue().get(String.format(DEVICE_CACHE_SUCCESS_KEY, deviceId));
|
|
||||||
if (cachedDevice != null) {
|
|
||||||
log.info("返回缓存的设备信息, deviceId: {}", deviceId);
|
|
||||||
return JacksonUtil.parseObject(cachedDevice, DeviceEntity.class);
|
|
||||||
}
|
|
||||||
log.error("无法获取设备信息且无缓存数据, deviceId: {}", deviceId);
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public DeviceEntity getDeviceByDeviceNo(String deviceNo) {
|
public DeviceEntity getDeviceByDeviceNo(String deviceNo) {
|
||||||
log.debug("根据设备编号获取设备信息, deviceNo: {}", deviceNo);
|
log.debug("根据设备编号获取设备信息, deviceNo: {}", deviceNo);
|
||||||
try {
|
DeviceV2DTO deviceDto = deviceIntegrationService.getDeviceByNo(deviceNo);
|
||||||
DeviceV2DTO deviceDto = deviceIntegrationService.getDeviceByNo(deviceNo);
|
DeviceEntity device = convertToEntity(deviceDto);
|
||||||
DeviceEntity device = convertToEntity(deviceDto);
|
return device;
|
||||||
if (device != null) {
|
|
||||||
// 存储到常规缓存
|
|
||||||
redisTemplate.opsForValue().set(String.format(DEVICE_CACHE_KEY, deviceNo), JacksonUtil.toJSONString(device), 3, TimeUnit.DAYS);
|
|
||||||
redisTemplate.opsForValue().set(String.format(DEVICE_CACHE_KEY, device.getId()), JacksonUtil.toJSONString(device), 3, TimeUnit.DAYS);
|
|
||||||
// 存储到成功结果缓存,用于失败时降级
|
|
||||||
redisTemplate.opsForValue().set(String.format(DEVICE_NO_CACHE_SUCCESS_KEY, deviceNo), JacksonUtil.toJSONString(device), 7, TimeUnit.DAYS);
|
|
||||||
redisTemplate.opsForValue().set(String.format(DEVICE_CACHE_SUCCESS_KEY, device.getId()), JacksonUtil.toJSONString(device), 7, TimeUnit.DAYS);
|
|
||||||
}
|
|
||||||
return device;
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.warn("调用zt-device服务失败, deviceNo: {}, 尝试返回缓存结果", deviceNo, e);
|
|
||||||
String cachedDevice = redisTemplate.opsForValue().get(String.format(DEVICE_NO_CACHE_SUCCESS_KEY, deviceNo));
|
|
||||||
if (cachedDevice != null) {
|
|
||||||
log.info("返回缓存的设备信息, deviceNo: {}", deviceNo);
|
|
||||||
return JacksonUtil.parseObject(cachedDevice, DeviceEntity.class);
|
|
||||||
}
|
|
||||||
log.error("无法获取设备信息且无缓存数据, deviceNo: {}", deviceNo);
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public DeviceConfigEntity getDeviceConfig(Long deviceId) {
|
public DeviceConfigEntity getDeviceConfig(Long deviceId) {
|
||||||
@@ -137,8 +99,6 @@ public class DeviceRepository {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
redisTemplate.delete(String.format(DEVICE_CACHE_KEY, deviceNo));
|
redisTemplate.delete(String.format(DEVICE_CACHE_KEY, deviceNo));
|
||||||
// 清理成功结果缓存
|
|
||||||
redisTemplate.delete(String.format(DEVICE_NO_CACHE_SUCCESS_KEY, deviceNo));
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
public boolean clearDeviceCache(Long deviceId) {
|
public boolean clearDeviceCache(Long deviceId) {
|
||||||
@@ -149,8 +109,6 @@ public class DeviceRepository {
|
|||||||
}
|
}
|
||||||
redisTemplate.delete(String.format(DEVICE_CACHE_KEY, deviceId));
|
redisTemplate.delete(String.format(DEVICE_CACHE_KEY, deviceId));
|
||||||
redisTemplate.delete(String.format(DEVICE_CONFIG_CACHE_KEY, deviceId));
|
redisTemplate.delete(String.format(DEVICE_CONFIG_CACHE_KEY, deviceId));
|
||||||
// 清理成功结果缓存
|
|
||||||
redisTemplate.delete(String.format(DEVICE_CACHE_SUCCESS_KEY, deviceId));
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -49,51 +49,15 @@ public class ScenicRepository {
|
|||||||
|
|
||||||
public ScenicV2DTO getScenicBasic(Long id) {
|
public ScenicV2DTO getScenicBasic(Long id) {
|
||||||
String key = String.format(SCENIC_BASIC_CACHE_KEY, id);
|
String key = String.format(SCENIC_BASIC_CACHE_KEY, id);
|
||||||
try {
|
ScenicV2DTO scenicDTO = scenicIntegrationService.getScenic(id);
|
||||||
ScenicV2DTO scenicDTO = scenicIntegrationService.getScenic(id);
|
return scenicDTO;
|
||||||
|
|
||||||
// 请求成功,写入缓存
|
|
||||||
if (scenicDTO != null) {
|
|
||||||
redisTemplate.opsForValue().set(
|
|
||||||
key,
|
|
||||||
JacksonUtil.toJSONString(scenicDTO)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return scenicDTO;
|
|
||||||
} catch (Exception e) {
|
|
||||||
// 请求失败,尝试从缓存获取历史成功数据
|
|
||||||
String cacheKey = key;
|
|
||||||
if (redisTemplate.hasKey(cacheKey)) {
|
|
||||||
return JacksonUtil.parseObject(redisTemplate.opsForValue().get(cacheKey), ScenicV2DTO.class);
|
|
||||||
}
|
|
||||||
// 缓存也没有,返回null
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ScenicEntity getScenic(Long id) {
|
public ScenicEntity getScenic(Long id) {
|
||||||
String key = String.format(SCENIC_CACHE_KEY, id);
|
String key = String.format(SCENIC_CACHE_KEY, id);
|
||||||
try {
|
ScenicV2WithConfigDTO scenicDTO = scenicIntegrationService.getScenicWithConfig(id);
|
||||||
ScenicV2WithConfigDTO scenicDTO = scenicIntegrationService.getScenicWithConfig(id);
|
ScenicEntity scenicEntity = convertToScenicEntity(scenicDTO);
|
||||||
ScenicEntity scenicEntity = convertToScenicEntity(scenicDTO);
|
return scenicEntity;
|
||||||
|
|
||||||
// 请求成功,写入缓存
|
|
||||||
if (scenicEntity != null) {
|
|
||||||
redisTemplate.opsForValue().set(
|
|
||||||
key,
|
|
||||||
JacksonUtil.toJSONString(scenicEntity)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return scenicEntity;
|
|
||||||
} catch (Exception e) {
|
|
||||||
// 请求失败,尝试从缓存获取历史成功数据
|
|
||||||
String cacheKey = key;
|
|
||||||
if (redisTemplate.hasKey(cacheKey)) {
|
|
||||||
return JacksonUtil.parseObject(redisTemplate.opsForValue().get(cacheKey), ScenicEntity.class);
|
|
||||||
}
|
|
||||||
// 缓存也没有,返回null
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ScenicConfigEntity getScenicConfig(Long scenicId) {
|
public ScenicConfigEntity getScenicConfig(Long scenicId) {
|
||||||
@@ -384,38 +348,13 @@ public class ScenicRepository {
|
|||||||
public ScenicConfigManager getScenicConfigManagerWithCache(Long scenicId) {
|
public ScenicConfigManager getScenicConfigManagerWithCache(Long scenicId) {
|
||||||
String key = String.format(SCENIC_CONFIG_CACHE_KEY + ":manager", scenicId);
|
String key = String.format(SCENIC_CONFIG_CACHE_KEY + ":manager", scenicId);
|
||||||
|
|
||||||
try {
|
List<com.ycwl.basic.integration.scenic.dto.config.ScenicConfigV2DTO> configList =
|
||||||
List<com.ycwl.basic.integration.scenic.dto.config.ScenicConfigV2DTO> configList =
|
scenicConfigIntegrationService.listConfigs(scenicId);
|
||||||
scenicConfigIntegrationService.listConfigs(scenicId);
|
if (configList != null) {
|
||||||
if (configList != null) {
|
ScenicConfigManager manager = new ScenicConfigManager(configList);
|
||||||
ScenicConfigManager manager = new ScenicConfigManager(configList);
|
return manager;
|
||||||
|
|
||||||
// 请求成功,写入缓存(将配置列表序列化存储)
|
|
||||||
redisTemplate.opsForValue().set(
|
|
||||||
key,
|
|
||||||
JacksonUtil.toJSONString(configList)
|
|
||||||
);
|
|
||||||
|
|
||||||
return manager;
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
} catch (Exception e) {
|
|
||||||
// 请求失败,尝试从缓存获取历史成功数据
|
|
||||||
if (redisTemplate.hasKey(key)) {
|
|
||||||
try {
|
|
||||||
String cachedConfigJson = redisTemplate.opsForValue().get(key);
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
List<ScenicConfigV2DTO> cachedConfigList =
|
|
||||||
JacksonUtil.parseArray(cachedConfigJson, ScenicConfigV2DTO.class);
|
|
||||||
return new ScenicConfigManager(cachedConfigList);
|
|
||||||
} catch (Exception cacheException) {
|
|
||||||
// 缓存解析失败,返回null
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// 缓存也没有,返回null
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user