-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Revise RedisCache and documentation #2051
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
Closed
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
40052ef
Prepare issue branch.
mp911de fc083d0
Add ScanOptions.pattern(…) accepting a byte[] pattern.
mp911de 44f78c2
Add support for configurable batch strategies using RedisCache.
mp911de f1aa5a6
Documentation.
mp911de 0a27cf3
Mention RedisCache locking behavior details.
mp911de 68cef9a
Transition BatchStrategy to interface.
christophstrobl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
src/main/java/org/springframework/data/redis/cache/BatchStrategies.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
/* | ||
* Copyright 2021 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.data.redis.cache; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.NoSuchElementException; | ||
import java.util.Optional; | ||
|
||
import org.springframework.data.redis.connection.RedisConnection; | ||
import org.springframework.data.redis.core.Cursor; | ||
import org.springframework.data.redis.core.ScanOptions; | ||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* A collection of predefined {@link BatchStrategy} implementations using {@code KEYS} or {@code SCAN} command. | ||
* | ||
* @author Mark Paluch | ||
* @author Christoph Strobl | ||
* @since 2.6 | ||
*/ | ||
public abstract class BatchStrategies { | ||
|
||
/** | ||
* A {@link BatchStrategy} using a single {@code KEYS} and {@code DEL} command to remove all matching keys. | ||
* {@code KEYS} scans the entire keyspace of the Redis database and can block the Redis worker thread for a long time | ||
* on large keyspaces. | ||
* <p/> | ||
* {@code KEYS} is supported for standalone and clustered (sharded) Redis operation modes. | ||
* | ||
* @return batching strategy using {@code KEYS}. | ||
*/ | ||
public static BatchStrategy keys() { | ||
return Keys.INSTANCE; | ||
} | ||
|
||
/** | ||
* A {@link BatchStrategy} using a {@code SCAN} cursors and potentially multiple {@code DEL} commands to remove all | ||
* matching keys. This strategy allows a configurable batch size to optimize for scan batching. | ||
* <p/> | ||
* Note that using the {@code SCAN} strategy might be not supported on all drivers and Redis operation modes. | ||
* | ||
* @return batching strategy using {@code SCAN}. | ||
*/ | ||
public static BatchStrategy scan(int batchSize) { | ||
|
||
Assert.isTrue(batchSize > 0, "Batch size must be greater than zero!"); | ||
|
||
return new Scan(batchSize); | ||
} | ||
|
||
/** | ||
* {@link BatchStrategy} using {@code KEYS}. | ||
*/ | ||
static class Keys implements BatchStrategy { | ||
|
||
static Keys INSTANCE = new Keys(); | ||
|
||
@Override | ||
public long cleanCache(RedisConnection connection, String name, byte[] pattern) { | ||
|
||
byte[][] keys = Optional.ofNullable(connection.keys(pattern)).orElse(Collections.emptySet()) | ||
.toArray(new byte[0][]); | ||
|
||
if (keys.length > 0) { | ||
connection.del(keys); | ||
} | ||
|
||
return keys.length; | ||
} | ||
} | ||
|
||
/** | ||
* {@link BatchStrategy} using {@code SCAN}. | ||
*/ | ||
static class Scan implements BatchStrategy { | ||
|
||
private final int batchSize; | ||
|
||
Scan(int batchSize) { | ||
this.batchSize = batchSize; | ||
} | ||
|
||
@Override | ||
public long cleanCache(RedisConnection connection, String name, byte[] pattern) { | ||
|
||
Cursor<byte[]> cursor = connection.scan(ScanOptions.scanOptions().count(batchSize).match(pattern).build()); | ||
|
||
long count = 0; | ||
|
||
PartitionIterator<byte[]> partitions = new PartitionIterator<>(cursor, batchSize); | ||
while (partitions.hasNext()) { | ||
|
||
List<byte[]> keys = partitions.next(); | ||
count += keys.size(); | ||
|
||
if (keys.size() > 0) { | ||
connection.del(keys.toArray(new byte[0][])); | ||
} | ||
} | ||
|
||
return count; | ||
} | ||
} | ||
|
||
/** | ||
* Utility to split and buffer outcome from a {@link Iterator} into {@link List lists} of {@code T} with a maximum | ||
* chunks {@code size}. | ||
* | ||
* @param <T> | ||
*/ | ||
static class PartitionIterator<T> implements Iterator<List<T>> { | ||
|
||
private final Iterator<T> iterator; | ||
private final int size; | ||
|
||
PartitionIterator(Iterator<T> iterator, int size) { | ||
|
||
this.iterator = iterator; | ||
this.size = size; | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return iterator.hasNext(); | ||
} | ||
|
||
@Override | ||
public List<T> next() { | ||
|
||
if (!hasNext()) { | ||
throw new NoSuchElementException(); | ||
} | ||
|
||
List<T> list = new ArrayList<>(size); | ||
while (list.size() < size && iterator.hasNext()) { | ||
list.add(iterator.next()); | ||
} | ||
|
||
return list; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Let's add a private constructor.