-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
DATAREDIS-1151 - Provide an example of using scan as an option in RedisCache #547
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
*/ | ||
package org.springframework.data.redis.cache; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.time.Duration; | ||
import java.util.Collections; | ||
|
@@ -27,9 +28,12 @@ | |
import org.springframework.data.redis.connection.RedisConnection; | ||
import org.springframework.data.redis.connection.RedisConnectionFactory; | ||
import org.springframework.data.redis.connection.RedisStringCommands.SetOption; | ||
import org.springframework.data.redis.core.Cursor; | ||
import org.springframework.data.redis.core.ScanOptions; | ||
import org.springframework.data.redis.core.types.Expiration; | ||
import org.springframework.lang.Nullable; | ||
import org.springframework.util.Assert; | ||
import reactor.core.publisher.Flux; | ||
|
||
/** | ||
* {@link RedisCacheWriter} implementation capable of reading/writing binary data from/to Redis in {@literal standalone} | ||
|
@@ -179,12 +183,31 @@ public void clean(String name, byte[] pattern) { | |
wasLocked = true; | ||
} | ||
|
||
byte[][] keys = Optional.ofNullable(connection.keys(pattern)).orElse(Collections.emptySet()) | ||
.toArray(new byte[0][]); | ||
// byte[][] keys = Optional.ofNullable(connection.keys(pattern)).orElse(Collections.emptySet()) | ||
// .toArray(new byte[0][]); | ||
// | ||
// if (keys.length > 0) { | ||
// connection.del(keys); | ||
// } | ||
|
||
if (keys.length > 0) { | ||
connection.del(keys); | ||
int batchSize = 1000; // maybe make it configurable | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the user choose to use |
||
ScanOptions options = ScanOptions.scanOptions().match(new String(pattern)).build(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. batchSize here seems not applied to ScanOptions |
||
|
||
try (Cursor<byte[]> cursor = connection.scan(options)) { | ||
while(cursor.hasNext()) { | ||
byte[][] keys = new byte[batchSize][]; | ||
|
||
for(int i = 0; i < batchSize && cursor.hasNext(); i++) { | ||
keys[i] = cursor.next(); | ||
} | ||
|
||
connection.del(keys); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. keys array may contains null element, which may fail DEL or UNLINK command.
|
||
} | ||
} catch (IOException e) { | ||
// not quite sure about the exception convention in this project | ||
throw new RuntimeException(e); | ||
} | ||
|
||
} finally { | ||
|
||
if (wasLocked && isLockingCacheWriter()) { | ||
|
@@ -234,25 +257,15 @@ private boolean isLockingCacheWriter() { | |
} | ||
|
||
private <T> T execute(String name, Function<RedisConnection, T> callback) { | ||
|
||
RedisConnection connection = connectionFactory.getConnection(); | ||
try { | ||
|
||
try (RedisConnection connection = connectionFactory.getConnection()) { | ||
checkAndPotentiallyWaitUntilUnlocked(name, connection); | ||
return callback.apply(connection); | ||
} finally { | ||
connection.close(); | ||
} | ||
} | ||
|
||
private void executeLockFree(Consumer<RedisConnection> callback) { | ||
|
||
RedisConnection connection = connectionFactory.getConnection(); | ||
|
||
try { | ||
try (RedisConnection connection = connectionFactory.getConnection()) { | ||
callback.accept(connection); | ||
} finally { | ||
connection.close(); | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this is a behavior change for you, maybe we could keep both and use a feature flag to toggle the choice?