增加 redis key 查询接口

This commit is contained in:
YunaiV 2021-01-27 01:06:58 +08:00
parent d9114504f8
commit 842496a534
6 changed files with 121 additions and 17 deletions

View File

@ -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);
}
}

View File

@ -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<RedisKeyDefine> defines = new ArrayList<>();
public static void add(RedisKeyDefine define) {
defines.add(define);
}
public static List<RedisKeyDefine> list() {
return defines;
}
public static int size() {
return defines.size();
}
}

View File

@ -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}}

View File

@ -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<InfRedisMonitorRespVO> getRedisMonitorInfo() {
// 获得 Redis 统计信息
Properties info = stringRedisTemplate.execute((RedisCallback<Properties>) RedisServerCommands::info);
Long dbSize = stringRedisTemplate.execute(RedisServerCommands::dbSize);
Properties commandStats = stringRedisTemplate.execute((RedisCallback<Properties>) connection -> connection.info("commandstats"));
Properties commandStats = stringRedisTemplate.execute((
RedisCallback<Properties>) 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<List<InfRedisKeyRespVO>> getKeyList() {
List<InfRedisKeyRespVO> 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);
}
}

View File

@ -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;
}

View File

@ -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);
}