diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/redis/RedisController.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/redis/RedisController.java index 303e67184..7d53c7332 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/redis/RedisController.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/redis/RedisController.java @@ -1,24 +1,30 @@ package cn.iocoder.yudao.module.infra.controller.admin.redis; +import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.util.StrUtil; import cn.iocoder.yudao.framework.common.pojo.CommonResult; import cn.iocoder.yudao.framework.redis.core.RedisKeyDefine; import cn.iocoder.yudao.framework.redis.core.RedisKeyRegistry; -import cn.iocoder.yudao.module.infra.controller.admin.redis.vo.RedisKeyRespVO; +import cn.iocoder.yudao.module.infra.controller.admin.redis.vo.RedisKeyDefineRespVO; +import cn.iocoder.yudao.module.infra.controller.admin.redis.vo.RedisKeyValueRespVO; import cn.iocoder.yudao.module.infra.controller.admin.redis.vo.RedisMonitorRespVO; import cn.iocoder.yudao.module.infra.convert.redis.RedisConvert; import io.swagger.annotations.Api; +import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiOperation; import org.springframework.data.redis.connection.RedisServerCommands; +import org.springframework.data.redis.core.Cursor; import org.springframework.data.redis.core.RedisCallback; +import org.springframework.data.redis.core.ScanOptions; 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 org.springframework.web.bind.annotation.*; import javax.annotation.Resource; +import java.util.LinkedHashSet; import java.util.List; import java.util.Properties; +import java.util.Set; import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; @@ -44,12 +50,66 @@ public class RedisController { return success(RedisConvert.INSTANCE.build(info, dbSize, commandStats)); } - @GetMapping("/get-key-list") - @ApiOperation("获得 Redis Key 列表") + @GetMapping("/get-key-define-list") + @ApiOperation("获得 Redis Key 模板列表") @PreAuthorize("@ss.hasPermission('infra:redis:get-key-list')") - public CommonResult> getKeyList() { + public CommonResult> getKeyDefineList() { List keyDefines = RedisKeyRegistry.list(); return success(RedisConvert.INSTANCE.convertList(keyDefines)); } + @GetMapping("/get-key-list") + @ApiOperation("获得 Redis keys 键名列表") + @ApiImplicitParam(name = "keyTemplate", value = "Redis Key 定义", example = "true", dataTypeClass = String.class) + @PreAuthorize("@ss.hasPermission('infra:redis:get-key-list')") + public CommonResult> getKeyDefineList(@RequestParam("keyTemplate") String keyTemplate) { + return success(getKeyDefineList0(keyTemplate)); + } + + private Set getKeyDefineList0(String keyTemplate) { + // key 格式化 + String key = StrUtil.replace(keyTemplate, "%[s|c|b|d|x|o|f|a|e|g]", parameter -> "*"); + // scan 扫描 key + Set keys = new LinkedHashSet<>(); + stringRedisTemplate.execute((RedisCallback>) connection -> { + try (Cursor cursor = connection.scan(ScanOptions.scanOptions().match(key).count(100).build())) { + cursor.forEachRemaining(value -> keys.add(StrUtil.utf8Str(value))); + } catch (Exception e) { + throw new RuntimeException(e); + } + return keys; + }); + return keys; + } + + @GetMapping("/get-key-value") + @ApiOperation("获得 Redis key 内容") + @ApiImplicitParam(name = "key", value = "Redis Key", example = "oauth2_access_token:233", dataTypeClass = String.class) + @PreAuthorize("@ss.hasPermission('infra:redis:get-key-list')") + public CommonResult getKeyValue(@RequestParam("key") String key) { + String value = stringRedisTemplate.opsForValue().get(key); + return success(new RedisKeyValueRespVO(key, value)); + } + + @DeleteMapping("/delete-key") + @ApiOperation("删除 Redis Key") + @ApiImplicitParam(name = "key", value = "Redis Key", example = "oauth2_access_token:233", dataTypeClass = String.class) + @PreAuthorize("@ss.hasPermission('infra:redis:get-key-list')") + public CommonResult deleteKey(@RequestParam("key") String key) { + stringRedisTemplate.delete(key); + return success(Boolean.TRUE); + } + + @DeleteMapping("/delete-keys") + @ApiOperation("删除 Redis Key 根据模板") + @ApiImplicitParam(name = "keyTemplate", value = "Redis Key 定义", example = "true", dataTypeClass = String.class) + @PreAuthorize("@ss.hasPermission('infra:redis:get-key-list')") + public CommonResult deleteKeys(@RequestParam("keyTemplate") String keyTemplate) { + Set keys = getKeyDefineList0(keyTemplate); + if (CollUtil.isNotEmpty(keys)) { + stringRedisTemplate.delete(keys); + } + return success(Boolean.TRUE); + } + } diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/redis/vo/RedisKeyDefineRespVO.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/redis/vo/RedisKeyDefineRespVO.java new file mode 100644 index 000000000..4c1b5acd8 --- /dev/null +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/redis/vo/RedisKeyDefineRespVO.java @@ -0,0 +1,36 @@ +package cn.iocoder.yudao.module.infra.controller.admin.redis.vo; + +import cn.iocoder.yudao.framework.redis.core.RedisKeyDefine; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; + +import java.time.Duration; + +@ApiModel("管理后台 - Redis Key 信息 Response VO") +@Data +@Builder +@AllArgsConstructor +public class RedisKeyDefineRespVO { + + @ApiModelProperty(value = "Key 模板", required = true, example = "login_user:%s") + private String keyTemplate; + + @ApiModelProperty(value = "Key 类型的枚举", required = true, example = "String") + private RedisKeyDefine.KeyTypeEnum keyType; + + @ApiModelProperty(value = "Value 类型", required = true, example = "java.lang.String") + private Class valueType; + + @ApiModelProperty(value = "超时类型", required = true, example = "1") + private RedisKeyDefine.TimeoutTypeEnum timeoutType; + + @ApiModelProperty(value = "过期时间,单位:毫秒", required = true, example = "1024") + private Duration timeout; + + @ApiModelProperty(value = "备注", required = true, example = "啦啦啦啦~") + private String memo; + +} diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/redis/vo/RedisKeyValueRespVO.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/redis/vo/RedisKeyValueRespVO.java new file mode 100644 index 000000000..0f16f0640 --- /dev/null +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/redis/vo/RedisKeyValueRespVO.java @@ -0,0 +1,19 @@ +package cn.iocoder.yudao.module.infra.controller.admin.redis.vo; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.AllArgsConstructor; +import lombok.Data; + +@ApiModel("管理后台 - 单个 Redis Key Value Response VO") +@Data +@AllArgsConstructor +public class RedisKeyValueRespVO { + + @ApiModelProperty(value = "c5f6990767804a928f4bb96ca249febf", required = true, example = "String") + private String key; + + @ApiModelProperty(required = true, example = "String") + private String value; + +} diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/convert/redis/RedisConvert.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/convert/redis/RedisConvert.java index e1c931184..6a1ded7d2 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/convert/redis/RedisConvert.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/convert/redis/RedisConvert.java @@ -2,7 +2,7 @@ package cn.iocoder.yudao.module.infra.convert.redis; import cn.hutool.core.util.StrUtil; import cn.iocoder.yudao.framework.redis.core.RedisKeyDefine; -import cn.iocoder.yudao.module.infra.controller.admin.redis.vo.RedisKeyRespVO; +import cn.iocoder.yudao.module.infra.controller.admin.redis.vo.RedisKeyDefineRespVO; import cn.iocoder.yudao.module.infra.controller.admin.redis.vo.RedisMonitorRespVO; import org.mapstruct.Mapper; import org.mapstruct.factory.Mappers; @@ -29,6 +29,6 @@ public interface RedisConvert { return respVO; } - List convertList(List list); + List convertList(List list); }