From 63229243fce93db55f9a9aa514f744b7fff50d2c Mon Sep 17 00:00:00 2001 From: dzq Date: Tue, 25 Nov 2025 17:49:16 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E7=BC=93=E5=AD=98=E7=9B=91=E6=8E=A7):=20?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=E7=BC=93=E5=AD=98=E7=AE=A1=E7=90=86=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E5=B9=B6=E6=B7=BB=E5=8A=A0=E9=94=99=E8=AF=AF=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加DomainConstants类定义服务域名常量 扩展CaffeineCacheController功能,新增缓存清空、数据查询等接口 为所有缓存操作添加异常处理和日志记录 --- .../monitor/CaffeineCacheController.java | 137 +++++++++++++++++- .../common/constant/DomainConstants.java | 6 + 2 files changed, 137 insertions(+), 6 deletions(-) create mode 100644 agileboot-common/src/main/java/com/agileboot/common/constant/DomainConstants.java diff --git a/agileboot-admin/src/main/java/com/agileboot/admin/controller/monitor/CaffeineCacheController.java b/agileboot-admin/src/main/java/com/agileboot/admin/controller/monitor/CaffeineCacheController.java index 5a09c8f..06813f0 100644 --- a/agileboot-admin/src/main/java/com/agileboot/admin/controller/monitor/CaffeineCacheController.java +++ b/agileboot-admin/src/main/java/com/agileboot/admin/controller/monitor/CaffeineCacheController.java @@ -1,19 +1,24 @@ package com.agileboot.admin.controller.monitor; +import com.agileboot.common.exception.ApiException; +import com.agileboot.common.exception.error.ErrorCode; import com.agileboot.domain.common.cache.CaffeineCacheService; import com.agileboot.common.core.dto.ResponseDTO; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.security.access.prepost.PreAuthorize; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; + +import java.util.HashMap; +import java.util.Map; /** * Caffeine缓存监控控制器 * @author valarchie */ +@Slf4j @Tag(name = "Caffeine缓存监控接口") @RestController @RequestMapping("/monitor/caffeine") @@ -26,10 +31,130 @@ public class CaffeineCacheController { * 获取Caffeine缓存统计信息 */ @Operation(summary = "获取Caffeine缓存统计信息") - @PreAuthorize("@permission.has('monitor:cache:list')") @GetMapping("/stats") public ResponseDTO getCacheStats() { - String stats = caffeineCacheService.getCacheStats(); - return ResponseDTO.ok(stats); + try { + String stats = caffeineCacheService.getCacheStats(); + return ResponseDTO.ok(stats); + } catch (Exception e) { + log.error("获取缓存统计信息失败", e); + return ResponseDTO.fail(new ApiException(ErrorCode.Internal.GET_CACHE_FAILED, e)); + } + } + + /** + * 清空指定类型的缓存 + * + * @param cacheName 缓存名称 (captchaCache, loginUserCache, userCache, roleCache, postCache, qyUseridCache, dynamicCodeCache) + */ + @Operation(summary = "清空指定缓存", description = "清空指定类型的Caffeine缓存") + @DeleteMapping("/cache/{cacheName}") + public ResponseDTO clearCache(@PathVariable String cacheName) { + try { + com.agileboot.infrastructure.cache.caffeine.AbstractCaffeineCacheTemplate cache = caffeineCacheService.getCacheInstance(cacheName); + if (cache == null) { + return ResponseDTO.fail("未知的缓存类型: " + cacheName + ",支持的类型: " + String.join(", ", caffeineCacheService.getAllCacheNames())); + } + cache.invalidateAll(); + return ResponseDTO.ok("缓存 [" + cacheName + "] 已清空"); + } catch (Exception e) { + log.error("清空缓存失败: {}", cacheName, e); + return ResponseDTO.fail(new ApiException(ErrorCode.Internal.GET_CACHE_FAILED, e)); + } + } + + /** + * 清空所有Caffeine缓存 + */ + @Operation(summary = "清空所有缓存", description = "清空所有类型的Caffeine缓存") + @DeleteMapping("/cache") + public ResponseDTO clearAllCaches() { + try { + for (String cacheName : caffeineCacheService.getAllCacheNames()) { + com.agileboot.infrastructure.cache.caffeine.AbstractCaffeineCacheTemplate cache = caffeineCacheService.getCacheInstance(cacheName); + if (cache != null) { + cache.invalidateAll(); + } + } + return ResponseDTO.ok("所有Caffeine缓存已清空"); + } catch (Exception e) { + log.error("清空所有缓存失败", e); + return ResponseDTO.fail(new ApiException(ErrorCode.Internal.GET_CACHE_FAILED, e)); + } + } + + /** + * 获取所有缓存数据 + */ + @Operation(summary = "获取所有缓存数据", description = "获取所有Caffeine缓存中的数据") + @GetMapping("/cache/all") + public ResponseDTO> getAllCacheData() { + try { + Map allCaches = new HashMap<>(); + for (String cacheName : caffeineCacheService.getAllCacheNames()) { + com.agileboot.infrastructure.cache.caffeine.AbstractCaffeineCacheTemplate cache = caffeineCacheService.getCacheInstance(cacheName); + if (cache != null) { + allCaches.put(cacheName, cache.getAll()); + } + } + return ResponseDTO.ok(allCaches); + } catch (Exception e) { + log.error("获取所有缓存数据失败", e); + return ResponseDTO.fail(new ApiException(ErrorCode.Internal.GET_CACHE_FAILED, e)); + } + } + + /** + * 获取指定缓存的数据 + */ + @Operation(summary = "获取指定缓存数据", description = "获取指定类型的Caffeine缓存数据") + @GetMapping("/cache/{cacheName}/data") + public ResponseDTO> getCacheData(@PathVariable String cacheName) { + try { + Map result = new HashMap<>(); + result.put("cacheName", cacheName); + + 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 ResponseDTO.fail(map); + } + + result.put("data", cache.getAll()); + return ResponseDTO.ok(result); + } catch (Exception e) { + log.error("获取缓存数据失败: {}", cacheName, e); + return ResponseDTO.fail(new ApiException(ErrorCode.Internal.GET_CACHE_FAILED, e)); + } + } + + /** + * 获取指定缓存中特定key的数据 + */ + @Operation(summary = "获取指定缓存中特定key的数据", description = "通过指定类型和key获取Caffeine缓存值") + @GetMapping("/cache/{cacheName}/data/{key}") + public ResponseDTO> 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 ResponseDTO.fail(map); + } + + Object value = cache.get(key); + result.put("value", value); + return ResponseDTO.ok(result); + } catch (Exception e) { + log.error("获取缓存数据失败: {}, key: {}", cacheName, key, e); + return ResponseDTO.fail(new ApiException(ErrorCode.Internal.GET_CACHE_FAILED, e)); + } } } \ No newline at end of file diff --git a/agileboot-common/src/main/java/com/agileboot/common/constant/DomainConstants.java b/agileboot-common/src/main/java/com/agileboot/common/constant/DomainConstants.java new file mode 100644 index 0000000..05627b2 --- /dev/null +++ b/agileboot-common/src/main/java/com/agileboot/common/constant/DomainConstants.java @@ -0,0 +1,6 @@ +package com.agileboot.common.constant; + +public class DomainConstants { + public static final String API_SERVICE_DOMAIN = "http://wxshop.ab98.cn/shop-api"; + public static final String ADMIN_SERVICE_DOMAIN = "http://wxshop.ab98.cn/shop-back-end"; +}