From 8a930bd7d5c2f6625b14622da5015a6ff8273f38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90li?= <15040126243@163.com> Date: Thu, 14 Jul 2022 21:26:51 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E4=BC=98=E5=8C=96=20pr201=20=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/monitor/CacheController.java | 10 +-- .../ruoyi/common/utils/redis/RedisUtils.java | 27 ++++++- .../framework/handler/KeyPrefixHandler.java | 74 +++++++++---------- 3 files changed, 65 insertions(+), 46 deletions(-) diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/monitor/CacheController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/monitor/CacheController.java index c1575ae7b..0f82c4eb5 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/monitor/CacheController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/monitor/CacheController.java @@ -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> getCacheKeys(@PathVariable String cacheName) { - Iterable iterable = RedisUtils.getClient().getKeys().getKeysByPattern(cacheName + "*"); - Collection cacheKyes = CollUtil.toCollection(iterable); + Collection cacheKyes = RedisUtils.keys(cacheName + "*"); return R.ok(cacheKyes); } @@ -113,7 +113,7 @@ public class CacheController { @SaCheckPermission("monitor:cache:list") @DeleteMapping("/clearCacheName/{cacheName}") public R 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 clearCacheAll() { - RedisUtils.getClient().getKeys().deleteByPattern("*"); + RedisUtils.deleteKeys("*"); return R.ok(); } diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/redis/RedisUtils.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/redis/RedisUtils.java index 7ed3b2827..20f3ea36d 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/redis/RedisUtils.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/redis/RedisUtils.java @@ -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 keys(final String pattern) { - Iterable iterable = CLIENT.getKeys().getKeysByPattern(pattern); - return IterUtil.toList(iterable); + Stream 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; } } diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/handler/KeyPrefixHandler.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/handler/KeyPrefixHandler.java index ddfa064e9..161c271b3 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/handler/KeyPrefixHandler.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/handler/KeyPrefixHandler.java @@ -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; - - //前缀为空 则返回空前缀 - public KeyPrefixHandler(String keyPrefix) { - this.keyPrefix = StringUtils.isBlank(keyPrefix) ? "" : keyPrefix + ":"; - } - - //增加前缀 - @Override - public String map(String name) { - if (StringUtils.isBlank(name)) { - return null; + + private final String keyPrefix; + + public KeyPrefixHandler(String keyPrefix) { + //前缀为空 则返回空前缀 + this.keyPrefix = StringUtils.isBlank(keyPrefix) ? "" : keyPrefix + ":"; } - if (StringUtils.isBlank(keyPrefix)) { - return name; + + /** + * 增加前缀 + */ + @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 (!name.startsWith(keyPrefix)) { - return keyPrefix + name; - } else { - return name; + + /** + * 去除前缀 + */ + @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; } - } - - //去除前缀 - @Override - public String unmap(String name) { - if (StringUtils.isBlank(name)) { - return null; - } - if (StringUtils.isBlank(keyPrefix)) { - return name; - } - if (name.startsWith(keyPrefix)) { - return name.substring(keyPrefix.length()); - } - return name; - } + }