diff --git a/starters/cache-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/cache/CacheUtils.java b/starters/cache-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/cache/CacheUtils.java index d9ee294374c7ce9ed5edbfa3e90b49795997b5a0..ab9225676f9be76f67f4b6385b5d7156b6c2f992 100644 --- a/starters/cache-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/cache/CacheUtils.java +++ b/starters/cache-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/cache/CacheUtils.java @@ -162,10 +162,17 @@ public class CacheUtils { } /** - * 模糊搜索 key + * 模糊搜索 key, 默认采用 scan 实现 */ public static Set keys(String pattern) { - return getCacheProvider().keys(pattern); + return keys(pattern, Long.MAX_VALUE); + } + + /** + * 模糊搜索 key, 默认采用 scan 实现 + */ + public static Set keys(String pattern, long limit) { + return getCacheProvider().keys(pattern, limit); } } \ No newline at end of file diff --git a/starters/cache-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/cache/provider/CacheProvider.java b/starters/cache-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/cache/provider/CacheProvider.java index 1c9d264a01425d9dccc668d711672b2a7dc664dc..70337f4da4e08476bac3b7cddce970b37e52dbe1 100644 --- a/starters/cache-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/cache/provider/CacheProvider.java +++ b/starters/cache-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/cache/provider/CacheProvider.java @@ -1,7 +1,10 @@ package com.schbrain.framework.autoconfigure.cache.provider; import java.time.Duration; -import java.util.*; +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.Set; /** * @author zhuyf @@ -30,11 +33,9 @@ public interface CacheProvider { void del(List cacheKeys); /** - * 模糊搜索KEY - * @param pattern - * @return + * 模糊搜索 key, 默认采用 scan 实现 */ - Set keys(String pattern); + Set keys(String pattern, long limit); /** * 缓存获取 diff --git a/starters/cache-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/cache/provider/CacheProviderDelegate.java b/starters/cache-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/cache/provider/CacheProviderDelegate.java index 361d930c002ecd699450ef6ab666875c12d80bfd..0405ffedad4adab784676fbc7606e1bd558f956e 100644 --- a/starters/cache-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/cache/provider/CacheProviderDelegate.java +++ b/starters/cache-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/cache/provider/CacheProviderDelegate.java @@ -63,8 +63,8 @@ public class CacheProviderDelegate implements CacheProvider { } @Override - public Set keys(String pattern) { - Set keys = getCacheProvider().keys(withKeyPrefix(pattern)); + public Set keys(String pattern, long limit) { + Set keys = getCacheProvider().keys(withKeyPrefix(pattern), limit); return StreamUtils.toSet(keys, this::removeKeyPrefix); } diff --git a/starters/cache-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/cache/provider/redis/RedisCacheProvider.java b/starters/cache-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/cache/provider/redis/RedisCacheProvider.java index d25ff38dc483a0f9dc2b96ab89068508570d60df..b426dbc81c6ca202561052cf5627d1fe02a87849 100644 --- a/starters/cache-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/cache/provider/redis/RedisCacheProvider.java +++ b/starters/cache-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/cache/provider/redis/RedisCacheProvider.java @@ -7,8 +7,11 @@ import com.schbrain.framework.autoconfigure.cache.exception.CacheException; import com.schbrain.framework.autoconfigure.cache.provider.CacheProvider; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; +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.data.redis.serializer.RedisSerializer; import org.springframework.util.CollectionUtils; import java.nio.charset.StandardCharsets; @@ -74,8 +77,18 @@ public class RedisCacheProvider implements CacheProvider { * 模糊搜索 key */ @Override - public Set keys(String pattern) { - return redisTemplate.keys(pattern); + public Set keys(String pattern, long limit) { + Set keys = new HashSet<>(); + RedisSerializer keySerializer = redisTemplate.getKeySerializer(); + ScanOptions scanOptions = ScanOptions.scanOptions().match(pattern).count(limit).build(); + Cursor cursor = Objects.requireNonNull(redisTemplate.execute(connection -> connection.scan(scanOptions), true)); + while (cursor.hasNext()) { + Object deserialized = keySerializer.deserialize(cursor.next()); + if (deserialized != null) { + keys.add(deserialized.toString()); + } + } + return keys; } /**