Skip to content

Support for scan in RedisTemplate #2263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2011-2021 the original author or authors.
* Copyright 2011-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -45,6 +45,7 @@
* @author Mark Paluch
* @author ihaohong
* @author Todd Merrill
* @author Chen Li
*/
public interface RedisOperations<K, V> {

Expand Down Expand Up @@ -259,6 +260,16 @@ <T> T execute(RedisScript<T> script, RedisSerializer<?> argsSerializer, RedisSer
@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.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2011-2021 the original author or authors.
* Copyright 2011-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -83,6 +83,7 @@
* @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
Expand Down Expand Up @@ -897,6 +898,19 @@ public Set<K> keys(K pattern) {
return keySerializer != null ? SerializationUtils.deserialize(rawKeys, keySerializer) : (Set<K>) rawKeys;
}

/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.RedisOperations#scan(java.lang.Object)
*/
@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));
}

/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.RedisOperations#persist(java.lang.Object)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
* @author Mark Paluch
* @author ihaohong
* @author Hendrik Duerkop
* @author Chen Li
*/
@MethodSource("testParams")
public class RedisTemplateIntegrationTests<K, V> {
Expand Down Expand Up @@ -126,6 +127,22 @@ void testKeys() throws Exception {
assertThat(redisTemplate.keys(keyPattern)).isNotNull();
}

@ParameterizedRedisTest // GH-2260
void testScan() {
K key1 = keyFactory.instance();
V value1 = valueFactory.instance();
assumeThat(key1 instanceof String || key1 instanceof byte[]).isTrue();
redisTemplate.opsForValue().set(key1, value1);
Cursor<K> cursor = redisTemplate.scan(ScanOptions.scanOptions().count(1).build());
long count = 0;
while (cursor.hasNext()) {
assertThat(cursor.next()).isEqualTo(key1);
count++;
}
cursor.close();
assertThat(count).isEqualTo(1);
}

@SuppressWarnings("rawtypes")
@ParameterizedRedisTest
void testTemplateNotInitialized() throws Exception {
Expand Down