提交 | 用户 | 时间
|
722af2
|
1 |
package com.dl.web.controller.monitor; |
X |
2 |
|
|
3 |
import cn.dev33.satoken.annotation.SaCheckPermission; |
|
4 |
import cn.hutool.core.collection.CollUtil; |
|
5 |
import com.dl.common.constant.CacheConstants; |
|
6 |
import com.dl.common.constant.CacheNames; |
|
7 |
import com.dl.common.core.domain.R; |
|
8 |
import com.dl.common.utils.JsonUtils; |
|
9 |
import com.dl.common.utils.StringUtils; |
|
10 |
import com.dl.common.utils.redis.CacheUtils; |
|
11 |
import com.dl.common.utils.redis.RedisUtils; |
|
12 |
import com.dl.system.domain.SysCache; |
|
13 |
import lombok.RequiredArgsConstructor; |
|
14 |
import org.redisson.spring.data.connection.RedissonConnectionFactory; |
|
15 |
import org.springframework.data.redis.connection.RedisConnection; |
|
16 |
import org.springframework.web.bind.annotation.*; |
|
17 |
|
|
18 |
import java.util.*; |
|
19 |
import java.util.stream.Collectors; |
|
20 |
|
|
21 |
/** |
|
22 |
* 缓存监控 |
|
23 |
* |
|
24 |
* @author Lion Li |
|
25 |
*/ |
|
26 |
@RequiredArgsConstructor |
|
27 |
@RestController |
|
28 |
@RequestMapping("/monitor/cache") |
|
29 |
public class CacheController { |
|
30 |
|
|
31 |
private final RedissonConnectionFactory connectionFactory; |
|
32 |
|
|
33 |
private final static List<SysCache> CACHES = new ArrayList<>(); |
|
34 |
|
|
35 |
static { |
|
36 |
CACHES.add(new SysCache(CacheConstants.ONLINE_TOKEN_KEY, "在线用户")); |
|
37 |
CACHES.add(new SysCache(CacheNames.SYS_CONFIG, "配置信息")); |
|
38 |
CACHES.add(new SysCache(CacheNames.SYS_DICT, "数据字典")); |
|
39 |
CACHES.add(new SysCache(CacheConstants.CAPTCHA_CODE_KEY, "验证码")); |
|
40 |
CACHES.add(new SysCache(CacheConstants.REPEAT_SUBMIT_KEY, "防重提交")); |
|
41 |
CACHES.add(new SysCache(CacheConstants.RATE_LIMIT_KEY, "限流处理")); |
|
42 |
CACHES.add(new SysCache(CacheNames.SYS_OSS_CONFIG, "OSS配置")); |
|
43 |
CACHES.add(new SysCache(CacheConstants.PWD_ERR_CNT_KEY, "密码错误次数")); |
|
44 |
} |
|
45 |
|
|
46 |
/** |
|
47 |
* 获取缓存监控列表 |
|
48 |
*/ |
|
49 |
@SaCheckPermission("monitor:cache:list") |
|
50 |
@GetMapping() |
|
51 |
public R<Map<String, Object>> getInfo() throws Exception { |
|
52 |
RedisConnection connection = connectionFactory.getConnection(); |
|
53 |
Properties info = connection.info(); |
|
54 |
Properties commandStats = connection.info("commandstats"); |
|
55 |
Long dbSize = connection.dbSize(); |
|
56 |
|
|
57 |
Map<String, Object> result = new HashMap<>(3); |
|
58 |
result.put("info", info); |
|
59 |
result.put("dbSize", dbSize); |
|
60 |
|
|
61 |
List<Map<String, String>> pieList = new ArrayList<>(); |
|
62 |
if (commandStats != null) { |
|
63 |
commandStats.stringPropertyNames().forEach(key -> { |
|
64 |
Map<String, String> data = new HashMap<>(2); |
|
65 |
String property = commandStats.getProperty(key); |
|
66 |
data.put("name", StringUtils.removeStart(key, "cmdstat_")); |
|
67 |
data.put("value", StringUtils.substringBetween(property, "calls=", ",usec")); |
|
68 |
pieList.add(data); |
|
69 |
}); |
|
70 |
} |
|
71 |
result.put("commandStats", pieList); |
|
72 |
return R.ok(result); |
|
73 |
} |
|
74 |
|
|
75 |
/** |
|
76 |
* 获取缓存监控缓存名列表 |
|
77 |
*/ |
|
78 |
@SaCheckPermission("monitor:cache:list") |
|
79 |
@GetMapping("/getNames") |
|
80 |
public R<List<SysCache>> cache() { |
|
81 |
return R.ok(CACHES); |
|
82 |
} |
|
83 |
|
|
84 |
/** |
|
85 |
* 获取缓存监控Key列表 |
|
86 |
* |
|
87 |
* @param cacheName 缓存名 |
|
88 |
*/ |
|
89 |
@SaCheckPermission("monitor:cache:list") |
|
90 |
@GetMapping("/getKeys/{cacheName}") |
|
91 |
public R<Collection<String>> getCacheKeys(@PathVariable String cacheName) { |
|
92 |
Collection<String> cacheKeys = new HashSet<>(0); |
|
93 |
if (isCacheNames(cacheName)) { |
|
94 |
Set<Object> keys = CacheUtils.keys(cacheName); |
|
95 |
if (CollUtil.isNotEmpty(keys)) { |
|
96 |
cacheKeys = keys.stream().map(Object::toString).collect(Collectors.toList()); |
|
97 |
} |
|
98 |
} else { |
|
99 |
cacheKeys = RedisUtils.keys(cacheName + "*"); |
|
100 |
} |
|
101 |
return R.ok(cacheKeys); |
|
102 |
} |
|
103 |
|
|
104 |
/** |
|
105 |
* 获取缓存监控缓存值详情 |
|
106 |
* |
|
107 |
* @param cacheName 缓存名 |
|
108 |
* @param cacheKey 缓存key |
|
109 |
*/ |
|
110 |
@SaCheckPermission("monitor:cache:list") |
|
111 |
@GetMapping("/getValue/{cacheName}/{cacheKey}") |
|
112 |
public R<SysCache> getCacheValue(@PathVariable String cacheName, @PathVariable String cacheKey) { |
|
113 |
Object cacheValue; |
|
114 |
if (isCacheNames(cacheName)) { |
|
115 |
cacheValue = CacheUtils.get(cacheName, cacheKey); |
|
116 |
} else { |
|
117 |
cacheValue = RedisUtils.getCacheObject(cacheKey); |
|
118 |
} |
|
119 |
SysCache sysCache = new SysCache(cacheName, cacheKey, JsonUtils.toJsonString(cacheValue)); |
|
120 |
return R.ok(sysCache); |
|
121 |
} |
|
122 |
|
|
123 |
/** |
|
124 |
* 清理缓存监控缓存名 |
|
125 |
* |
|
126 |
* @param cacheName 缓存名 |
|
127 |
*/ |
|
128 |
@SaCheckPermission("monitor:cache:list") |
|
129 |
@DeleteMapping("/clearCacheName/{cacheName}") |
|
130 |
public R<Void> clearCacheName(@PathVariable String cacheName) { |
|
131 |
if (isCacheNames(cacheName)) { |
|
132 |
CacheUtils.clear(cacheName); |
|
133 |
} else { |
|
134 |
RedisUtils.deleteKeys(cacheName + "*"); |
|
135 |
} |
|
136 |
return R.ok(); |
|
137 |
} |
|
138 |
|
|
139 |
/** |
|
140 |
* 清理缓存监控Key |
|
141 |
* |
|
142 |
* @param cacheKey key名 |
|
143 |
*/ |
|
144 |
@SaCheckPermission("monitor:cache:list") |
|
145 |
@DeleteMapping("/clearCacheKey/{cacheName}/{cacheKey}") |
|
146 |
public R<Void> clearCacheKey(@PathVariable String cacheName, @PathVariable String cacheKey) { |
|
147 |
if (isCacheNames(cacheName)) { |
|
148 |
CacheUtils.evict(cacheName, cacheKey); |
|
149 |
} else { |
|
150 |
RedisUtils.deleteObject(cacheKey); |
|
151 |
} |
|
152 |
return R.ok(); |
|
153 |
} |
|
154 |
|
|
155 |
/** |
|
156 |
* 清理全部缓存监控 |
|
157 |
*/ |
|
158 |
@SaCheckPermission("monitor:cache:list") |
|
159 |
@DeleteMapping("/clearCacheAll") |
|
160 |
public R<Void> clearCacheAll() { |
|
161 |
RedisUtils.deleteKeys("*"); |
|
162 |
return R.ok(); |
|
163 |
} |
|
164 |
|
|
165 |
private boolean isCacheNames(String cacheName) { |
|
166 |
return !StringUtils.contains(cacheName, ":"); |
|
167 |
} |
|
168 |
} |