From 8a930bd7d5c2f6625b14622da5015a6ff8273f38 Mon Sep 17 00:00:00 2001 From: 疯狂的狮子li <15040126243@163.com> Date: 星期四, 14 七月 2022 21:26:51 +0800 Subject: [PATCH] update 优化 pr201 代码逻辑 --- ruoyi-framework/src/main/java/com/ruoyi/framework/handler/KeyPrefixHandler.java | 74 ++++++++++++++++++------------------ ruoyi-common/src/main/java/com/ruoyi/common/utils/redis/RedisUtils.java | 27 +++++++++++-- ruoyi-admin/src/main/java/com/ruoyi/web/controller/monitor/CacheController.java | 10 ++-- 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 c1575ae..0f82c4e 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 @@ 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 @@ @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 @@ @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 @@ @SaCheckPermission("monitor:cache:list") @DeleteMapping("/clearCacheAll") public R<Void> 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 7ed3b28..20f3ea3 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.Map; import java.util.Set; import java.util.function.Consumer; +import java.util.stream.Collectors; +import java.util.stream.Stream; /** * redis 宸ュ叿绫� @@ -24,6 +26,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 @@ * @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 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 ddfa064..161c271 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 @@ 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; - } + } -- Gitblit v1.9.3