完善 Redis 缓存的查询

This commit is contained in:
YunaiV 2022-07-08 20:39:34 +08:00
parent a0f7f0ff12
commit 2372c25e8d
6 changed files with 107 additions and 168 deletions

View File

@ -1,13 +1,16 @@
package cn.iocoder.yudao.module.infra.controller.admin.redis; 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.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.redis.core.RedisKeyDefine; import cn.iocoder.yudao.framework.redis.core.RedisKeyDefine;
import cn.iocoder.yudao.framework.redis.core.RedisKeyRegistry; 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.RedisKeyValueRespVO;
import cn.iocoder.yudao.module.infra.controller.admin.redis.vo.RedisMonitorRespVO; import cn.iocoder.yudao.module.infra.controller.admin.redis.vo.RedisMonitorRespVO;
import cn.iocoder.yudao.module.infra.convert.redis.RedisConvert; import cn.iocoder.yudao.module.infra.convert.redis.RedisConvert;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.connection.RedisServerCommands; import org.springframework.data.redis.connection.RedisServerCommands;
import org.springframework.data.redis.core.Cursor; 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 org.springframework.web.bind.annotation.*;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.nio.charset.StandardCharsets; import java.util.*;
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; 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)); return success(RedisConvert.INSTANCE.build(info, dbSize, commandStats));
} }
@GetMapping("/get-key-list") @GetMapping("/get-key-define-list")
@ApiOperation("获得 Redis Key 模板列表") @ApiOperation("获得 Redis Key 模板列表")
@PreAuthorize("@ss.hasPermission('infra:redis:get-key-list')") @PreAuthorize("@ss.hasPermission('infra:redis:get-key-list')")
public CommonResult<List<RedisKeyRespVO>> getKeyList() { public CommonResult<List<RedisKeyDefineRespVO>> getKeyDefineList() {
List<RedisKeyDefine> keyDefines = RedisKeyRegistry.list(); List<RedisKeyDefine> keyDefines = RedisKeyRegistry.list();
return success(RedisConvert.INSTANCE.convertList(keyDefines)); return success(RedisConvert.INSTANCE.convertList(keyDefines));
} }
@GetMapping("/get-key-Defines") @GetMapping("/get-key-list")
@ApiOperation("获得 Redis keys 键名列表") @ApiOperation("获得 Redis keys 键名列表")
@ApiImplicitParam(name = "keyTemplate", value = "Redis Key 定义", example = "true", dataTypeClass = String.class)
@PreAuthorize("@ss.hasPermission('infra:redis:get-key-list')") @PreAuthorize("@ss.hasPermission('infra:redis:get-key-list')")
public CommonResult<Set<String>> getKeyDefines(@RequestParam("keyDefine") String keyDefine) { public CommonResult<Set<String>> getKeyDefineList(@RequestParam("keyTemplate") String keyTemplate) {
Set<String> keys = new HashSet<>(); return success(getKeyDefineList0(keyTemplate));
stringRedisTemplate.execute((RedisCallback<Set<String>>) connection -> { }
try (Cursor<byte[]> cursor = connection.scan(ScanOptions.scanOptions()
.match(keyDefine + "*") private Set<String> getKeyDefineList0(String keyTemplate) {
.count(Integer.MAX_VALUE).build())) { // key 格式化
while (cursor.hasNext()) { String key = StrUtil.replace(keyTemplate, "%[s|c|b|d|x|o|f|a|e|g]", parameter -> "*");
keys.add(new String(cursor.next(), StandardCharsets.UTF_8)); // scan 扫描 key
} Set<String> keys = new LinkedHashSet<>();
stringRedisTemplate.execute((RedisCallback<Set<String>>) connection -> {
try (Cursor<byte[]> cursor = connection.scan(ScanOptions.scanOptions().match(key).count(100).build())) {
cursor.forEachRemaining(value -> keys.add(StrUtil.utf8Str(value)));
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
return keys; return keys;
}); });
return success(keys); return keys;
}
@DeleteMapping("/delete-key-defines")
@ApiOperation("删除 Redis Key 根据模板")
@PreAuthorize("@ss.hasPermission('infra:redis:get-key-list')")
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-value") @GetMapping("/get-key-value")
@ApiOperation("获得 Redis 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')") @PreAuthorize("@ss.hasPermission('infra:redis:get-key-list')")
public CommonResult<RedisKeyValueRespVO> getKeyValue(@RequestParam("keyDefine") String keyDefine, @RequestParam("cacheKey") String cacheKey) { public CommonResult<RedisKeyValueRespVO> getKeyValue(@RequestParam("key") String key) {
String cacheValue = stringRedisTemplate.opsForValue().get(cacheKey); String value = stringRedisTemplate.opsForValue().get(key);
return success(RedisKeyValueRespVO.of(keyDefine, cacheKey, cacheValue)); return success(new RedisKeyValueRespVO(key, value));
} }
@DeleteMapping("/delete-key-value") @DeleteMapping("/delete-key")
@ApiOperation("删除 Redis Key 根据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')") @PreAuthorize("@ss.hasPermission('infra:redis:get-key-list')")
public CommonResult<Boolean> deleteKeyValue(@RequestParam("cacheKey") String cacheKey) { public CommonResult<Boolean> deleteKey(@RequestParam("key") String key) {
stringRedisTemplate.delete(cacheKey); stringRedisTemplate.delete(key);
return success(Boolean.TRUE); return success(Boolean.TRUE);
} }
@DeleteMapping("/delete-cache-all") @DeleteMapping("/delete-keys")
@ApiOperation(value="删除 所有缓存", notes="不使用该接口") @ApiOperation("删除 Redis Key 根据模板")
@ApiImplicitParam(name = "keyTemplate", value = "Redis Key 定义", example = "true", dataTypeClass = String.class)
@PreAuthorize("@ss.hasPermission('infra:redis:get-key-list')") @PreAuthorize("@ss.hasPermission('infra:redis:get-key-list')")
public CommonResult<Boolean> deleteCacheAll() { public CommonResult<Boolean> deleteKeys(@RequestParam("keyTemplate") String keyTemplate) {
return success(stringRedisTemplate.execute((RedisCallback<Boolean>) connection -> { Set<String> keys = getKeyDefineList0(keyTemplate);
connection.flushAll(); if (CollUtil.isNotEmpty(keys)) {
return Boolean.TRUE; stringRedisTemplate.delete(keys);
})); }
return success(Boolean.TRUE);
} }
} }

View File

@ -13,9 +13,9 @@ import java.time.Duration;
@Data @Data
@Builder @Builder
@AllArgsConstructor @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; private String keyTemplate;
@ApiModelProperty(value = "Key 类型的枚举", required = true, example = "String") @ApiModelProperty(value = "Key 类型的枚举", required = true, example = "String")

View File

@ -3,32 +3,17 @@ package cn.iocoder.yudao.module.infra.controller.admin.redis.vo;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data; import lombok.Data;
import org.apache.commons.lang3.StringUtils;
@ApiModel("管理后台 - Redis Key Value onse VO") @ApiModel("管理后台 - 单个 Redis Key Value Response VO")
@Data @Data
@Builder
@AllArgsConstructor @AllArgsConstructor
public class RedisKeyValueRespVO { public class RedisKeyValueRespVO {
@ApiModelProperty(value = "oauth2_access_token:%s", required = true, example = "String")
private String keyTemplate;
@ApiModelProperty(value = "c5f6990767804a928f4bb96ca249febf", required = true, example = "String") @ApiModelProperty(value = "c5f6990767804a928f4bb96ca249febf", required = true, example = "String")
private String key; private String key;
@ApiModelProperty(required = true, example = "String") @ApiModelProperty(required = true, example = "String")
private String value; 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();
}
} }

View File

@ -2,7 +2,7 @@ package cn.iocoder.yudao.module.infra.convert.redis;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.redis.core.RedisKeyDefine; 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 cn.iocoder.yudao.module.infra.controller.admin.redis.vo.RedisMonitorRespVO;
import org.mapstruct.Mapper; import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers; import org.mapstruct.factory.Mappers;
@ -29,6 +29,6 @@ public interface RedisConvert {
return respVO; return respVO;
} }
List<RedisKeyRespVO> convertList(List<RedisKeyDefine> list); List<RedisKeyDefineRespVO> convertList(List<RedisKeyDefine> list);
} }

View File

@ -9,34 +9,46 @@ export function getCache() {
} }
// 获取模块 // 获取模块
export function getKeyList() { export function getKeyDefineList() {
return request({ return request({
url: '/infra/redis/get-key-list', url: '/infra/redis/get-key-define-list',
method: 'get' method: 'get'
}) })
} }
// 获取键名列表 // 获取键名列表
export function getKeyDefines(keyDefine) { export function getKeyList(keyTemplate) {
return request({ return request({
url: '/infra/redis/get-key-Defines?keyDefine=' + keyDefine, url: '/infra/redis/get-key-list',
method: 'get' method: 'get',
params: {
keyTemplate
}
}) })
} }
// 获取缓存内容 // 获取缓存内容
export function getKeyValue(keyDefine, key) { export function getKeyValue(key) {
return request({ return request({
url: '/infra/redis/get-key-value?keyDefine=' + keyDefine + "&cacheKey=" + key, url: '/infra/redis/get-key-value?key=' + key,
method: 'get' method: 'get'
}) })
} }
// 根据键名删除缓存 // 根据键名删除缓存
export function deleteKeyValue(key) { export function deleteKey(key) {
return request({ return request({
url: '/infra/redis/delete-key-value?cacheKey=' + key, url: '/infra/redis/delete-key?key=' + key,
method: 'delete' method: 'delete'
}) })
} }
export function deleteKeys(keyTemplate) {
return request({
url: '/infra/redis/delete-keys?',
method: 'delete',
params: {
keyTemplate
}
})
}

View File

@ -31,7 +31,7 @@
</tr> </tr>
<tr> <tr>
<td><div class="cell">AOF是否开启</div></td> <td><div class="cell">AOF是否开启</div></td>
<td><div class="cell" v-if="cache.info">{{ cache.info.aof_enabled == "0" ? "" : "" }}</div></td> <td><div class="cell" v-if="cache.info">{{ cache.info.aof_enabled === "0" ? "" : "" }}</div></td>
<td><div class="cell">RDB是否成功</div></td> <td><div class="cell">RDB是否成功</div></td>
<td><div class="cell" v-if="cache.info">{{ cache.info.rdb_last_bgsave_status }}</div></td> <td><div class="cell" v-if="cache.info">{{ cache.info.rdb_last_bgsave_status }}</div></td>
<td><div class="cell">Key数量</div></td> <td><div class="cell">Key数量</div></td>
@ -66,12 +66,7 @@
</el-col> </el-col>
</el-row> </el-row>
<el-table <el-table v-loading="keyDefineListLoad" :data="keyDefineList" row-key="id" @row-click="openKeyTemplate">
v-loading="keyListLoad"
:data="keyList"
row-key="id"
@row-click="openCacheInfo"
>
<el-table-column prop="keyTemplate" label="Key 模板" width="200" /> <el-table-column prop="keyTemplate" label="Key 模板" width="200" />
<el-table-column prop="keyType" label="Key 类型" width="100" /> <el-table-column prop="keyType" label="Key 类型" width="100" />
<el-table-column prop="valueType" label="Value 类型" /> <el-table-column prop="valueType" label="Value 类型" />
@ -90,79 +85,36 @@
</el-table> </el-table>
<!-- 缓存模块信息框 --> <!-- 缓存模块信息框 -->
<el-dialog <el-dialog :title="keyTemplate + ' 模板'" :visible.sync="open" width="70vw" append-to-body>
:title="keyTemplate + '模块'"
:visible.sync="open"
width="60vw"
append-to-body
>
<el-row :gutter="10"> <el-row :gutter="10">
<el-col :span="10" class="card-box"> <el-col :span="14" class="card-box">
<el-card style="height: 70vh"> <el-card style="height: 70vh; overflow: scroll">
<div slot="header"> <div slot="header">
<span>键名列表</span> <span>键名列表</span>
<el-button <el-button style="float: right; padding: 3px 0" type="text" icon="el-icon-refresh-right" @click="refreshKeys" />
style="float: right; padding: 3px 0"
type="text"
icon="el-icon-refresh-right"
@click="refreshCacheKeys"
></el-button>
</div> </div>
<el-table <el-table :data="cacheKeys" style="width: 100%" @row-click="handleKeyValue">
:data="cachekeys" <el-table-column label="缓存键名" align="center" :show-overflow-tooltip="true">
style="width: 100%" <template slot-scope="scope">{{ scope.row }}</template>
@row-click="handleCacheValue"
>
<el-table-column
label="序号"
width="60"
type="index"
></el-table-column>
<el-table-column
label="缓存键名"
align="center"
:show-overflow-tooltip="true"
:formatter="keyFormatter"
>
</el-table-column> </el-table-column>
<el-table-column <el-table-column label="操作" width="60" align="center" class-name="small-padding fixed-width">
label="操作"
width="60"
align="center"
class-name="small-padding fixed-width"
>
<template slot-scope="scope"> <template slot-scope="scope">
<el-button <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDeleteKey(scope.row)" />
size="mini"
type="text"
icon="el-icon-delete"
@click="handleClearCacheKey(scope.row)"
></el-button>
</template> </template>
</el-table-column> </el-table-column>
</el-table> </el-table>
</el-card> </el-card>
</el-col> </el-col>
<el-col :span="14"> <el-col :span="10">
<el-card :bordered="false" style="height: 70vh"> <el-card :bordered="false" style="height: 70vh">
<div slot="header"> <div slot="header">
<span>缓存内容</span> <span>缓存内容</span>
<!-- <el-button <el-button style="float: right; padding: 3px 0" type="text" icon="el-icon-refresh-right"
style="float: right; padding: 3px 0" @click="handleDeleteKeys(keyTemplate)">清理全部</el-button>
type="text"
icon="el-icon-refresh-right"
@click="handleClearCacheAll()"
>清理全部</el-button>
-->
</div> </div>
<el-form :model="cacheForm"> <el-form :model="cacheForm">
<el-row :gutter="32"> <el-row :gutter="32">
<el-col :offset="1" :span="22">
<el-form-item label="缓存名称:" prop="keyTemplate">
<el-input v-model="cacheForm.keyTemplate" :readOnly="true" />
</el-form-item>
</el-col>
<el-col :offset="1" :span="22"> <el-col :offset="1" :span="22">
<el-form-item label="缓存键名:" prop="key"> <el-form-item label="缓存键名:" prop="key">
<el-input v-model="cacheForm.key" :readOnly="true" /> <el-input v-model="cacheForm.key" :readOnly="true" />
@ -170,12 +122,7 @@
</el-col> </el-col>
<el-col :offset="1" :span="22"> <el-col :offset="1" :span="22">
<el-form-item label="缓存内容:" prop="value"> <el-form-item label="缓存内容:" prop="value">
<el-input <el-input v-model="cacheForm.value" type="textarea" :rows="12" :readOnly="true"/>
v-model="cacheForm.value"
type="textarea"
:rows="12"
:readOnly="true"
/>
</el-form-item> </el-form-item>
</el-col> </el-col>
</el-row> </el-row>
@ -188,7 +135,7 @@
</template> </template>
<script> <script>
import { getCache, getKeyList, getKeyDefines, getKeyValue, deleteKeyValue } from "@/api/infra/redis"; import {getCache, getKeyDefineList, getKeyList, getKeyValue, deleteKey, deleteKeys} from "@/api/infra/redis";
import echarts from "echarts"; import echarts from "echarts";
export default { export default {
@ -202,12 +149,12 @@ export default {
// cache // cache
cache: [], cache: [],
// key // key
keyListLoad: true, keyDefineListLoad: true,
keyList: [], keyDefineList: [],
// //
open: false, open: false,
keyTemplate: "", keyTemplate: "",
cachekeys: [], cacheKeys: [],
cacheForm: {} cacheForm: {}
}; };
}, },
@ -275,9 +222,9 @@ export default {
}); });
// Redis Key // Redis Key
getKeyList().then(response => { getKeyDefineList().then(response => {
this.keyList = response.data; this.keyDefineList = response.data;
this.keyListLoad = false; this.keyDefineListLoad = false;
}); });
}, },
@ -287,46 +234,45 @@ export default {
}, },
// //
openCacheInfo (e) { openKeyTemplate (keyDefine) {
this.open = true; this.open = true;
let keyDefine = e.keyTemplate.substring(0, e.keyTemplate.length - 2);
this.keyTemplate = keyDefine;
// //
this.handleCacheKeys(keyDefine); this.keyTemplate = keyDefine.keyTemplate;
}, this.doGetKeyList(this.keyTemplate);
/** 键名前缀去除 */
keyFormatter (cacheKey) {
return cacheKey.replace(this.keyTemplate, "");
}, },
// //
handleCacheKeys (keyDefine){ doGetKeyList (keyTemplate) {
const cacheName = keyDefine !== undefined ? keyDefine : this.keyTemplate; getKeyList(keyTemplate).then(response => {
getKeyDefines(cacheName).then(response => { this.cacheKeys = response.data
this.cachekeys = response.data
this.cacheForm = {} this.cacheForm = {}
}) })
}, },
// //
handleCacheValue (e){ handleKeyValue (key) {
getKeyValue(this.keyTemplate, e).then(response => { getKeyValue(key).then(response => {
this.cacheForm = response.data this.cacheForm = response.data
}) })
}, },
// //
refreshCacheKeys(){ refreshKeys() {
this.$modal.msgSuccess("刷新键名列表成功"); this.$modal.msgSuccess("刷新键名列表成功");
this.handleCacheKeys(); this.doGetKeyList(this.keyTemplate);
}, },
// //
handleClearCacheKey(key){ handleDeleteKey(key){
deleteKeyValue(key).then(response =>{ deleteKey(key).then(response => {
this.$modal.msgSuccess("清理缓存键名[" + key + "]成功"); this.$modal.msgSuccess("清理缓存键名[" + key + "]成功");
this.handleCacheKeys(); this.doGetKeyList(this.keyTemplate);
})
},
handleDeleteKeys(keyTemplate){
deleteKeys(keyTemplate).then(response => {
this.$modal.msgSuccess("清空[" + keyTemplate + "]成功");
this.doGetKeyList(this.keyTemplate);
}) })
}, },
}, },