缓存管理 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 查看可用缓存 |