Support scan in RedisTemplate.

Closes #2260
Original pull request: #2263.
This commit is contained in:
Chen Li
2022-02-17 13:57:31 +08:00
committed by Mark Paluch
parent 31cc3bda6a
commit 04165fb747
3 changed files with 38 additions and 0 deletions

View File

@@ -45,6 +45,7 @@ import org.springframework.util.Assert;
* @author Mark Paluch
* @author ihaohong
* @author Todd Merrill
* @author Chen Li
*/
public interface RedisOperations<K, V> {
@@ -259,6 +260,16 @@ public interface RedisOperations<K, V> {
@Nullable
Set<K> keys(K pattern);
/**
* Use a {@link Cursor} to iterate over keys. <br />
* <strong>Important:</strong> Call {@link Cursor#close()} when done to avoid resource leak.
*
* @param options must not be {@literal null}.
* @return never {@literal null}.
* @see <a href="https://redis.io/commands/scan">Redis Documentation: SCAN</a>
*/
Cursor<K> scan(ScanOptions options);
/**
* Return a random key from the keyspace.
*

View File

@@ -83,6 +83,7 @@ import org.springframework.util.CollectionUtils;
* @author Mark Paluch
* @author Denis Zavedeev
* @author ihaohong
* @author Chen Li
* @param <K> the Redis key type against which the template works (usually a String)
* @param <V> the Redis value type against which the template works
* @see StringRedisTemplate
@@ -801,6 +802,15 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
return keySerializer != null ? SerializationUtils.deserialize(rawKeys, keySerializer) : (Set<K>) rawKeys;
}
@Override
public Cursor<K> scan(ScanOptions options) {
Assert.notNull(options, "ScanOptions must not be null!");
return executeWithStickyConnection(
(RedisCallback<Cursor<K>>) connection -> new ConvertingCursor<>(connection.scan(options),
this::deserializeKey));
}
@Override
public Boolean persist(K key) {