CacheController.java 4.2 KB

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