Skip to content

Commit 5740262

Browse files
Rename clean to clear which aligns with what the method does.
1 parent 74e90b9 commit 5740262

File tree

3 files changed

+69
-19
lines changed

3 files changed

+69
-19
lines changed

src/main/java/org/springframework/data/redis/cache/DefaultRedisCacheWriter.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -438,13 +438,13 @@ public boolean removeIfPresent(String name, byte[] key) {
438438
}
439439

440440
@Override
441-
public void clean(String name, byte[] pattern) {
441+
public void clear(String name, byte[] pattern) {
442442

443443
Assert.notNull(name, "Name must not be null");
444444
Assert.notNull(pattern, "Pattern must not be null");
445445

446446
if (writeAsynchronously()) {
447-
asyncCacheWriter.clean(name, pattern, batchStrategy)
447+
asyncCacheWriter.clear(name, pattern, batchStrategy)
448448
.thenAccept(deleteCount -> statistics.incDeletesBy(name, deleteCount.intValue()));
449449
return;
450450
}
@@ -651,8 +651,9 @@ interface AsyncCacheWriter {
651651
* @param pattern {@link String pattern} used to match Redis keys to clear.
652652
* @param batchStrategy strategy to use.
653653
* @return a future that signals completion emitting the number of removed keys.
654+
* @since 4.0
654655
*/
655-
CompletableFuture<Long> clean(String name, byte[] pattern, BatchStrategy batchStrategy);
656+
CompletableFuture<Long> clear(String name, byte[] pattern, BatchStrategy batchStrategy);
656657

657658
}
658659

@@ -686,7 +687,7 @@ public CompletableFuture<Void> remove(String name, byte[] key) {
686687
}
687688

688689
@Override
689-
public CompletableFuture<Long> clean(String name, byte[] pattern, BatchStrategy batchStrategy) {
690+
public CompletableFuture<Long> clear(String name, byte[] pattern, BatchStrategy batchStrategy) {
690691
throw new UnsupportedOperationException("async clean not supported");
691692
}
692693

@@ -701,10 +702,10 @@ public CompletableFuture<Long> clean(String name, byte[] pattern, BatchStrategy
701702
class AsynchronousCacheWriterDelegate implements AsyncCacheWriter {
702703

703704
private static final int DEFAULT_SCAN_BATCH_SIZE = 64;
704-
private final int cleanBatchSize;
705+
private final int clearBatchSize;
705706

706707
public AsynchronousCacheWriterDelegate() {
707-
this.cleanBatchSize = batchStrategy instanceof BatchStrategies.Scan scan ? scan.batchSize()
708+
this.clearBatchSize = batchStrategy instanceof BatchStrategies.Scan scan ? scan.batchSize()
708709
: DEFAULT_SCAN_BATCH_SIZE;
709710
}
710711

@@ -765,14 +766,14 @@ public CompletableFuture<Void> remove(String name, byte[] key) {
765766
}
766767

767768
@Override
768-
public CompletableFuture<Long> clean(String name, byte[] pattern, BatchStrategy batchStrategy) {
769+
public CompletableFuture<Long> clear(String name, byte[] pattern, BatchStrategy batchStrategy) {
769770

770771
return doWithConnection(connection -> {
771-
return doWithLocking(name, pattern, null, connection, () -> doClean(pattern, connection));
772+
return doWithLocking(name, pattern, null, connection, () -> doClear(pattern, connection));
772773
});
773774
}
774775

775-
private Mono<Long> doClean(byte[] pattern, ReactiveRedisConnection connection) {
776+
private Mono<Long> doClear(byte[] pattern, ReactiveRedisConnection connection) {
776777

777778
ReactiveKeyCommands commands = connection.keyCommands();
778779

@@ -781,11 +782,11 @@ private Mono<Long> doClean(byte[] pattern, ReactiveRedisConnection connection) {
781782
if (batchStrategy instanceof BatchStrategies.Keys) {
782783
keys = commands.keys(ByteBuffer.wrap(pattern)).flatMapMany(Flux::fromIterable);
783784
} else {
784-
keys = commands.scan(ScanOptions.scanOptions().count(cleanBatchSize).match(pattern).build());
785+
keys = commands.scan(ScanOptions.scanOptions().count(clearBatchSize).match(pattern).build());
785786
}
786787

787788
return keys
788-
.buffer(cleanBatchSize) //
789+
.buffer(clearBatchSize) //
789790
.flatMap(commands::mUnlink) //
790791
.collect(Collectors.summingLong(Long::longValue));
791792
}

src/main/java/org/springframework/data/redis/cache/RedisCacheWriter.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,24 @@ default boolean removeIfPresent(String name, byte[] key) {
285285
*
286286
* @param name cache name must not be {@literal null}.
287287
* @param pattern pattern for the keys to remove. Must not be {@literal null}.
288+
* @deprecated since 4.0 in favor of {@link #clear(String, byte[])}
288289
*/
289-
void clean(String name, byte[] pattern);
290+
@Deprecated(since = "4.0", forRemoval = true)
291+
default void clean(String name, byte[] pattern) {
292+
clear(name, pattern);
293+
}
294+
295+
/**
296+
* Remove all keys following the given pattern.
297+
* <p>
298+
* Actual clearing may be performed in an asynchronous or deferred fashion, with subsequent lookups possibly still
299+
* seeing the entries.
300+
*
301+
* @param name cache name must not be {@literal null}.
302+
* @param pattern pattern for the keys to remove. Must not be {@literal null}.
303+
* @since 4.0
304+
*/
305+
void clear(String name, byte[] pattern);
290306

291307
/**
292308
* Remove all keys following the given pattern expecting all entries to be immediately invisible for subsequent
@@ -298,7 +314,7 @@ default boolean removeIfPresent(String name, byte[] key) {
298314
* presence of entries could not be determined).
299315
*/
300316
default boolean invalidate(String name, byte[] pattern) {
301-
clean(name, pattern);
317+
clear(name, pattern);
302318
return false;
303319
}
304320

@@ -379,7 +395,7 @@ default RedisCacheWriterConfigurer enableLocking(Consumer<CacheLockingConfigurat
379395

380396
/**
381397
* Use immediate writes (i.e. write operations such as
382-
* {@link RedisCacheWriter#put(String, byte[], byte[], Duration)} or {@link #clean(String, byte[])}) shall apply
398+
* {@link RedisCacheWriter#put(String, byte[], byte[], Duration)} or {@link #clear(String, byte[])}) shall apply
383399
* immediately.
384400
* <p>
385401
* Several {@link org.springframework.cache.Cache} operations can be performed asynchronously or deferred and this
@@ -395,7 +411,7 @@ default RedisCacheWriterConfigurer immediateWrites() {
395411

396412
/**
397413
* Configure whether to use immediate writes (i.e. write operations such as
398-
* {@link RedisCacheWriter#put(String, byte[], byte[], Duration)} or {@link #clean(String, byte[])}) shall apply
414+
* {@link RedisCacheWriter#put(String, byte[], byte[], Duration)} or {@link #clear(String, byte[])}) shall apply
399415
* immediately.
400416
* <p>
401417
* Several {@link org.springframework.cache.Cache} operations can be performed asynchronously or deferred and this

src/test/java/org/springframework/data/redis/cache/DefaultRedisCacheWriterTests.java

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@
1515
*/
1616
package org.springframework.data.redis.cache;
1717

18-
import static org.assertj.core.api.Assertions.*;
19-
import static org.junit.Assume.*;
20-
import static org.springframework.data.redis.cache.RedisCacheWriter.*;
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
import static org.junit.Assume.assumeTrue;
20+
import static org.springframework.data.redis.cache.RedisCacheWriter.RedisCacheWriterConfigurer;
21+
import static org.springframework.data.redis.cache.RedisCacheWriter.TtlFunction;
22+
import static org.springframework.data.redis.cache.RedisCacheWriter.lockingRedisCacheWriter;
23+
import static org.springframework.data.redis.cache.RedisCacheWriter.nonLockingRedisCacheWriter;
2124

2225
import java.nio.charset.StandardCharsets;
2326
import java.time.Duration;
@@ -31,6 +34,7 @@
3134
import java.util.concurrent.atomic.AtomicLong;
3235
import java.util.concurrent.atomic.AtomicReference;
3336
import java.util.function.Consumer;
37+
import java.util.stream.Stream;
3438

3539
import org.awaitility.Awaitility;
3640
import org.jspecify.annotations.Nullable;
@@ -39,8 +43,9 @@
3943
import org.junit.jupiter.api.Disabled;
4044
import org.junit.jupiter.api.Test;
4145
import org.junit.jupiter.params.ParameterizedClass;
46+
import org.junit.jupiter.params.ParameterizedTest;
47+
import org.junit.jupiter.params.provider.Arguments;
4248
import org.junit.jupiter.params.provider.MethodSource;
43-
4449
import org.springframework.data.redis.connection.RedisConnection;
4550
import org.springframework.data.redis.connection.RedisConnectionFactory;
4651
import org.springframework.data.redis.connection.RedisStringCommands.SetOption;
@@ -78,6 +83,10 @@ public static Collection<Object[]> testParams() {
7883
return CacheTestParams.justConnectionFactories();
7984
}
8085

86+
static Stream<Arguments> clearRemovesAffectedKeysArgs() {
87+
return Stream.of(Arguments.of(BatchStrategies.keys()), Arguments.of(BatchStrategies.scan(37)));
88+
}
89+
8190
@BeforeEach
8291
void setUp() {
8392
doWithConnection(RedisConnection::flushAll);
@@ -533,6 +542,30 @@ void noOpStatisticsCollectorReturnsEmptyStatsInstance() {
533542
assertThat(stats.getPuts()).isZero();
534543
}
535544

545+
@ParameterizedTest // GH-3236
546+
@MethodSource("clearRemovesAffectedKeysArgs")
547+
void clearRemovesAffectedKeys(BatchStrategy batchStrategy) {
548+
549+
DefaultRedisCacheWriter cw = (DefaultRedisCacheWriter) lockingRedisCacheWriter(connectionFactory, batchStrategy)
550+
.withStatisticsCollector(CacheStatisticsCollector.create());
551+
552+
int nrKeys = 100;
553+
for (int i = 0; i < nrKeys; i++) {
554+
cw.putIfAbsent(CACHE_NAME, "%s::key-%s".formatted(CACHE_NAME, i).getBytes(StandardCharsets.UTF_8),
555+
binaryCacheValue, Duration.ofSeconds(30));
556+
}
557+
558+
cw.clear(CACHE_NAME, (CACHE_NAME + "::*").getBytes(StandardCharsets.UTF_8));
559+
560+
doWithConnection(connection -> {
561+
Awaitility.await().pollInSameThread().atMost(Duration.ofSeconds(5)).pollDelay(Duration.ZERO)
562+
.until(() -> !connection.exists(binaryCacheKey));
563+
assertThat(connection.keyCommands().exists(binaryCacheKey)).isFalse();
564+
});
565+
566+
assertThat(cw.getCacheStatistics(CACHE_NAME).getDeletes()).isEqualTo(nrKeys);
567+
}
568+
536569
@Test // GH-1686
537570
@Disabled("Occasional failures on CI but not locally")
538571
void doLockShouldGetLock() throws InterruptedException {

0 commit comments

Comments
 (0)