feat(cache): 添加缓存清理功能以同步数据变更

- 在设备默认配置的创建、更新和删除操作后清理相关缓存
- 在设备信息更新和删除操作后清理设备缓存
- 在分账规则的更新、删除、启用和禁用操作后清理规则缓存
- 实现了针对特定配置键的缓存清理方法
- 实现了清除所有默认配置缓存的方法
- 实现了针对特定规则ID的缓存清理方法
This commit is contained in:
2026-02-13 12:07:41 +08:00
parent 533fb306ca
commit b2012f9209
3 changed files with 48 additions and 2 deletions

View File

@@ -64,6 +64,8 @@ public class DeviceDefaultConfigIntegrationService {
log.info("创建默认配置, configKey: {}", request.getConfigKey());
CommonResponse<String> response = defaultConfigClient.createDefaultConfig(request);
String result = handleResponse(response, "创建默认配置失败");
// 清理相关缓存
clearDefaultConfigCache(request.getConfigKey());
return result != null;
}
@@ -75,6 +77,9 @@ public class DeviceDefaultConfigIntegrationService {
CommonResponse<Map<String, Object>> response = defaultConfigClient.updateDefaultConfig(configKey, updates);
Map<String, Object> result = handleResponse(response, "更新默认配置失败");
// 清理相关缓存
clearDefaultConfigCache(configKey);
// 检查是否有冲突信息
if (result != null && result.containsKey("conflict")) {
Object conflictObj = result.get("conflict");
@@ -94,6 +99,8 @@ public class DeviceDefaultConfigIntegrationService {
log.info("删除默认配置, configKey: {}", configKey);
CommonResponse<String> response = defaultConfigClient.deleteDefaultConfig(configKey);
String result = handleResponse(response, "删除默认配置失败");
// 清理相关缓存
clearDefaultConfigCache(configKey);
return result != null;
}
@@ -103,7 +110,26 @@ public class DeviceDefaultConfigIntegrationService {
public BatchDefaultConfigResponse batchUpdateDefaultConfigs(BatchDefaultConfigRequest request) {
log.info("批量更新默认配置, configs count: {}", request.getConfigs().size());
CommonResponse<BatchDefaultConfigResponse> response = defaultConfigClient.batchUpdateDefaultConfigs(request);
return handleResponse(response, "批量更新默认配置失败");
BatchDefaultConfigResponse result = handleResponse(response, "批量更新默认配置失败");
// 清理所有默认配置相关缓存
clearAllDefaultConfigCache();
return result;
}
/**
* 清理指定配置的缓存
*/
private void clearDefaultConfigCache(String configKey) {
if (configKey != null) {
fallbackService.clearFallbackCache(SERVICE_NAME, "defaults:config:" + configKey);
}
}
/**
* 清理所有默认配置缓存
*/
private void clearAllDefaultConfigCache() {
fallbackService.clearAllFallbackCache(SERVICE_NAME);
}
/**

View File

@@ -58,12 +58,16 @@ public class DeviceIntegrationService {
log.debug("更新设备信息, deviceId: {}", deviceId);
CommonResponse<String> response = deviceV2Client.updateDevice(deviceId, request);
handleResponse(response, "更新设备信息失败");
// 清理设备相关缓存
fallbackService.clearFallbackCache(SERVICE_NAME, "device:" + deviceId);
}
public void deleteDevice(Long deviceId) {
log.debug("删除设备, deviceId: {}", deviceId);
CommonResponse<String> response = deviceV2Client.deleteDevice(deviceId);
handleResponse(response, "删除设备失败");
// 清理设备相关缓存
fallbackService.clearFallbackCache(SERVICE_NAME, "device:" + deviceId);
}
public PageResponse<DeviceV2DTO> listDevices(Integer page, Integer pageSize, String name, String no,

View File

@@ -83,7 +83,10 @@ public class ProfitShareIntegrationService {
public RuleVO updateRule(Long ruleId, CreateRuleRequest request) {
log.debug("更新分账规则, ruleId: {}", ruleId);
CommonResponse<RuleVO> response = profitShareClient.updateRule(ruleId, request);
return handleResponse(response, "更新分账规则失败");
RuleVO result = handleResponse(response, "更新分账规则失败");
// 清理规则缓存
clearRuleCache(ruleId);
return result;
}
/**
@@ -93,6 +96,8 @@ public class ProfitShareIntegrationService {
log.debug("删除分账规则, ruleId: {}", ruleId);
CommonResponse<Void> response = profitShareClient.deleteRule(ruleId);
handleResponse(response, "删除分账规则失败");
// 清理规则缓存
clearRuleCache(ruleId);
}
/**
@@ -102,6 +107,8 @@ public class ProfitShareIntegrationService {
log.debug("启用分账规则, ruleId: {}", ruleId);
CommonResponse<Void> response = profitShareClient.enableRule(ruleId);
handleResponse(response, "启用分账规则失败");
// 清理规则缓存
clearRuleCache(ruleId);
}
/**
@@ -111,6 +118,8 @@ public class ProfitShareIntegrationService {
log.debug("禁用分账规则, ruleId: {}", ruleId);
CommonResponse<Void> response = profitShareClient.disableRule(ruleId);
handleResponse(response, "禁用分账规则失败");
// 清理规则缓存
clearRuleCache(ruleId);
}
// ==================== 分账记录查询 ====================
@@ -211,6 +220,13 @@ public class ProfitShareIntegrationService {
// ==================== 私有方法 ====================
/**
* 清理规则相关缓存
*/
private void clearRuleCache(Long ruleId) {
fallbackService.clearFallbackCache(SERVICE_NAME, "rule:" + ruleId);
}
private <T> T handleResponse(CommonResponse<T> response, String errorMessage) {
if (response == null || !response.isSuccess()) {
String msg = response != null && response.getMessage() != null