CacheManagementController.java
3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package com.infoloop.tianting.cache;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
/**
* 缓存管理控制器
* 提供缓存操作的REST端点
*/
@Slf4j
@RestController
@RequestMapping("/actuator/cache")
public class CacheManagementController {
@Autowired
@Qualifier("multiLevelCacheService")
private CacheService cacheService;
/**
* 获取缓存统计信息
*/
@GetMapping("/stats")
public CacheStats getCacheStats() {
return cacheService.getStats();
}
/**
* 清除指定缓存
*/
@DeleteMapping("/evict")
public ResponseEntity<?> evictCache(@RequestParam String key) {
try {
cacheService.evict(key);
log.info("缓存清除成功: {}", key);
return ResponseEntity.ok("缓存已清除: " + key);
} catch (Exception e) {
log.error("缓存清除失败: key={}, error={}", key, e.getMessage(), e);
return ResponseEntity.internalServerError().body("缓存清除失败: " + e.getMessage());
}
}
/**
* 根据前缀清除缓存
*/
@DeleteMapping("/evict/prefix")
public ResponseEntity<?> evictByPrefix(@RequestParam String prefix) {
try {
cacheService.evictByPrefix(prefix);
log.info("前缀缓存清除成功: {}", prefix);
return ResponseEntity.ok("前缀缓存已清除: " + prefix);
} catch (Exception e) {
log.error("前缀缓存清除失败: prefix={}, error={}", prefix, e.getMessage(), e);
return ResponseEntity.internalServerError().body("前缀缓存清除失败: " + e.getMessage());
}
}
/**
* 清空所有缓存
*/
@DeleteMapping("/clear")
public ResponseEntity<?> clearCache() {
try {
cacheService.clear();
log.info("所有缓存清空成功");
return ResponseEntity.ok("所有缓存已清除");
} catch (Exception e) {
log.error("缓存清空失败: error={}", e.getMessage(), e);
return ResponseEntity.internalServerError().body("缓存清空失败: " + e.getMessage());
}
}
/**
* 检查缓存是否存在
*/
@GetMapping("/exists")
public ResponseEntity<?> checkExists(@RequestParam String key) {
try {
boolean exists = cacheService.exists(key);
return ResponseEntity.ok(String.format("缓存key '%s' %s", key, exists ? "存在" : "不存在"));
} catch (Exception e) {
log.error("缓存存在性检查失败: key={}, error={}", key, e.getMessage(), e);
return ResponseEntity.internalServerError().body("缓存检查失败: " + e.getMessage());
}
}
/**
* 获取缓存过期时间
*/
@GetMapping("/expire")
public ResponseEntity<?> getExpire(@RequestParam String key) {
try {
long expire = cacheService.getExpire(key);
String message;
if (expire == -2) {
message = "缓存key不存在";
} else if (expire == -1) {
message = "缓存key永久有效";
} else {
message = String.format("缓存key剩余时间: %d秒", expire);
}
return ResponseEntity.ok(message);
} catch (Exception e) {
log.error("缓存过期时间获取失败: key={}, error={}", key, e.getMessage(), e);
return ResponseEntity.internalServerError().body("缓存过期时间获取失败: " + e.getMessage());
}
}
}