hdy
2024-03-12 9a80017dfb643f66af44c142f84d8a6d8af42682
提交 | 用户 | 时间
e57a89 1 package com.jcdm.web.controller.monitor;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.HashMap;
6 import java.util.List;
7 import java.util.Map;
8 import java.util.Properties;
9 import java.util.Set;
10 import org.springframework.beans.factory.annotation.Autowired;
11 import org.springframework.data.redis.core.RedisCallback;
12 import org.springframework.data.redis.core.RedisTemplate;
13 import org.springframework.security.access.prepost.PreAuthorize;
14 import org.springframework.web.bind.annotation.DeleteMapping;
15 import org.springframework.web.bind.annotation.GetMapping;
16 import org.springframework.web.bind.annotation.PathVariable;
17 import org.springframework.web.bind.annotation.RequestMapping;
18 import org.springframework.web.bind.annotation.RestController;
19 import com.jcdm.common.constant.CacheConstants;
20 import com.jcdm.common.core.domain.AjaxResult;
21 import com.jcdm.common.utils.StringUtils;
22 import com.jcdm.system.domain.SysCache;
23
24 /**
25  * 缓存监控
26  * 
27  * @author jc
28  */
29 @RestController
30 @RequestMapping("/monitor/cache")
31 public class CacheController
32 {
33     @Autowired
34     private RedisTemplate<String, String> redisTemplate;
35
36     private final static List<SysCache> caches = new ArrayList<SysCache>();
37     {
38         caches.add(new SysCache(CacheConstants.LOGIN_TOKEN_KEY, "用户信息"));
39         caches.add(new SysCache(CacheConstants.SYS_CONFIG_KEY, "配置信息"));
40         caches.add(new SysCache(CacheConstants.SYS_DICT_KEY, "数据字典"));
41         caches.add(new SysCache(CacheConstants.CAPTCHA_CODE_KEY, "验证码"));
42         caches.add(new SysCache(CacheConstants.REPEAT_SUBMIT_KEY, "防重提交"));
43         caches.add(new SysCache(CacheConstants.RATE_LIMIT_KEY, "限流处理"));
44         caches.add(new SysCache(CacheConstants.PWD_ERR_CNT_KEY, "密码错误次数"));
45     }
46
47     @PreAuthorize("@ss.hasPermi('monitor:cache:list')")
48     @GetMapping()
49     public AjaxResult getInfo() throws Exception
50     {
51         Properties info = (Properties) redisTemplate.execute((RedisCallback<Object>) connection -> connection.info());
52         Properties commandStats = (Properties) redisTemplate.execute((RedisCallback<Object>) connection -> connection.info("commandstats"));
53         Object dbSize = redisTemplate.execute((RedisCallback<Object>) connection -> connection.dbSize());
54
55         Map<String, Object> result = new HashMap<>(3);
56         result.put("info", info);
57         result.put("dbSize", dbSize);
58
59         List<Map<String, String>> pieList = new ArrayList<>();
60         commandStats.stringPropertyNames().forEach(key -> {
61             Map<String, String> data = new HashMap<>(2);
62             String property = commandStats.getProperty(key);
63             data.put("name", StringUtils.removeStart(key, "cmdstat_"));
64             data.put("value", StringUtils.substringBetween(property, "calls=", ",usec"));
65             pieList.add(data);
66         });
67         result.put("commandStats", pieList);
68         return AjaxResult.success(result);
69     }
70
71     @PreAuthorize("@ss.hasPermi('monitor:cache:list')")
72     @GetMapping("/getNames")
73     public AjaxResult cache()
74     {
75         return AjaxResult.success(caches);
76     }
77
78     @PreAuthorize("@ss.hasPermi('monitor:cache:list')")
79     @GetMapping("/getKeys/{cacheName}")
80     public AjaxResult getCacheKeys(@PathVariable String cacheName)
81     {
82         Set<String> cacheKeys = redisTemplate.keys(cacheName + "*");
83         return AjaxResult.success(cacheKeys);
84     }
85
86     @PreAuthorize("@ss.hasPermi('monitor:cache:list')")
87     @GetMapping("/getValue/{cacheName}/{cacheKey}")
88     public AjaxResult getCacheValue(@PathVariable String cacheName, @PathVariable String cacheKey)
89     {
90         String cacheValue = redisTemplate.opsForValue().get(cacheKey);
91         SysCache sysCache = new SysCache(cacheName, cacheKey, cacheValue);
92         return AjaxResult.success(sysCache);
93     }
94
95     @PreAuthorize("@ss.hasPermi('monitor:cache:list')")
96     @DeleteMapping("/clearCacheName/{cacheName}")
97     public AjaxResult clearCacheName(@PathVariable String cacheName)
98     {
99         Collection<String> cacheKeys = redisTemplate.keys(cacheName + "*");
100         redisTemplate.delete(cacheKeys);
101         return AjaxResult.success();
102     }
103
104     @PreAuthorize("@ss.hasPermi('monitor:cache:list')")
105     @DeleteMapping("/clearCacheKey/{cacheKey}")
106     public AjaxResult clearCacheKey(@PathVariable String cacheKey)
107     {
108         redisTemplate.delete(cacheKey);
109         return AjaxResult.success();
110     }
111
112     @PreAuthorize("@ss.hasPermi('monitor:cache:list')")
113     @DeleteMapping("/clearCacheAll")
114     public AjaxResult clearCacheAll()
115     {
116         Collection<String> cacheKeys = redisTemplate.keys("*");
117         redisTemplate.delete(cacheKeys);
118         return AjaxResult.success();
119     }
120 }