fix(Redis监控): fix review

This commit is contained in:
luowenfeng 2022-07-08 11:47:02 +08:00
parent a8ebc56472
commit c0b1f62afd
4 changed files with 58 additions and 32 deletions

View File

@ -4,19 +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.controller.admin.redis.vo.RedisValuesRespVO;
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.*;
import javax.annotation.Resource;
import java.util.*;
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;
@ -50,45 +56,61 @@ public class RedisController {
return success(RedisConvert.INSTANCE.convertList(keyDefines));
}
@GetMapping("/get-key/{keyDefine}")
@GetMapping("/get-key-Defines")
@ApiOperation("获得 Redis keys 键名列表")
@PreAuthorize("@ss.hasPermission('infra:redis:get-key-list')")
public CommonResult<Set<String>> getKeyDefineKeys(@PathVariable("keyDefine") String keyDefine) {
Set<String> Keys = stringRedisTemplate.keys(keyDefine + "*");
return success(Keys);
public CommonResult<Set<String>> getKeyDefines(@RequestParam("keyDefine") String keyDefine) {
Set<String> keys = new HashSet<>();
stringRedisTemplate.execute((RedisCallback<Set<String>>) connection -> {
try (Cursor<byte[]> 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("/clear-key-define/{keyDefine}")
@DeleteMapping("/delete-key-defines")
@ApiOperation("删除 Redis Key 根据模板")
@PreAuthorize("@ss.hasPermission('infra:redis:get-key-list')")
public CommonResult<Boolean> clearKeyDefineKeys(@PathVariable("keyDefine") String keyDefine) {
stringRedisTemplate.delete(Objects.requireNonNull(stringRedisTemplate.keys(keyDefine + "*")));
public CommonResult<Boolean> deleteKeyDefines(@RequestParam("keyDefine") String keyDefine) {
Set<String> keys = stringRedisTemplate.keys(keyDefine + "*");
if(keys != null && keys.isEmpty()){
stringRedisTemplate.delete(keys);
}
return success(Boolean.TRUE);
}
@GetMapping("/get-key/{keyDefine}/{cacheKey}")
@GetMapping("/get-key-value")
@ApiOperation("获得 Redis key 内容")
@PreAuthorize("@ss.hasPermission('infra:redis:get-key-list')")
public CommonResult<RedisValuesRespVO> getKeyValue(@PathVariable("keyDefine") String keyDefine, @PathVariable("cacheKey") String cacheKey) {
public CommonResult<RedisKeyValueRespVO> getKeyValue(@RequestParam("keyDefine") String keyDefine, @RequestParam("cacheKey") String cacheKey) {
String cacheValue = stringRedisTemplate.opsForValue().get(cacheKey);
return success(new RedisValuesRespVO(keyDefine, cacheKey, cacheValue));
return success(RedisKeyValueRespVO.of(keyDefine, cacheKey, cacheValue));
}
@DeleteMapping("/clear-key/{cacheKey}")
@DeleteMapping("/delete-key-value")
@ApiOperation("删除 Redis Key 根据key")
@PreAuthorize("@ss.hasPermission('infra:redis:get-key-list')")
public CommonResult<Boolean> clearCacheKey(@PathVariable String cacheKey) {
public CommonResult<Boolean> deleteKeyValue(@RequestParam("cacheKey") String cacheKey) {
stringRedisTemplate.delete(cacheKey);
return success(Boolean.TRUE);
}
@DeleteMapping("/clear-cache-all")
@DeleteMapping("/delete-cache-all")
@ApiOperation(value="删除 所有缓存", notes="不使用该接口")
@PreAuthorize("@ss.hasPermission('infra:redis:get-key-list')")
public CommonResult<Boolean> clearCacheAll() {
Collection<String> cacheKeys = stringRedisTemplate.keys("*");
stringRedisTemplate.delete(cacheKeys);
return success(Boolean.TRUE);
public CommonResult<Boolean> deleteCacheAll() {
return success(stringRedisTemplate.execute((RedisCallback<Boolean>) connection -> {
connection.flushAll();
return Boolean.TRUE;
}));
}
}

View File

@ -2,6 +2,7 @@ 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;
@ -9,7 +10,8 @@ import org.apache.commons.lang3.StringUtils;
@ApiModel("管理后台 - Redis Key Value onse VO")
@Data
@Builder
public class RedisValuesRespVO {
@AllArgsConstructor
public class RedisKeyValueRespVO {
@ApiModelProperty(value = "oauth2_access_token:%s", required = true, example = "String")
private String keyTemplate;
@ -20,10 +22,12 @@ public class RedisValuesRespVO {
@ApiModelProperty(required = true, example = "String")
private String value;
public RedisValuesRespVO(String keyTemplate, String key, String value){
this.keyTemplate = StringUtils.replace(keyTemplate, ":", "");
this.key = StringUtils.replace(key, keyTemplate, "");
this.value = 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();
}

View File

@ -17,9 +17,9 @@ export function getKeyList() {
}
// 获取键名列表
export function getKeyDefineKeys(keyDefine) {
export function getKeyDefines(keyDefine) {
return request({
url: '/infra/redis/get-key/' + keyDefine,
url: '/infra/redis/get-key-Defines?keyDefine=' + keyDefine,
method: 'get'
})
}
@ -27,15 +27,15 @@ export function getKeyDefineKeys(keyDefine) {
// 获取缓存内容
export function getKeyValue(keyDefine, key) {
return request({
url: '/infra/redis/get-key/' + keyDefine + "/" + key,
url: '/infra/redis/get-key-value?keyDefine=' + keyDefine + "&cacheKey=" + key,
method: 'get'
})
}
// 根据键名删除缓存
export function clearCacheKey(key) {
export function deleteKeyValue(key) {
return request({
url: '/infra/redis/clear-key/' + key,
url: '/infra/redis/delete-key-value?cacheKey=' + key,
method: 'delete'
})
}

View File

@ -188,7 +188,7 @@
</template>
<script>
import { getCache, getKeyList, getKeyDefineKeys, getKeyValue, clearCacheKey } from "@/api/infra/redis";
import { getCache, getKeyList, getKeyDefines, getKeyValue, deleteKeyValue } from "@/api/infra/redis";
import echarts from "echarts";
export default {
@ -303,7 +303,7 @@ export default {
//
handleCacheKeys (keyDefine){
const cacheName = keyDefine !== undefined ? keyDefine : this.keyTemplate;
getKeyDefineKeys(cacheName).then(response => {
getKeyDefines(cacheName).then(response => {
this.cachekeys = response.data
this.cacheForm = {}
})
@ -324,7 +324,7 @@ export default {
//
handleClearCacheKey(key){
clearCacheKey(key).then(response =>{
deleteKeyValue(key).then(response =>{
this.$modal.msgSuccess("清理缓存键名[" + key + "]成功");
this.handleCacheKeys();
})