From f65ea0b1fb02e0689e595baf7744c0211f77abff Mon Sep 17 00:00:00 2001 From: josroseboom Date: Mon, 1 Aug 2022 10:17:33 +0200 Subject: [PATCH] Add support to clear cache by key pattern. Closes #2379 Original pull request: #2380. --- .../data/redis/cache/RedisCache.java | 14 +++++++++- .../data/redis/cache/RedisCacheTests.java | 27 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/springframework/data/redis/cache/RedisCache.java b/src/main/java/org/springframework/data/redis/cache/RedisCache.java index 59dfb833d..e304eff53 100644 --- a/src/main/java/org/springframework/data/redis/cache/RedisCache.java +++ b/src/main/java/org/springframework/data/redis/cache/RedisCache.java @@ -45,6 +45,7 @@ import org.springframework.util.ReflectionUtils; * @author Christoph Strobl * @author Mark Paluch * @author Piotr Mionskowski + * @author Jos Roseboom * @see RedisCacheConfiguration * @see RedisCacheWriter * @since 2.0 @@ -174,8 +175,19 @@ public class RedisCache extends AbstractValueAdaptingCache { @Override public void clear() { + clearByPattern("*"); + } - byte[] pattern = conversionService.convert(createCacheKey("*"), byte[].class); + /** + *

Clear keys that match the provided pattern.

+ *
+ *

Useful when the cache keys consists of multiple parameters. For example: + * a cache key consists of a brand id and a country id. Now country 42 has relevant data updated. We want to clear all + * the caches that involve country 42, regardless the brand. That can be done by clearByPattern("*42")

+ * @param keyPattern the pattern of the key + */ + public void clearByPattern(String keyPattern) { + byte[] pattern = conversionService.convert(createCacheKey(keyPattern), byte[].class); cacheWriter.clean(name, pattern); } diff --git a/src/test/java/org/springframework/data/redis/cache/RedisCacheTests.java b/src/test/java/org/springframework/data/redis/cache/RedisCacheTests.java index 8a97b3c64..99d332854 100644 --- a/src/test/java/org/springframework/data/redis/cache/RedisCacheTests.java +++ b/src/test/java/org/springframework/data/redis/cache/RedisCacheTests.java @@ -61,6 +61,7 @@ import org.springframework.lang.Nullable; * @author Christoph Strobl * @author Mark Paluch * @author Piotr Mionskowski + * @author Jos Roseboom */ @MethodSource("testParams") public class RedisCacheTests { @@ -108,6 +109,32 @@ public class RedisCacheTests { }); } + @ParameterizedRedisTest // GH-2379 + void cacheShouldBeClearedByPattern() { + + cache.put(key, sample); + + final String keyPattern = "*" + key.substring(1); + cache.clearByPattern(keyPattern); + + doWithConnection(connection -> { + assertThat(connection.exists(binaryCacheKey)).isFalse(); + }); + } + + @ParameterizedRedisTest // GH-2379 + void cacheShouldNotBeClearedIfNoPatternMatch() { + + cache.put(key, sample); + + final String keyPattern = "*" + key.substring(1) + "tail"; + cache.clearByPattern(keyPattern); + + doWithConnection(connection -> { + assertThat(connection.exists(binaryCacheKey)).isTrue(); + }); + } + @ParameterizedRedisTest // DATAREDIS-481 void putNullShouldAddEntryForNullValue() {