From 63d1ff85b669fcdf1424238356b96a1825351970 Mon Sep 17 00:00:00 2001 From: dzq Date: Tue, 25 Nov 2025 17:12:56 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E7=BC=93=E5=AD=98):=20=E9=87=8D?= =?UTF-8?q?=E6=9E=84=E7=BC=93=E5=AD=98=E6=9C=8D=E5=8A=A1=E5=8F=8A=E6=8E=A7?= =?UTF-8?q?=E5=88=B6=E5=99=A8=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将缓存操作逻辑集中到CaffeineCacheService中,新增获取缓存实例和缓存名称列表的方法 简化CaffeineController中的重复代码,并新增通过key获取缓存值的接口 --- .gitignore | 2 + .../api/controller/CaffeineController.java | 127 ++++++++---------- .../common/cache/CaffeineCacheService.java | 37 +++++ 3 files changed, 96 insertions(+), 70 deletions(-) diff --git a/.gitignore b/.gitignore index 69b2a5f..6f9b1ab 100644 --- a/.gitignore +++ b/.gitignore @@ -49,3 +49,5 @@ nbdist/ /agileboot-admin/src/main/resources/application-prod.yml /.svn + +.trae \ No newline at end of file diff --git a/agileboot-api/src/main/java/com/agileboot/api/controller/CaffeineController.java b/agileboot-api/src/main/java/com/agileboot/api/controller/CaffeineController.java index e027150..6003ba3 100644 --- a/agileboot-api/src/main/java/com/agileboot/api/controller/CaffeineController.java +++ b/agileboot-api/src/main/java/com/agileboot/api/controller/CaffeineController.java @@ -53,32 +53,12 @@ public class CaffeineController { @Operation(summary = "清空指定缓存", description = "清空指定类型的Caffeine缓存") public ResponseEntity 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: - return ResponseEntity.badRequest() - .body("未知的缓存类型: " + cacheName + ",支持的类型: captchaCache, loginUserCache, userCache, roleCache, postCache, qyUseridCache, dynamicCodeCache"); + com.agileboot.infrastructure.cache.caffeine.AbstractCaffeineCacheTemplate cache = caffeineCacheService.getCacheInstance(cacheName); + if (cache == null) { + return ResponseEntity.badRequest() + .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 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 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 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: - Map map = new HashMap<>(); - map.put("error", "未知的缓存类型: " + cacheName); - map.put("supportedTypes", new String[]{ - "captchaCache", "loginUserCache", "userCache", - "roleCache", "postCache", "qyUseridCache", "dynamicCodeCache" - }); - return ResponseEntity.badRequest() - .body(map); + com.agileboot.infrastructure.cache.caffeine.AbstractCaffeineCacheTemplate cache = caffeineCacheService.getCacheInstance(cacheName); + if (cache == null) { + Map map = new HashMap<>(); + map.put("error", "未知的缓存类型: " + cacheName); + 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 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 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 map = new HashMap<>(); + map.put("error", "获取缓存数据失败: " + e.getMessage()); + return ResponseEntity.internalServerError() + .body(map); + } + } } diff --git a/agileboot-domain/src/main/java/com/agileboot/domain/common/cache/CaffeineCacheService.java b/agileboot-domain/src/main/java/com/agileboot/domain/common/cache/CaffeineCacheService.java index a677a39..3920f66 100644 --- a/agileboot-domain/src/main/java/com/agileboot/domain/common/cache/CaffeineCacheService.java +++ b/agileboot-domain/src/main/java/com/agileboot/domain/common/cache/CaffeineCacheService.java @@ -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" + }; + } } \ No newline at end of file