update 优化 pr201 代码逻辑
This commit is contained in:
parent
ca301891db
commit
8a930bd7d5
@ -1,12 +1,12 @@
|
||||
package com.ruoyi.web.controller.monitor;
|
||||
|
||||
import cn.dev33.satoken.annotation.SaCheckPermission;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import com.ruoyi.common.constant.CacheConstants;
|
||||
import com.ruoyi.common.core.domain.R;
|
||||
import com.ruoyi.common.utils.JsonUtils;
|
||||
import com.ruoyi.common.utils.StringUtils;
|
||||
import com.ruoyi.common.utils.redis.RedisUtils;
|
||||
import com.ruoyi.oss.constant.OssConstant;
|
||||
import com.ruoyi.system.domain.SysCache;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.redisson.spring.data.connection.RedissonConnectionFactory;
|
||||
@ -38,6 +38,7 @@ public class CacheController {
|
||||
CACHES.add(new SysCache(CacheConstants.CAPTCHA_CODE_KEY, "验证码"));
|
||||
CACHES.add(new SysCache(CacheConstants.REPEAT_SUBMIT_KEY, "防重提交"));
|
||||
CACHES.add(new SysCache(CacheConstants.RATE_LIMIT_KEY, "限流处理"));
|
||||
CACHES.add(new SysCache(OssConstant.SYS_OSS_KEY, "OSS配置"));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -86,8 +87,7 @@ public class CacheController {
|
||||
@SaCheckPermission("monitor:cache:list")
|
||||
@GetMapping("/getKeys/{cacheName}")
|
||||
public R<Collection<String>> getCacheKeys(@PathVariable String cacheName) {
|
||||
Iterable<String> iterable = RedisUtils.getClient().getKeys().getKeysByPattern(cacheName + "*");
|
||||
Collection<String> cacheKyes = CollUtil.toCollection(iterable);
|
||||
Collection<String> cacheKyes = RedisUtils.keys(cacheName + "*");
|
||||
return R.ok(cacheKyes);
|
||||
}
|
||||
|
||||
@ -113,7 +113,7 @@ public class CacheController {
|
||||
@SaCheckPermission("monitor:cache:list")
|
||||
@DeleteMapping("/clearCacheName/{cacheName}")
|
||||
public R<Void> clearCacheName(@PathVariable String cacheName) {
|
||||
RedisUtils.getClient().getKeys().deleteByPattern(cacheName + "*");
|
||||
RedisUtils.deleteKeys(cacheName + "*");
|
||||
return R.ok();
|
||||
}
|
||||
|
||||
@ -135,7 +135,7 @@ public class CacheController {
|
||||
@SaCheckPermission("monitor:cache:list")
|
||||
@DeleteMapping("/clearCacheAll")
|
||||
public R<Void> clearCacheAll() {
|
||||
RedisUtils.getClient().getKeys().deleteByPattern("*");
|
||||
RedisUtils.deleteKeys("*");
|
||||
return R.ok();
|
||||
}
|
||||
|
||||
|
@ -1,10 +1,10 @@
|
||||
package com.ruoyi.common.utils.redis;
|
||||
|
||||
import cn.hutool.core.collection.IterUtil;
|
||||
import com.ruoyi.common.utils.spring.SpringUtils;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.redisson.api.*;
|
||||
import org.redisson.config.Config;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.Collection;
|
||||
@ -12,6 +12,8 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* redis 工具类
|
||||
@ -25,6 +27,14 @@ public class RedisUtils {
|
||||
|
||||
private static final RedissonClient CLIENT = SpringUtils.getBean(RedissonClient.class);
|
||||
|
||||
public static NameMapper getNameMapper() {
|
||||
Config config = CLIENT.getConfig();
|
||||
if (config.isClusterConfig()) {
|
||||
return config.useClusterServers().getNameMapper();
|
||||
}
|
||||
return config.useSingleServer().getNameMapper();
|
||||
}
|
||||
|
||||
/**
|
||||
* 限流
|
||||
*
|
||||
@ -415,8 +425,17 @@ public class RedisUtils {
|
||||
* @return 对象列表
|
||||
*/
|
||||
public static Collection<String> keys(final String pattern) {
|
||||
Iterable<String> iterable = CLIENT.getKeys().getKeysByPattern(pattern);
|
||||
return IterUtil.toList(iterable);
|
||||
Stream<String> stream = CLIENT.getKeys().getKeysStreamByPattern(getNameMapper().map(pattern));
|
||||
return stream.map(key -> getNameMapper().unmap(key)).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除缓存的基本对象列表
|
||||
*
|
||||
* @param pattern 字符串前缀
|
||||
*/
|
||||
public static void deleteKeys(final String pattern) {
|
||||
CLIENT.getKeys().deleteByPattern(getNameMapper().map(pattern));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -426,6 +445,6 @@ public class RedisUtils {
|
||||
*/
|
||||
public static Boolean hasKey(String key) {
|
||||
RKeys rKeys = CLIENT.getKeys();
|
||||
return rKeys.countExists(key) > 0;
|
||||
return rKeys.countExists(getNameMapper().map(key)) > 0;
|
||||
}
|
||||
}
|
||||
|
@ -3,48 +3,48 @@ package com.ruoyi.framework.handler;
|
||||
import com.ruoyi.common.utils.StringUtils;
|
||||
import org.redisson.api.NameMapper;
|
||||
|
||||
/*
|
||||
/**
|
||||
* redis缓存key前缀处理
|
||||
*
|
||||
* @author ye
|
||||
* @create 2022/7/14 17:44
|
||||
* @date 2022/7/14 17:44
|
||||
* @since 4.3.0
|
||||
*/
|
||||
public class KeyPrefixHandler implements NameMapper {
|
||||
|
||||
private final String keyPrefix;
|
||||
private final String keyPrefix;
|
||||
|
||||
//前缀为空 则返回空前缀
|
||||
public KeyPrefixHandler(String keyPrefix) {
|
||||
this.keyPrefix = StringUtils.isBlank(keyPrefix) ? "" : keyPrefix + ":";
|
||||
}
|
||||
public KeyPrefixHandler(String keyPrefix) {
|
||||
//前缀为空 则返回空前缀
|
||||
this.keyPrefix = StringUtils.isBlank(keyPrefix) ? "" : keyPrefix + ":";
|
||||
}
|
||||
|
||||
//增加前缀
|
||||
@Override
|
||||
public String map(String name) {
|
||||
if (StringUtils.isBlank(name)) {
|
||||
return null;
|
||||
/**
|
||||
* 增加前缀
|
||||
*/
|
||||
@Override
|
||||
public String map(String name) {
|
||||
if (StringUtils.isBlank(name)) {
|
||||
return null;
|
||||
}
|
||||
if (StringUtils.isNotBlank(keyPrefix) && !name.startsWith(keyPrefix)) {
|
||||
return keyPrefix + name;
|
||||
}
|
||||
return name;
|
||||
}
|
||||
if (StringUtils.isBlank(keyPrefix)) {
|
||||
return name;
|
||||
}
|
||||
if (!name.startsWith(keyPrefix)) {
|
||||
return keyPrefix + name;
|
||||
} else {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
|
||||
//去除前缀
|
||||
@Override
|
||||
public String unmap(String name) {
|
||||
if (StringUtils.isBlank(name)) {
|
||||
return null;
|
||||
/**
|
||||
* 去除前缀
|
||||
*/
|
||||
@Override
|
||||
public String unmap(String name) {
|
||||
if (StringUtils.isBlank(name)) {
|
||||
return null;
|
||||
}
|
||||
if (StringUtils.isNotBlank(keyPrefix) && name.startsWith(keyPrefix)) {
|
||||
return name.substring(keyPrefix.length());
|
||||
}
|
||||
return name;
|
||||
}
|
||||
if (StringUtils.isBlank(keyPrefix)) {
|
||||
return name;
|
||||
}
|
||||
if (name.startsWith(keyPrefix)) {
|
||||
return name.substring(keyPrefix.length());
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user