diff --git a/src/main/java/cn/iocoder/dashboard/framework/redis/core/RedisKeyDefine.java b/src/main/java/cn/iocoder/dashboard/framework/redis/core/RedisKeyDefine.java index ee3a342a0..b9adcc3f1 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/redis/core/RedisKeyDefine.java +++ b/src/main/java/cn/iocoder/dashboard/framework/redis/core/RedisKeyDefine.java @@ -1,6 +1,8 @@ package cn.iocoder.dashboard.framework.redis.core; +import lombok.AllArgsConstructor; import lombok.Data; +import lombok.Getter; import java.time.Duration; @@ -24,15 +26,20 @@ public class RedisKeyDefine { } - /** - * 过期时间 - 永不过期 - */ - public static final Duration TIMEOUT_FOREVER = null; + @Getter + @AllArgsConstructor + public enum TimeoutTypeEnum { - /** - * 过期时间 - 动态,通过参数传入 - */ - public static final Duration TIMEOUT_DYNAMIC = null; + FOREVER(1), // 永不超时 + DYNAMIC(2), // 动态超时 + FIXED(3); // 固定超时 + + /** + * 类型 + */ + private final Integer type; + + } /** * Key 模板 @@ -48,10 +55,12 @@ public class RedisKeyDefine { * 如果是使用分布式锁,设置为 {@link java.util.concurrent.locks.Lock} 类型 */ private final Class valueType; + /** + * 超时类型 + */ + private final TimeoutTypeEnum timeoutType; /** * 过期时间 - * - * 为空时,表示永不过期 {@link #TIMEOUT_FOREVER} */ private final Duration timeout; @@ -59,7 +68,20 @@ public class RedisKeyDefine { this.keyTemplate = keyTemplate; this.keyType = keyType; this.valueType = valueType; + this.timeoutType = TimeoutTypeEnum.FIXED; this.timeout = timeout; + // 添加注册表 + RedisKeyRegistry.add(this); + } + + public RedisKeyDefine(String keyTemplate, KeyTypeEnum keyType, Class valueType, TimeoutTypeEnum timeoutType) { + this.keyTemplate = keyTemplate; + this.keyType = keyType; + this.valueType = valueType; + this.timeoutType = timeoutType; + this.timeout = Duration.ZERO; + // 添加注册表 + RedisKeyRegistry.add(this); } } diff --git a/src/main/java/cn/iocoder/dashboard/framework/redis/core/RedisKeyRegistry.java b/src/main/java/cn/iocoder/dashboard/framework/redis/core/RedisKeyRegistry.java new file mode 100644 index 000000000..4a220fbd0 --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/framework/redis/core/RedisKeyRegistry.java @@ -0,0 +1,25 @@ +package cn.iocoder.dashboard.framework.redis.core; + +import java.util.ArrayList; +import java.util.List; + +/** + * {@link RedisKeyDefine} 注册表 + */ +public class RedisKeyRegistry { + + private static final List defines = new ArrayList<>(); + + public static void add(RedisKeyDefine define) { + defines.add(define); + } + + public static List list() { + return defines; + } + + public static int size() { + return defines.size(); + } + +} diff --git a/src/main/java/cn/iocoder/dashboard/modules/infra/controller/redis/RedisController.http b/src/main/java/cn/iocoder/dashboard/modules/infra/controller/redis/RedisController.http index ce46e5e7e..1c474e93f 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/infra/controller/redis/RedisController.http +++ b/src/main/java/cn/iocoder/dashboard/modules/infra/controller/redis/RedisController.http @@ -1,3 +1,7 @@ -### 请求 /get-permission-info 接口 => 成功 +### 请求 /infra/redis/get-monitor-info 接口 => 成功 GET {{baseUrl}}/infra/redis/get-monitor-info Authorization: Bearer {{token}} + +### 请求 /infra/redis/get-key-list 接口 => 成功 +GET {{baseUrl}}/infra/redis/get-key-list +Authorization: Bearer {{token}} diff --git a/src/main/java/cn/iocoder/dashboard/modules/infra/controller/redis/RedisController.java b/src/main/java/cn/iocoder/dashboard/modules/infra/controller/redis/RedisController.java index 63f39cf7a..b40f95aea 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/infra/controller/redis/RedisController.java +++ b/src/main/java/cn/iocoder/dashboard/modules/infra/controller/redis/RedisController.java @@ -2,18 +2,21 @@ package cn.iocoder.dashboard.modules.infra.controller.redis; import cn.hutool.core.util.StrUtil; import cn.iocoder.dashboard.common.pojo.CommonResult; +import cn.iocoder.dashboard.framework.redis.core.RedisKeyRegistry; +import cn.iocoder.dashboard.modules.infra.controller.redis.vo.InfRedisKeyRespVO; import cn.iocoder.dashboard.modules.infra.controller.redis.vo.InfRedisMonitorRespVO; import org.springframework.data.redis.connection.RedisServerCommands; import org.springframework.data.redis.core.RedisCallback; import org.springframework.data.redis.core.StringRedisTemplate; -import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import java.util.ArrayList; +import java.util.List; import java.util.Properties; +import java.util.stream.Collectors; import static cn.iocoder.dashboard.common.pojo.CommonResult.success; @@ -24,13 +27,14 @@ public class RedisController { @Resource private StringRedisTemplate stringRedisTemplate; - @PreAuthorize("@ss.hasPermission('monitor:cache:list')") +// @PreAuthorize("@ss.hasPermission('infra:redis:get-monitor-info')") @GetMapping("/get-monitor-info") public CommonResult getRedisMonitorInfo() { // 获得 Redis 统计信息 Properties info = stringRedisTemplate.execute((RedisCallback) RedisServerCommands::info); Long dbSize = stringRedisTemplate.execute(RedisServerCommands::dbSize); - Properties commandStats = stringRedisTemplate.execute((RedisCallback) connection -> connection.info("commandstats")); + Properties commandStats = stringRedisTemplate.execute(( + RedisCallback) connection -> connection.info("commandstats")); assert commandStats != null; // 断言,避免警告 // 拼接结果返回 @@ -46,4 +50,19 @@ public class RedisController { return success(respVO); } +// @PreAuthorize("@ss.hasPermission('infra:redis:get-key-list')") + @GetMapping("/get-key-list") + public CommonResult> getKeyList() { + List respVOList = RedisKeyRegistry.list().stream() + .map(define -> InfRedisKeyRespVO.builder() + .keyTemplate(define.getKeyTemplate()) + .keyType(define.getKeyType().name()) + .valueType(define.getValueType().getName()) + .timeoutType(define.getTimeoutType().getType()) + .timeout((int) define.getTimeout().getSeconds()) + .build()) + .collect(Collectors.toList()); + return success(respVOList); + } + } diff --git a/src/main/java/cn/iocoder/dashboard/modules/infra/controller/redis/vo/InfRedisKeyRespVO.java b/src/main/java/cn/iocoder/dashboard/modules/infra/controller/redis/vo/InfRedisKeyRespVO.java new file mode 100644 index 000000000..666188d84 --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/modules/infra/controller/redis/vo/InfRedisKeyRespVO.java @@ -0,0 +1,33 @@ +package cn.iocoder.dashboard.modules.infra.controller.redis.vo; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; + +@Data +@Builder +@AllArgsConstructor +public class InfRedisKeyRespVO { + + /** + * Key 模板 + */ + private final String keyTemplate; + /** + * Key 类型的枚举 + */ + private final String keyType; + /** + * Value 类型 + */ + private final String valueType; + /** + * 超时类型 + */ + private final Integer timeoutType; + /** + * 过期时间 + */ + private final Integer timeout; + +} diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/dal/redis/RedisKeyConstants.java b/src/main/java/cn/iocoder/dashboard/modules/system/dal/redis/RedisKeyConstants.java index b21019def..c5ca44578 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/dal/redis/RedisKeyConstants.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/dal/redis/RedisKeyConstants.java @@ -6,7 +6,6 @@ import cn.iocoder.dashboard.framework.security.core.LoginUser; import java.time.Duration; import static cn.iocoder.dashboard.framework.redis.core.RedisKeyDefine.KeyTypeEnum.STRING; -import static cn.iocoder.dashboard.framework.redis.core.RedisKeyDefine.TIMEOUT_DYNAMIC; /** * Redis Key 枚举类 @@ -20,13 +19,15 @@ public interface RedisKeyConstants { * * key 的 format 的参数是 sessionId */ - RedisKeyDefine LOGIN_USER = new RedisKeyDefine("login_user:%s", STRING, LoginUser.class, Duration.ofMinutes(30)); + RedisKeyDefine LOGIN_USER = new RedisKeyDefine("login_user:%s", STRING, LoginUser.class, + Duration.ofMinutes(30)); /** * 验证码的缓存 * * key 的 format 的参数是 uuid */ - RedisKeyDefine CAPTCHA_CODE = new RedisKeyDefine("captcha_code:%s", STRING, String.class, TIMEOUT_DYNAMIC); + RedisKeyDefine CAPTCHA_CODE = new RedisKeyDefine("captcha_code:%s", STRING, String.class, + RedisKeyDefine.TimeoutTypeEnum.DYNAMIC); }