Commit c0b1f62afd4a3041583073a547b485aa0be18ea8
1 parent
a8ebc564
fix(Redis监控): fix review
Showing
4 changed files
with
58 additions
and
32 deletions
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/redis/RedisController.java
| ... | ... | @@ -4,19 +4,25 @@ import cn.iocoder.yudao.framework.common.pojo.CommonResult; |
| 4 | 4 | import cn.iocoder.yudao.framework.redis.core.RedisKeyDefine; |
| 5 | 5 | import cn.iocoder.yudao.framework.redis.core.RedisKeyRegistry; |
| 6 | 6 | import cn.iocoder.yudao.module.infra.controller.admin.redis.vo.RedisKeyRespVO; |
| 7 | +import cn.iocoder.yudao.module.infra.controller.admin.redis.vo.RedisKeyValueRespVO; | |
| 7 | 8 | import cn.iocoder.yudao.module.infra.controller.admin.redis.vo.RedisMonitorRespVO; |
| 8 | -import cn.iocoder.yudao.module.infra.controller.admin.redis.vo.RedisValuesRespVO; | |
| 9 | 9 | import cn.iocoder.yudao.module.infra.convert.redis.RedisConvert; |
| 10 | 10 | import io.swagger.annotations.Api; |
| 11 | 11 | import io.swagger.annotations.ApiOperation; |
| 12 | 12 | import org.springframework.data.redis.connection.RedisServerCommands; |
| 13 | +import org.springframework.data.redis.core.Cursor; | |
| 13 | 14 | import org.springframework.data.redis.core.RedisCallback; |
| 15 | +import org.springframework.data.redis.core.ScanOptions; | |
| 14 | 16 | import org.springframework.data.redis.core.StringRedisTemplate; |
| 15 | 17 | import org.springframework.security.access.prepost.PreAuthorize; |
| 16 | 18 | import org.springframework.web.bind.annotation.*; |
| 17 | 19 | |
| 18 | 20 | import javax.annotation.Resource; |
| 19 | -import java.util.*; | |
| 21 | +import java.nio.charset.StandardCharsets; | |
| 22 | +import java.util.HashSet; | |
| 23 | +import java.util.List; | |
| 24 | +import java.util.Properties; | |
| 25 | +import java.util.Set; | |
| 20 | 26 | |
| 21 | 27 | import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; |
| 22 | 28 | |
| ... | ... | @@ -50,45 +56,61 @@ public class RedisController { |
| 50 | 56 | return success(RedisConvert.INSTANCE.convertList(keyDefines)); |
| 51 | 57 | } |
| 52 | 58 | |
| 53 | - @GetMapping("/get-key/{keyDefine}") | |
| 59 | + @GetMapping("/get-key-Defines") | |
| 54 | 60 | @ApiOperation("获得 Redis keys 键名列表") |
| 55 | 61 | @PreAuthorize("@ss.hasPermission('infra:redis:get-key-list')") |
| 56 | - public CommonResult<Set<String>> getKeyDefineKeys(@PathVariable("keyDefine") String keyDefine) { | |
| 57 | - Set<String> Keys = stringRedisTemplate.keys(keyDefine + "*"); | |
| 58 | - return success(Keys); | |
| 62 | + public CommonResult<Set<String>> getKeyDefines(@RequestParam("keyDefine") String keyDefine) { | |
| 63 | + Set<String> keys = new HashSet<>(); | |
| 64 | + stringRedisTemplate.execute((RedisCallback<Set<String>>) connection -> { | |
| 65 | + try (Cursor<byte[]> cursor = connection.scan(ScanOptions.scanOptions() | |
| 66 | + .match(keyDefine + "*") | |
| 67 | + .count(Integer.MAX_VALUE).build())) { | |
| 68 | + while (cursor.hasNext()) { | |
| 69 | + keys.add(new String(cursor.next(), StandardCharsets.UTF_8)); | |
| 70 | + } | |
| 71 | + } catch (Exception e) { | |
| 72 | + throw new RuntimeException(e); | |
| 73 | + } | |
| 74 | + return keys; | |
| 75 | + }); | |
| 76 | + return success(keys); | |
| 59 | 77 | } |
| 60 | 78 | |
| 61 | - @DeleteMapping("/clear-key-define/{keyDefine}") | |
| 79 | + @DeleteMapping("/delete-key-defines") | |
| 62 | 80 | @ApiOperation("删除 Redis Key 根据模板") |
| 63 | 81 | @PreAuthorize("@ss.hasPermission('infra:redis:get-key-list')") |
| 64 | - public CommonResult<Boolean> clearKeyDefineKeys(@PathVariable("keyDefine") String keyDefine) { | |
| 65 | - stringRedisTemplate.delete(Objects.requireNonNull(stringRedisTemplate.keys(keyDefine + "*"))); | |
| 82 | + public CommonResult<Boolean> deleteKeyDefines(@RequestParam("keyDefine") String keyDefine) { | |
| 83 | + Set<String> keys = stringRedisTemplate.keys(keyDefine + "*"); | |
| 84 | + if(keys != null && keys.isEmpty()){ | |
| 85 | + stringRedisTemplate.delete(keys); | |
| 86 | + } | |
| 66 | 87 | return success(Boolean.TRUE); |
| 67 | 88 | } |
| 68 | 89 | |
| 69 | - @GetMapping("/get-key/{keyDefine}/{cacheKey}") | |
| 90 | + @GetMapping("/get-key-value") | |
| 70 | 91 | @ApiOperation("获得 Redis key 内容") |
| 71 | 92 | @PreAuthorize("@ss.hasPermission('infra:redis:get-key-list')") |
| 72 | - public CommonResult<RedisValuesRespVO> getKeyValue(@PathVariable("keyDefine") String keyDefine, @PathVariable("cacheKey") String cacheKey) { | |
| 93 | + public CommonResult<RedisKeyValueRespVO> getKeyValue(@RequestParam("keyDefine") String keyDefine, @RequestParam("cacheKey") String cacheKey) { | |
| 73 | 94 | String cacheValue = stringRedisTemplate.opsForValue().get(cacheKey); |
| 74 | - return success(new RedisValuesRespVO(keyDefine, cacheKey, cacheValue)); | |
| 95 | + return success(RedisKeyValueRespVO.of(keyDefine, cacheKey, cacheValue)); | |
| 75 | 96 | } |
| 76 | 97 | |
| 77 | - @DeleteMapping("/clear-key/{cacheKey}") | |
| 98 | + @DeleteMapping("/delete-key-value") | |
| 78 | 99 | @ApiOperation("删除 Redis Key 根据key") |
| 79 | 100 | @PreAuthorize("@ss.hasPermission('infra:redis:get-key-list')") |
| 80 | - public CommonResult<Boolean> clearCacheKey(@PathVariable String cacheKey) { | |
| 101 | + public CommonResult<Boolean> deleteKeyValue(@RequestParam("cacheKey") String cacheKey) { | |
| 81 | 102 | stringRedisTemplate.delete(cacheKey); |
| 82 | 103 | return success(Boolean.TRUE); |
| 83 | 104 | } |
| 84 | 105 | |
| 85 | - @DeleteMapping("/clear-cache-all") | |
| 106 | + @DeleteMapping("/delete-cache-all") | |
| 86 | 107 | @ApiOperation(value="删除 所有缓存", notes="不使用该接口") |
| 87 | 108 | @PreAuthorize("@ss.hasPermission('infra:redis:get-key-list')") |
| 88 | - public CommonResult<Boolean> clearCacheAll() { | |
| 89 | - Collection<String> cacheKeys = stringRedisTemplate.keys("*"); | |
| 90 | - stringRedisTemplate.delete(cacheKeys); | |
| 91 | - return success(Boolean.TRUE); | |
| 109 | + public CommonResult<Boolean> deleteCacheAll() { | |
| 110 | + return success(stringRedisTemplate.execute((RedisCallback<Boolean>) connection -> { | |
| 111 | + connection.flushAll(); | |
| 112 | + return Boolean.TRUE; | |
| 113 | + })); | |
| 92 | 114 | } |
| 93 | 115 | |
| 94 | 116 | } | ... | ... |
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/redis/vo/RedisValuesRespVO.java renamed to yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/redis/vo/RedisKeyValueRespVO.java
| ... | ... | @@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.infra.controller.admin.redis.vo; |
| 2 | 2 | |
| 3 | 3 | import io.swagger.annotations.ApiModel; |
| 4 | 4 | import io.swagger.annotations.ApiModelProperty; |
| 5 | +import lombok.AllArgsConstructor; | |
| 5 | 6 | import lombok.Builder; |
| 6 | 7 | import lombok.Data; |
| 7 | 8 | import org.apache.commons.lang3.StringUtils; |
| ... | ... | @@ -9,7 +10,8 @@ import org.apache.commons.lang3.StringUtils; |
| 9 | 10 | @ApiModel("管理后台 - Redis Key Value onse VO") |
| 10 | 11 | @Data |
| 11 | 12 | @Builder |
| 12 | -public class RedisValuesRespVO { | |
| 13 | +@AllArgsConstructor | |
| 14 | +public class RedisKeyValueRespVO { | |
| 13 | 15 | |
| 14 | 16 | @ApiModelProperty(value = "oauth2_access_token:%s", required = true, example = "String") |
| 15 | 17 | private String keyTemplate; |
| ... | ... | @@ -20,10 +22,12 @@ public class RedisValuesRespVO { |
| 20 | 22 | @ApiModelProperty(required = true, example = "String") |
| 21 | 23 | private String value; |
| 22 | 24 | |
| 23 | - public RedisValuesRespVO(String keyTemplate, String key, String value){ | |
| 24 | - this.keyTemplate = StringUtils.replace(keyTemplate, ":", ""); | |
| 25 | - this.key = StringUtils.replace(key, keyTemplate, ""); | |
| 26 | - this.value = value; | |
| 25 | + public static RedisKeyValueRespVO of(String keyTemplate, String key, String value){ | |
| 26 | + return RedisKeyValueRespVO.builder() | |
| 27 | + .keyTemplate(StringUtils.replace(keyTemplate, ":", "")) | |
| 28 | + .key(StringUtils.replace(key, keyTemplate, "")) | |
| 29 | + .value(value) | |
| 30 | + .build(); | |
| 27 | 31 | } |
| 28 | 32 | |
| 29 | 33 | ... | ... |
yudao-ui-admin/src/api/infra/redis.js
| ... | ... | @@ -17,9 +17,9 @@ export function getKeyList() { |
| 17 | 17 | } |
| 18 | 18 | |
| 19 | 19 | // 获取键名列表 |
| 20 | -export function getKeyDefineKeys(keyDefine) { | |
| 20 | +export function getKeyDefines(keyDefine) { | |
| 21 | 21 | return request({ |
| 22 | - url: '/infra/redis/get-key/' + keyDefine, | |
| 22 | + url: '/infra/redis/get-key-Defines?keyDefine=' + keyDefine, | |
| 23 | 23 | method: 'get' |
| 24 | 24 | }) |
| 25 | 25 | } |
| ... | ... | @@ -27,15 +27,15 @@ export function getKeyDefineKeys(keyDefine) { |
| 27 | 27 | // 获取缓存内容 |
| 28 | 28 | export function getKeyValue(keyDefine, key) { |
| 29 | 29 | return request({ |
| 30 | - url: '/infra/redis/get-key/' + keyDefine + "/" + key, | |
| 30 | + url: '/infra/redis/get-key-value?keyDefine=' + keyDefine + "&cacheKey=" + key, | |
| 31 | 31 | method: 'get' |
| 32 | 32 | }) |
| 33 | 33 | } |
| 34 | 34 | |
| 35 | 35 | // 根据键名删除缓存 |
| 36 | -export function clearCacheKey(key) { | |
| 36 | +export function deleteKeyValue(key) { | |
| 37 | 37 | return request({ |
| 38 | - url: '/infra/redis/clear-key/' + key, | |
| 38 | + url: '/infra/redis/delete-key-value?cacheKey=' + key, | |
| 39 | 39 | method: 'delete' |
| 40 | 40 | }) |
| 41 | 41 | } | ... | ... |
yudao-ui-admin/src/views/infra/redis/index.vue
| ... | ... | @@ -188,7 +188,7 @@ |
| 188 | 188 | </template> |
| 189 | 189 | |
| 190 | 190 | <script> |
| 191 | -import { getCache, getKeyList, getKeyDefineKeys, getKeyValue, clearCacheKey } from "@/api/infra/redis"; | |
| 191 | +import { getCache, getKeyList, getKeyDefines, getKeyValue, deleteKeyValue } from "@/api/infra/redis"; | |
| 192 | 192 | import echarts from "echarts"; |
| 193 | 193 | |
| 194 | 194 | export default { |
| ... | ... | @@ -303,7 +303,7 @@ export default { |
| 303 | 303 | // 获取键名列表 |
| 304 | 304 | handleCacheKeys (keyDefine){ |
| 305 | 305 | const cacheName = keyDefine !== undefined ? keyDefine : this.keyTemplate; |
| 306 | - getKeyDefineKeys(cacheName).then(response => { | |
| 306 | + getKeyDefines(cacheName).then(response => { | |
| 307 | 307 | this.cachekeys = response.data |
| 308 | 308 | this.cacheForm = {} |
| 309 | 309 | }) |
| ... | ... | @@ -324,7 +324,7 @@ export default { |
| 324 | 324 | |
| 325 | 325 | // 删除缓存 |
| 326 | 326 | handleClearCacheKey(key){ |
| 327 | - clearCacheKey(key).then(response =>{ | |
| 327 | + deleteKeyValue(key).then(response =>{ | |
| 328 | 328 | this.$modal.msgSuccess("清理缓存键名[" + key + "]成功"); |
| 329 | 329 | this.handleCacheKeys(); |
| 330 | 330 | }) | ... | ... |