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