Commit 842496a534793659840a5b980496a59be8185686

Authored by YunaiV
1 parent d9114504

增加 redis key 查询接口

src/main/java/cn/iocoder/dashboard/framework/redis/core/RedisKeyDefine.java
1 1 package cn.iocoder.dashboard.framework.redis.core;
2 2  
  3 +import lombok.AllArgsConstructor;
3 4 import lombok.Data;
  5 +import lombok.Getter;
4 6  
5 7 import java.time.Duration;
6 8  
... ... @@ -24,15 +26,20 @@ public class RedisKeyDefine {
24 26  
25 27 }
26 28  
27   - /**
28   - * 过期时间 - 永不过期
29   - */
30   - public static final Duration TIMEOUT_FOREVER = null;
  29 + @Getter
  30 + @AllArgsConstructor
  31 + public enum TimeoutTypeEnum {
31 32  
32   - /**
33   - * 过期时间 - 动态,通过参数传入
34   - */
35   - public static final Duration TIMEOUT_DYNAMIC = null;
  33 + FOREVER(1), // 永不超时
  34 + DYNAMIC(2), // 动态超时
  35 + FIXED(3); // 固定超时
  36 +
  37 + /**
  38 + * 类型
  39 + */
  40 + private final Integer type;
  41 +
  42 + }
36 43  
37 44 /**
38 45 * Key 模板
... ... @@ -49,9 +56,11 @@ public class RedisKeyDefine {
49 56 */
50 57 private final Class<?> valueType;
51 58 /**
  59 + * 超时类型
  60 + */
  61 + private final TimeoutTypeEnum timeoutType;
  62 + /**
52 63 * 过期时间
53   - *
54   - * 为空时,表示永不过期 {@link #TIMEOUT_FOREVER}
55 64 */
56 65 private final Duration timeout;
57 66  
... ... @@ -59,7 +68,20 @@ public class RedisKeyDefine {
59 68 this.keyTemplate = keyTemplate;
60 69 this.keyType = keyType;
61 70 this.valueType = valueType;
  71 + this.timeoutType = TimeoutTypeEnum.FIXED;
62 72 this.timeout = timeout;
  73 + // 添加注册表
  74 + RedisKeyRegistry.add(this);
  75 + }
  76 +
  77 + public RedisKeyDefine(String keyTemplate, KeyTypeEnum keyType, Class<?> valueType, TimeoutTypeEnum timeoutType) {
  78 + this.keyTemplate = keyTemplate;
  79 + this.keyType = keyType;
  80 + this.valueType = valueType;
  81 + this.timeoutType = timeoutType;
  82 + this.timeout = Duration.ZERO;
  83 + // 添加注册表
  84 + RedisKeyRegistry.add(this);
63 85 }
64 86  
65 87 }
... ...
src/main/java/cn/iocoder/dashboard/framework/redis/core/RedisKeyRegistry.java 0 → 100644
  1 +package cn.iocoder.dashboard.framework.redis.core;
  2 +
  3 +import java.util.ArrayList;
  4 +import java.util.List;
  5 +
  6 +/**
  7 + * {@link RedisKeyDefine} 注册表
  8 + */
  9 +public class RedisKeyRegistry {
  10 +
  11 + private static final List<RedisKeyDefine> defines = new ArrayList<>();
  12 +
  13 + public static void add(RedisKeyDefine define) {
  14 + defines.add(define);
  15 + }
  16 +
  17 + public static List<RedisKeyDefine> list() {
  18 + return defines;
  19 + }
  20 +
  21 + public static int size() {
  22 + return defines.size();
  23 + }
  24 +
  25 +}
... ...
src/main/java/cn/iocoder/dashboard/modules/infra/controller/redis/RedisController.http
1   -### 请求 /get-permission-info 接口 => 成功
  1 +### 请求 /infra/redis/get-monitor-info 接口 => 成功
2 2 GET {{baseUrl}}/infra/redis/get-monitor-info
3 3 Authorization: Bearer {{token}}
  4 +
  5 +### 请求 /infra/redis/get-key-list 接口 => 成功
  6 +GET {{baseUrl}}/infra/redis/get-key-list
  7 +Authorization: Bearer {{token}}
... ...
src/main/java/cn/iocoder/dashboard/modules/infra/controller/redis/RedisController.java
... ... @@ -2,18 +2,21 @@ package cn.iocoder.dashboard.modules.infra.controller.redis;
2 2  
3 3 import cn.hutool.core.util.StrUtil;
4 4 import cn.iocoder.dashboard.common.pojo.CommonResult;
  5 +import cn.iocoder.dashboard.framework.redis.core.RedisKeyRegistry;
  6 +import cn.iocoder.dashboard.modules.infra.controller.redis.vo.InfRedisKeyRespVO;
5 7 import cn.iocoder.dashboard.modules.infra.controller.redis.vo.InfRedisMonitorRespVO;
6 8 import org.springframework.data.redis.connection.RedisServerCommands;
7 9 import org.springframework.data.redis.core.RedisCallback;
8 10 import org.springframework.data.redis.core.StringRedisTemplate;
9   -import org.springframework.security.access.prepost.PreAuthorize;
10 11 import org.springframework.web.bind.annotation.GetMapping;
11 12 import org.springframework.web.bind.annotation.RequestMapping;
12 13 import org.springframework.web.bind.annotation.RestController;
13 14  
14 15 import javax.annotation.Resource;
15 16 import java.util.ArrayList;
  17 +import java.util.List;
16 18 import java.util.Properties;
  19 +import java.util.stream.Collectors;
17 20  
18 21 import static cn.iocoder.dashboard.common.pojo.CommonResult.success;
19 22  
... ... @@ -24,13 +27,14 @@ public class RedisController {
24 27 @Resource
25 28 private StringRedisTemplate stringRedisTemplate;
26 29  
27   - @PreAuthorize("@ss.hasPermission('monitor:cache:list')")
  30 +// @PreAuthorize("@ss.hasPermission('infra:redis:get-monitor-info')")
28 31 @GetMapping("/get-monitor-info")
29 32 public CommonResult<InfRedisMonitorRespVO> getRedisMonitorInfo() {
30 33 // 获得 Redis 统计信息
31 34 Properties info = stringRedisTemplate.execute((RedisCallback<Properties>) RedisServerCommands::info);
32 35 Long dbSize = stringRedisTemplate.execute(RedisServerCommands::dbSize);
33   - Properties commandStats = stringRedisTemplate.execute((RedisCallback<Properties>) connection -> connection.info("commandstats"));
  36 + Properties commandStats = stringRedisTemplate.execute((
  37 + RedisCallback<Properties>) connection -> connection.info("commandstats"));
34 38 assert commandStats != null; // 断言,避免警告
35 39  
36 40 // 拼接结果返回
... ... @@ -46,4 +50,19 @@ public class RedisController {
46 50 return success(respVO);
47 51 }
48 52  
  53 +// @PreAuthorize("@ss.hasPermission('infra:redis:get-key-list')")
  54 + @GetMapping("/get-key-list")
  55 + public CommonResult<List<InfRedisKeyRespVO>> getKeyList() {
  56 + List<InfRedisKeyRespVO> respVOList = RedisKeyRegistry.list().stream()
  57 + .map(define -> InfRedisKeyRespVO.builder()
  58 + .keyTemplate(define.getKeyTemplate())
  59 + .keyType(define.getKeyType().name())
  60 + .valueType(define.getValueType().getName())
  61 + .timeoutType(define.getTimeoutType().getType())
  62 + .timeout((int) define.getTimeout().getSeconds())
  63 + .build())
  64 + .collect(Collectors.toList());
  65 + return success(respVOList);
  66 + }
  67 +
49 68 }
... ...
src/main/java/cn/iocoder/dashboard/modules/infra/controller/redis/vo/InfRedisKeyRespVO.java 0 → 100644
  1 +package cn.iocoder.dashboard.modules.infra.controller.redis.vo;
  2 +
  3 +import lombok.AllArgsConstructor;
  4 +import lombok.Builder;
  5 +import lombok.Data;
  6 +
  7 +@Data
  8 +@Builder
  9 +@AllArgsConstructor
  10 +public class InfRedisKeyRespVO {
  11 +
  12 + /**
  13 + * Key 模板
  14 + */
  15 + private final String keyTemplate;
  16 + /**
  17 + * Key 类型的枚举
  18 + */
  19 + private final String keyType;
  20 + /**
  21 + * Value 类型
  22 + */
  23 + private final String valueType;
  24 + /**
  25 + * 超时类型
  26 + */
  27 + private final Integer timeoutType;
  28 + /**
  29 + * 过期时间
  30 + */
  31 + private final Integer timeout;
  32 +
  33 +}
... ...
src/main/java/cn/iocoder/dashboard/modules/system/dal/redis/RedisKeyConstants.java
... ... @@ -6,7 +6,6 @@ import cn.iocoder.dashboard.framework.security.core.LoginUser;
6 6 import java.time.Duration;
7 7  
8 8 import static cn.iocoder.dashboard.framework.redis.core.RedisKeyDefine.KeyTypeEnum.STRING;
9   -import static cn.iocoder.dashboard.framework.redis.core.RedisKeyDefine.TIMEOUT_DYNAMIC;
10 9  
11 10 /**
12 11 * Redis Key 枚举类
... ... @@ -20,13 +19,15 @@ public interface RedisKeyConstants {
20 19 *
21 20 * key 的 format 的参数是 sessionId
22 21 */
23   - RedisKeyDefine LOGIN_USER = new RedisKeyDefine("login_user:%s", STRING, LoginUser.class, Duration.ofMinutes(30));
  22 + RedisKeyDefine LOGIN_USER = new RedisKeyDefine("login_user:%s", STRING, LoginUser.class,
  23 + Duration.ofMinutes(30));
24 24  
25 25 /**
26 26 * 验证码的缓存
27 27 *
28 28 * key 的 format 的参数是 uuid
29 29 */
30   - RedisKeyDefine CAPTCHA_CODE = new RedisKeyDefine("captcha_code:%s", STRING, String.class, TIMEOUT_DYNAMIC);
  30 + RedisKeyDefine CAPTCHA_CODE = new RedisKeyDefine("captcha_code:%s", STRING, String.class,
  31 + RedisKeyDefine.TimeoutTypeEnum.DYNAMIC);
31 32  
32 33 }
... ...