Skip to content
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

Closed
wants to merge 1 commit into from
Closed
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
Expand Up @@ -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;
Expand All @@ -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}
Expand Down Expand Up @@ -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);
// }
Copy link
Author

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?


if (keys.length > 0) {
connection.del(keys);
int batchSize = 1000; // maybe make it configurable
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the user choose to use SCAN approach, we could let them configure this batch size as well, so that the Redis timeout option and batch delete size option are both in the hands of users.

ScanOptions options = ScanOptions.scanOptions().match(new String(pattern)).build();
Copy link

Choose a reason for hiding this comment

The 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);
Copy link

@rickgong rickgong Apr 8, 2021

Choose a reason for hiding this comment

The 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.
Also, we can check if redis support UNLINK command.

private boolean isUnLinkSupported(RedisConnection redisConnection) {
	if (unlinkSupported == null) {//private volatile Boolean unlinkSupported = null;
		String smokingKey = "unlink-test" + System.currentTimeMillis() + UUID.randomUUID().toString();
		try {
			redisConnection.unlink(smokingKey.getBytes());
			unlinkSupported = true;
		} catch (RedisSystemException e) {
			unlinkSupported = false;
		}
	}
	return unlinkSupported;
}

}
} catch (IOException e) {
// not quite sure about the exception convention in this project
throw new RuntimeException(e);
}

} finally {

if (wasLocked && isLockingCacheWriter()) {
Expand Down Expand Up @@ -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();
}
}

Expand Down