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
|
/agileboot-admin/src/main/resources/application-prod.yml
|
||||||
|
|
||||||
/.svn
|
/.svn
|
||||||
|
|
||||||
|
.trae
|
||||||
|
|
@ -53,32 +53,12 @@ public class CaffeineController {
|
||||||
@Operation(summary = "清空指定缓存", description = "清空指定类型的Caffeine缓存")
|
@Operation(summary = "清空指定缓存", description = "清空指定类型的Caffeine缓存")
|
||||||
public ResponseEntity<String> clearCache(@PathVariable String cacheName) {
|
public ResponseEntity<String> clearCache(@PathVariable String cacheName) {
|
||||||
try {
|
try {
|
||||||
switch (cacheName) {
|
com.agileboot.infrastructure.cache.caffeine.AbstractCaffeineCacheTemplate<?> cache = caffeineCacheService.getCacheInstance(cacheName);
|
||||||
case "captchaCache":
|
if (cache == null) {
|
||||||
caffeineCacheService.captchaCache.invalidateAll();
|
return ResponseEntity.badRequest()
|
||||||
break;
|
.body("未知的缓存类型: " + cacheName + ",支持的类型: " + String.join(", ", caffeineCacheService.getAllCacheNames()));
|
||||||
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:
|
|
||||||
return ResponseEntity.badRequest()
|
|
||||||
.body("未知的缓存类型: " + cacheName + ",支持的类型: captchaCache, loginUserCache, userCache, roleCache, postCache, qyUseridCache, dynamicCodeCache");
|
|
||||||
}
|
}
|
||||||
|
cache.invalidateAll();
|
||||||
return ResponseEntity.ok("缓存 [" + cacheName + "] 已清空");
|
return ResponseEntity.ok("缓存 [" + cacheName + "] 已清空");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("清空缓存失败: {}", cacheName, e);
|
log.error("清空缓存失败: {}", cacheName, e);
|
||||||
|
|
@ -94,13 +74,12 @@ public class CaffeineController {
|
||||||
@Operation(summary = "清空所有缓存", description = "清空所有类型的Caffeine缓存")
|
@Operation(summary = "清空所有缓存", description = "清空所有类型的Caffeine缓存")
|
||||||
public ResponseEntity<String> clearAllCaches() {
|
public ResponseEntity<String> clearAllCaches() {
|
||||||
try {
|
try {
|
||||||
caffeineCacheService.captchaCache.invalidateAll();
|
for (String cacheName : caffeineCacheService.getAllCacheNames()) {
|
||||||
caffeineCacheService.loginUserCache.invalidateAll();
|
com.agileboot.infrastructure.cache.caffeine.AbstractCaffeineCacheTemplate<?> cache = caffeineCacheService.getCacheInstance(cacheName);
|
||||||
caffeineCacheService.userCache.invalidateAll();
|
if (cache != null) {
|
||||||
caffeineCacheService.roleCache.invalidateAll();
|
cache.invalidateAll();
|
||||||
caffeineCacheService.postCache.invalidateAll();
|
}
|
||||||
caffeineCacheService.qyUseridCache.invalidateAll();
|
}
|
||||||
caffeineCacheService.dynamicCodeCache.invalidateAll();
|
|
||||||
|
|
||||||
return ResponseEntity.ok("所有Caffeine缓存已清空");
|
return ResponseEntity.ok("所有Caffeine缓存已清空");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|
@ -118,13 +97,12 @@ public class CaffeineController {
|
||||||
public ResponseEntity<?> getAllCacheData() {
|
public ResponseEntity<?> getAllCacheData() {
|
||||||
try {
|
try {
|
||||||
Map<String, Object> allCaches = new HashMap<>();
|
Map<String, Object> allCaches = new HashMap<>();
|
||||||
allCaches.put("captchaCache", caffeineCacheService.captchaCache.getAll());
|
for (String cacheName : caffeineCacheService.getAllCacheNames()) {
|
||||||
allCaches.put("loginUserCache", caffeineCacheService.loginUserCache.getAll());
|
com.agileboot.infrastructure.cache.caffeine.AbstractCaffeineCacheTemplate<?> cache = caffeineCacheService.getCacheInstance(cacheName);
|
||||||
allCaches.put("userCache", caffeineCacheService.userCache.getAll());
|
if (cache != null) {
|
||||||
allCaches.put("roleCache", caffeineCacheService.roleCache.getAll());
|
allCaches.put(cacheName, cache.getAll());
|
||||||
allCaches.put("postCache", caffeineCacheService.postCache.getAll());
|
}
|
||||||
allCaches.put("qyUseridCache", caffeineCacheService.qyUseridCache.getAll());
|
}
|
||||||
allCaches.put("dynamicCodeCache", caffeineCacheService.dynamicCodeCache.getAll());
|
|
||||||
|
|
||||||
return ResponseEntity.ok(allCaches);
|
return ResponseEntity.ok(allCaches);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|
@ -144,39 +122,16 @@ public class CaffeineController {
|
||||||
Map<String, Object> result = new HashMap<>();
|
Map<String, Object> result = new HashMap<>();
|
||||||
result.put("cacheName", cacheName);
|
result.put("cacheName", cacheName);
|
||||||
|
|
||||||
switch (cacheName) {
|
com.agileboot.infrastructure.cache.caffeine.AbstractCaffeineCacheTemplate<?> cache = caffeineCacheService.getCacheInstance(cacheName);
|
||||||
case "captchaCache":
|
if (cache == null) {
|
||||||
result.put("data", caffeineCacheService.captchaCache.getAll());
|
Map<String, Object> map = new HashMap<>();
|
||||||
break;
|
map.put("error", "未知的缓存类型: " + cacheName);
|
||||||
case "loginUserCache":
|
map.put("supportedTypes", caffeineCacheService.getAllCacheNames());
|
||||||
result.put("data", caffeineCacheService.loginUserCache.getAll());
|
return ResponseEntity.badRequest()
|
||||||
break;
|
.body(map);
|
||||||
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:
|
|
||||||
Map<String, Object> map = new HashMap<>();
|
|
||||||
map.put("error", "未知的缓存类型: " + cacheName);
|
|
||||||
map.put("supportedTypes", new String[]{
|
|
||||||
"captchaCache", "loginUserCache", "userCache",
|
|
||||||
"roleCache", "postCache", "qyUseridCache", "dynamicCodeCache"
|
|
||||||
});
|
|
||||||
return ResponseEntity.badRequest()
|
|
||||||
.body(map);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
result.put("data", cache.getAll());
|
||||||
return ResponseEntity.ok(result);
|
return ResponseEntity.ok(result);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("获取缓存数据失败: {}", cacheName, e);
|
log.error("获取缓存数据失败: {}", cacheName, e);
|
||||||
|
|
@ -186,4 +141,36 @@ public class CaffeineController {
|
||||||
.body(map);
|
.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");
|
stats.append("Dynamic Code Cache: ").append(dynamicCodeCache.getStats()).append("\n");
|
||||||
return stats.toString();
|
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