feat(缓存监控): 新增缓存管理功能并添加错误处理
添加DomainConstants类定义服务域名常量 扩展CaffeineCacheController功能,新增缓存清空、数据查询等接口 为所有缓存操作添加异常处理和日志记录
This commit is contained in:
parent
63d1ff85b6
commit
63229243fc
|
|
@ -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<String> 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<String> 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<String> 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<Map<String, Object>> getAllCacheData() {
|
||||
try {
|
||||
Map<String, Object> 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<Map<String, Object>> getCacheData(@PathVariable String cacheName) {
|
||||
try {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("cacheName", cacheName);
|
||||
|
||||
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 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<Map<String, Object>> 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 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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";
|
||||
}
|
||||
Loading…
Reference in New Issue