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 f83aa0c20..3e66b3348 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,13 +1,16 @@ 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; @@ -18,11 +21,7 @@ import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; -import java.nio.charset.StandardCharsets; -import java.util.HashSet; -import java.util.List; -import java.util.Properties; -import java.util.Set; +import java.util.*; import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; @@ -48,69 +47,66 @@ public class RedisController { return success(RedisConvert.INSTANCE.build(info, dbSize, commandStats)); } - @GetMapping("/get-key-list") + @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-Defines") + @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> getKeyDefines(@RequestParam("keyDefine") String keyDefine) { - Set keys = new HashSet<>(); - stringRedisTemplate.execute((RedisCallback>) connection -> { - try (Cursor cursor = connection.scan(ScanOptions.scanOptions() - .match(keyDefine + "*") - .count(Integer.MAX_VALUE).build())) { - while (cursor.hasNext()) { - keys.add(new String(cursor.next(), StandardCharsets.UTF_8)); - } + 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 success(keys); - } - - @DeleteMapping("/delete-key-defines") - @ApiOperation("删除 Redis Key 根据模板") - @PreAuthorize("@ss.hasPermission('infra:redis:get-key-list')") - public CommonResult deleteKeyDefines(@RequestParam("keyDefine") String keyDefine) { - Set keys = stringRedisTemplate.keys(keyDefine + "*"); - if(keys != null && keys.isEmpty()){ - stringRedisTemplate.delete(keys); - } - return success(Boolean.TRUE); + 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("keyDefine") String keyDefine, @RequestParam("cacheKey") String cacheKey) { - String cacheValue = stringRedisTemplate.opsForValue().get(cacheKey); - return success(RedisKeyValueRespVO.of(keyDefine, cacheKey, cacheValue)); + public CommonResult getKeyValue(@RequestParam("key") String key) { + String value = stringRedisTemplate.opsForValue().get(key); + return success(new RedisKeyValueRespVO(key, value)); } - @DeleteMapping("/delete-key-value") - @ApiOperation("删除 Redis Key 根据key") + @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 deleteKeyValue(@RequestParam("cacheKey") String cacheKey) { - stringRedisTemplate.delete(cacheKey); + public CommonResult deleteKey(@RequestParam("key") String key) { + stringRedisTemplate.delete(key); return success(Boolean.TRUE); } - @DeleteMapping("/delete-cache-all") - @ApiOperation(value="删除 所有缓存", notes="不使用该接口") + @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 deleteCacheAll() { - return success(stringRedisTemplate.execute((RedisCallback) connection -> { - connection.flushAll(); - return Boolean.TRUE; - })); + 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/RedisKeyRespVO.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/redis/vo/RedisKeyDefineRespVO.java similarity index 89% rename from yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/redis/vo/RedisKeyRespVO.java rename to yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/redis/vo/RedisKeyDefineRespVO.java index 6fb71fe9a..4c1b5acd8 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/redis/vo/RedisKeyRespVO.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/redis/vo/RedisKeyDefineRespVO.java @@ -13,9 +13,9 @@ import java.time.Duration; @Data @Builder @AllArgsConstructor -public class RedisKeyRespVO { +public class RedisKeyDefineRespVO { - @ApiModelProperty(value = "login_user:%s", required = true, example = "String") + @ApiModelProperty(value = "Key 模板", required = true, example = "login_user:%s") private String keyTemplate; @ApiModelProperty(value = "Key 类型的枚举", required = true, example = "String") 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 index 7f186e659..0f16f0640 100644 --- 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 @@ -3,32 +3,17 @@ 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.Builder; import lombok.Data; -import org.apache.commons.lang3.StringUtils; -@ApiModel("管理后台 - Redis Key Value onse VO") +@ApiModel("管理后台 - 单个 Redis Key Value Response VO") @Data -@Builder @AllArgsConstructor public class RedisKeyValueRespVO { - @ApiModelProperty(value = "oauth2_access_token:%s", required = true, example = "String") - private String keyTemplate; - @ApiModelProperty(value = "c5f6990767804a928f4bb96ca249febf", required = true, example = "String") private String key; @ApiModelProperty(required = true, example = "String") private String value; - public static RedisKeyValueRespVO of(String keyTemplate, String key, String value){ - return RedisKeyValueRespVO.builder() - .keyTemplate(StringUtils.replace(keyTemplate, ":", "")) - .key(StringUtils.replace(key, keyTemplate, "")) - .value(value) - .build(); - } - - } 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); } diff --git a/yudao-ui-admin/src/api/infra/redis.js b/yudao-ui-admin/src/api/infra/redis.js index ab5ab0489..dadc221bf 100644 --- a/yudao-ui-admin/src/api/infra/redis.js +++ b/yudao-ui-admin/src/api/infra/redis.js @@ -9,34 +9,46 @@ export function getCache() { } // 获取模块 -export function getKeyList() { +export function getKeyDefineList() { return request({ - url: '/infra/redis/get-key-list', + url: '/infra/redis/get-key-define-list', method: 'get' }) } // 获取键名列表 -export function getKeyDefines(keyDefine) { +export function getKeyList(keyTemplate) { return request({ - url: '/infra/redis/get-key-Defines?keyDefine=' + keyDefine, - method: 'get' + url: '/infra/redis/get-key-list', + method: 'get', + params: { + keyTemplate + } }) } // 获取缓存内容 -export function getKeyValue(keyDefine, key) { +export function getKeyValue(key) { return request({ - url: '/infra/redis/get-key-value?keyDefine=' + keyDefine + "&cacheKey=" + key, + url: '/infra/redis/get-key-value?key=' + key, method: 'get' }) } // 根据键名删除缓存 -export function deleteKeyValue(key) { +export function deleteKey(key) { return request({ - url: '/infra/redis/delete-key-value?cacheKey=' + key, + url: '/infra/redis/delete-key?key=' + key, method: 'delete' }) } +export function deleteKeys(keyTemplate) { + return request({ + url: '/infra/redis/delete-keys?', + method: 'delete', + params: { + keyTemplate + } + }) +} diff --git a/yudao-ui-admin/src/views/infra/redis/index.vue b/yudao-ui-admin/src/views/infra/redis/index.vue index 8c09b00bc..16b2f94f0 100644 --- a/yudao-ui-admin/src/views/infra/redis/index.vue +++ b/yudao-ui-admin/src/views/infra/redis/index.vue @@ -31,7 +31,7 @@
AOF是否开启
-
{{ cache.info.aof_enabled == "0" ? "否" : "是" }}
+
{{ cache.info.aof_enabled === "0" ? "否" : "是" }}
RDB是否成功
{{ cache.info.rdb_last_bgsave_status }}
Key数量
@@ -66,12 +66,7 @@ - + @@ -90,79 +85,36 @@ - + - - + +
键名列表 - +
- - - + + + - +
- +
缓存内容 - + 清理全部
- - - - - @@ -170,12 +122,7 @@ - + @@ -188,7 +135,7 @@