跳到主要内容

缓存管理 API

CacheManagementController 提供缓存管理接口,用于查看、清除和预热缓存。


📋 接口列表

方法路径说明
GET/actuator/cache获取所有缓存
DELETE/actuator/cache/\{cacheName\}清除指定缓存
DELETE/actuator/cache清除所有缓存
DELETE/actuator/cache/user/\{userId\}清除用户缓存
POST/actuator/cache/warmup触发缓存预热
GET/actuator/cache/redis/info获取 Redis 信息

🧪 测试示例

获取所有缓存

curl http://localhost:8080/actuator/cache \
-H "Authorization: Bearer {token}"
{
"cacheNames": ["user:roles"],
"totalCaches": 1
}

清除指定缓存

curl -X DELETE http://localhost:8080/actuator/cache/user:roles \
-H "Authorization: Bearer {token}"

触发缓存预热

curl -X POST http://localhost:8080/actuator/cache/warmup \
-H "Authorization: Bearer {token}"
{
"success": true,
"message": "缓存预热完成",
"duration": "256ms"
}

获取 Redis 信息

curl http://localhost:8080/actuator/cache/redis/info \
-H "Authorization: Bearer {token}"

🔐 认证

这些接口需要 JWT Token:

# 1. 登录获取 Token
TOKEN=$(curl -s -X POST http://localhost:8080/api/v1/auth/login \
-H "Content-Type: application/json" \
-d '{"username":"admin","password":"123456"}' | jq -r '.data.token')

# 2. 使用 Token 访问
curl http://localhost:8080/actuator/cache \
-H "Authorization: Bearer $TOKEN"

🐛 常见问题

错误原因解决
403 Forbidden未提供 Token登录获取 Token
404 Not Found路径错误检查 URL
缓存不存在cacheName 错误先 GET 查看可用缓存