增加 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,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);
}