From 0b7013c007a1e1b91118eace37e2ad9dc555f595 Mon Sep 17 00:00:00 2001 From: liaozan <378024053@qq.com> Date: Tue, 20 Jun 2023 15:13:04 +0800 Subject: [PATCH] Add keys operation --- .../autoconfigure/cache/CacheUtils.java | 11 +++++++++-- .../cache/provider/CacheProvider.java | 11 ++++++----- .../cache/provider/CacheProviderDelegate.java | 4 ++-- .../provider/redis/RedisCacheProvider.java | 17 +++++++++++++++-- 4 files changed, 32 insertions(+), 11 deletions(-) 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 d9ee294..ab92256 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 1c9d264..70337f4 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 361d930..0405ffe 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 d25ff38..b426dbc 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; } /** -- GitLab