refactor(缓存): 重构缓存服务及控制器代码
将缓存操作逻辑集中到CaffeineCacheService中,新增获取缓存实例和缓存名称列表的方法 简化CaffeineController中的重复代码,并新增通过key获取缓存值的接口
This commit is contained in:
parent
52d1aea9f0
commit
63d1ff85b6
|
|
@ -49,3 +49,5 @@ nbdist/
|
|||
/agileboot-admin/src/main/resources/application-prod.yml
|
||||
|
||||
/.svn
|
||||
|
||||
.trae
|
||||
|
|
@ -53,32 +53,12 @@ public class CaffeineController {
|
|||
@Operation(summary = "清空指定缓存", description = "清空指定类型的Caffeine缓存")
|
||||
public ResponseEntity<String> clearCache(@PathVariable String cacheName) {
|
||||
try {
|
||||
switch (cacheName) {
|
||||
case "captchaCache":
|
||||
caffeineCacheService.captchaCache.invalidateAll();
|
||||
break;
|
||||
case "loginUserCache":
|
||||
caffeineCacheService.loginUserCache.invalidateAll();
|
||||
break;
|
||||
case "userCache":
|
||||
caffeineCacheService.userCache.invalidateAll();
|
||||
break;
|
||||
case "roleCache":
|
||||
caffeineCacheService.roleCache.invalidateAll();
|
||||
break;
|
||||
case "postCache":
|
||||
caffeineCacheService.postCache.invalidateAll();
|
||||
break;
|
||||
case "qyUseridCache":
|
||||
caffeineCacheService.qyUseridCache.invalidateAll();
|
||||
break;
|
||||
case "dynamicCodeCache":
|
||||
caffeineCacheService.dynamicCodeCache.invalidateAll();
|
||||
break;
|
||||
default:
|
||||
com.agileboot.infrastructure.cache.caffeine.AbstractCaffeineCacheTemplate<?> cache = caffeineCacheService.getCacheInstance(cacheName);
|
||||
if (cache == null) {
|
||||
return ResponseEntity.badRequest()
|
||||
.body("未知的缓存类型: " + cacheName + ",支持的类型: captchaCache, loginUserCache, userCache, roleCache, postCache, qyUseridCache, dynamicCodeCache");
|
||||
.body("未知的缓存类型: " + cacheName + ",支持的类型: " + String.join(", ", caffeineCacheService.getAllCacheNames()));
|
||||
}
|
||||
cache.invalidateAll();
|
||||
return ResponseEntity.ok("缓存 [" + cacheName + "] 已清空");
|
||||
} catch (Exception e) {
|
||||
log.error("清空缓存失败: {}", cacheName, e);
|
||||
|
|
@ -94,13 +74,12 @@ public class CaffeineController {
|
|||
@Operation(summary = "清空所有缓存", description = "清空所有类型的Caffeine缓存")
|
||||
public ResponseEntity<String> clearAllCaches() {
|
||||
try {
|
||||
caffeineCacheService.captchaCache.invalidateAll();
|
||||
caffeineCacheService.loginUserCache.invalidateAll();
|
||||
caffeineCacheService.userCache.invalidateAll();
|
||||
caffeineCacheService.roleCache.invalidateAll();
|
||||
caffeineCacheService.postCache.invalidateAll();
|
||||
caffeineCacheService.qyUseridCache.invalidateAll();
|
||||
caffeineCacheService.dynamicCodeCache.invalidateAll();
|
||||
for (String cacheName : caffeineCacheService.getAllCacheNames()) {
|
||||
com.agileboot.infrastructure.cache.caffeine.AbstractCaffeineCacheTemplate<?> cache = caffeineCacheService.getCacheInstance(cacheName);
|
||||
if (cache != null) {
|
||||
cache.invalidateAll();
|
||||
}
|
||||
}
|
||||
|
||||
return ResponseEntity.ok("所有Caffeine缓存已清空");
|
||||
} catch (Exception e) {
|
||||
|
|
@ -118,13 +97,12 @@ public class CaffeineController {
|
|||
public ResponseEntity<?> getAllCacheData() {
|
||||
try {
|
||||
Map<String, Object> allCaches = new HashMap<>();
|
||||
allCaches.put("captchaCache", caffeineCacheService.captchaCache.getAll());
|
||||
allCaches.put("loginUserCache", caffeineCacheService.loginUserCache.getAll());
|
||||
allCaches.put("userCache", caffeineCacheService.userCache.getAll());
|
||||
allCaches.put("roleCache", caffeineCacheService.roleCache.getAll());
|
||||
allCaches.put("postCache", caffeineCacheService.postCache.getAll());
|
||||
allCaches.put("qyUseridCache", caffeineCacheService.qyUseridCache.getAll());
|
||||
allCaches.put("dynamicCodeCache", caffeineCacheService.dynamicCodeCache.getAll());
|
||||
for (String cacheName : caffeineCacheService.getAllCacheNames()) {
|
||||
com.agileboot.infrastructure.cache.caffeine.AbstractCaffeineCacheTemplate<?> cache = caffeineCacheService.getCacheInstance(cacheName);
|
||||
if (cache != null) {
|
||||
allCaches.put(cacheName, cache.getAll());
|
||||
}
|
||||
}
|
||||
|
||||
return ResponseEntity.ok(allCaches);
|
||||
} catch (Exception e) {
|
||||
|
|
@ -144,39 +122,16 @@ public class CaffeineController {
|
|||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("cacheName", cacheName);
|
||||
|
||||
switch (cacheName) {
|
||||
case "captchaCache":
|
||||
result.put("data", caffeineCacheService.captchaCache.getAll());
|
||||
break;
|
||||
case "loginUserCache":
|
||||
result.put("data", caffeineCacheService.loginUserCache.getAll());
|
||||
break;
|
||||
case "userCache":
|
||||
result.put("data", caffeineCacheService.userCache.getAll());
|
||||
break;
|
||||
case "roleCache":
|
||||
result.put("data", caffeineCacheService.roleCache.getAll());
|
||||
break;
|
||||
case "postCache":
|
||||
result.put("data", caffeineCacheService.postCache.getAll());
|
||||
break;
|
||||
case "qyUseridCache":
|
||||
result.put("data", caffeineCacheService.qyUseridCache.getAll());
|
||||
break;
|
||||
case "dynamicCodeCache":
|
||||
result.put("data", caffeineCacheService.dynamicCodeCache.getAll());
|
||||
break;
|
||||
default:
|
||||
com.agileboot.infrastructure.cache.caffeine.AbstractCaffeineCacheTemplate<?> cache = caffeineCacheService.getCacheInstance(cacheName);
|
||||
if (cache == null) {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("error", "未知的缓存类型: " + cacheName);
|
||||
map.put("supportedTypes", new String[]{
|
||||
"captchaCache", "loginUserCache", "userCache",
|
||||
"roleCache", "postCache", "qyUseridCache", "dynamicCodeCache"
|
||||
});
|
||||
map.put("supportedTypes", caffeineCacheService.getAllCacheNames());
|
||||
return ResponseEntity.badRequest()
|
||||
.body(map);
|
||||
}
|
||||
|
||||
result.put("data", cache.getAll());
|
||||
return ResponseEntity.ok(result);
|
||||
} catch (Exception e) {
|
||||
log.error("获取缓存数据失败: {}", cacheName, e);
|
||||
|
|
@ -186,4 +141,36 @@ public class CaffeineController {
|
|||
.body(map);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定缓存中特定key的数据
|
||||
*/
|
||||
@GetMapping("/cache/{cacheName}/data/{key}")
|
||||
@Operation(summary = "获取指定缓存中特定key的数据", description = "通过指定类型和key获取Caffeine缓存值")
|
||||
public ResponseEntity<?> getCacheDataByKey(@PathVariable String cacheName, @PathVariable String key) {
|
||||
try {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("cacheName", cacheName);
|
||||
result.put("key", key);
|
||||
|
||||
com.agileboot.infrastructure.cache.caffeine.AbstractCaffeineCacheTemplate<?> cache = caffeineCacheService.getCacheInstance(cacheName);
|
||||
if (cache == null) {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("error", "未知的缓存类型: " + cacheName);
|
||||
map.put("supportedTypes", caffeineCacheService.getAllCacheNames());
|
||||
return ResponseEntity.badRequest()
|
||||
.body(map);
|
||||
}
|
||||
|
||||
Object value = cache.get(key);
|
||||
result.put("value", value);
|
||||
return ResponseEntity.ok(result);
|
||||
} catch (Exception e) {
|
||||
log.error("获取缓存数据失败: {}, key: {}", cacheName, key, e);
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("error", "获取缓存数据失败: " + e.getMessage());
|
||||
return ResponseEntity.internalServerError()
|
||||
.body(map);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -114,4 +114,41 @@ public class CaffeineCacheService {
|
|||
stats.append("Dynamic Code Cache: ").append(dynamicCodeCache.getStats()).append("\n");
|
||||
return stats.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过缓存名称获取缓存实例
|
||||
* @param cacheName 缓存名称
|
||||
* @return 对应的缓存实例,如果缓存名称无效则返回null
|
||||
*/
|
||||
public AbstractCaffeineCacheTemplate<?> getCacheInstance(String cacheName) {
|
||||
switch (cacheName) {
|
||||
case "captchaCache":
|
||||
return captchaCache;
|
||||
case "loginUserCache":
|
||||
return loginUserCache;
|
||||
case "userCache":
|
||||
return userCache;
|
||||
case "roleCache":
|
||||
return roleCache;
|
||||
case "postCache":
|
||||
return postCache;
|
||||
case "qyUseridCache":
|
||||
return qyUseridCache;
|
||||
case "dynamicCodeCache":
|
||||
return dynamicCodeCache;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有缓存实例的名称列表
|
||||
* @return 缓存实例名称列表
|
||||
*/
|
||||
public String[] getAllCacheNames() {
|
||||
return new String[]{
|
||||
"captchaCache", "loginUserCache", "userCache",
|
||||
"roleCache", "postCache", "qyUseridCache", "dynamicCodeCache"
|
||||
};
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue