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..f83aa0c20 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 @@ -4,21 +4,25 @@ 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.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.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.nio.charset.StandardCharsets; +import java.util.HashSet; import java.util.List; import java.util.Properties; +import java.util.Set; import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; @@ -45,11 +49,68 @@ public class RedisController { } @GetMapping("/get-key-list") - @ApiOperation("获得 Redis Key 列表") + @ApiOperation("获得 Redis Key 模板列表") @PreAuthorize("@ss.hasPermission('infra:redis:get-key-list')") public CommonResult> getKeyList() { List keyDefines = RedisKeyRegistry.list(); return success(RedisConvert.INSTANCE.convertList(keyDefines)); } + @GetMapping("/get-key-Defines") + @ApiOperation("获得 Redis keys 键名列表") + @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)); + } + } 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); + } + + @GetMapping("/get-key-value") + @ApiOperation("获得 Redis key 内容") + @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)); + } + + @DeleteMapping("/delete-key-value") + @ApiOperation("删除 Redis Key 根据key") + @PreAuthorize("@ss.hasPermission('infra:redis:get-key-list')") + public CommonResult deleteKeyValue(@RequestParam("cacheKey") String cacheKey) { + stringRedisTemplate.delete(cacheKey); + return success(Boolean.TRUE); + } + + @DeleteMapping("/delete-cache-all") + @ApiOperation(value="删除 所有缓存", notes="不使用该接口") + @PreAuthorize("@ss.hasPermission('infra:redis:get-key-list')") + public CommonResult deleteCacheAll() { + return success(stringRedisTemplate.execute((RedisCallback) connection -> { + connection.flushAll(); + return Boolean.TRUE; + })); + } + } 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..7f186e659 --- /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,34 @@ +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") +@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-ui-admin/src/api/infra/redis.js b/yudao-ui-admin/src/api/infra/redis.js index 12ce00430..ab5ab0489 100644 --- a/yudao-ui-admin/src/api/infra/redis.js +++ b/yudao-ui-admin/src/api/infra/redis.js @@ -8,10 +8,35 @@ export function getCache() { }) } -// TODO +// 获取模块 export function getKeyList() { return request({ url: '/infra/redis/get-key-list', method: 'get' }) } + +// 获取键名列表 +export function getKeyDefines(keyDefine) { + return request({ + url: '/infra/redis/get-key-Defines?keyDefine=' + keyDefine, + method: 'get' + }) +} + +// 获取缓存内容 +export function getKeyValue(keyDefine, key) { + return request({ + url: '/infra/redis/get-key-value?keyDefine=' + keyDefine + "&cacheKey=" + key, + method: 'get' + }) +} + +// 根据键名删除缓存 +export function deleteKeyValue(key) { + return request({ + url: '/infra/redis/delete-key-value?cacheKey=' + key, + method: 'delete' + }) +} + diff --git a/yudao-ui-admin/src/views/infra/redis/index.vue b/yudao-ui-admin/src/views/infra/redis/index.vue index 1db94b9cc..8c09b00bc 100644 --- a/yudao-ui-admin/src/views/infra/redis/index.vue +++ b/yudao-ui-admin/src/views/infra/redis/index.vue @@ -67,32 +67,133 @@ - + + + + + +
+ 键名列表 + +
+ + + + + + + + +
+
+ + + +
+ 缓存内容 + +
+ + + + + + + + + + + + + + + + + + + +
+
+
+
+