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 e304eff53..173e39d58 100644 --- a/src/main/java/org/springframework/data/redis/cache/RedisCache.java +++ b/src/main/java/org/springframework/data/redis/cache/RedisCache.java @@ -175,20 +175,19 @@ public class RedisCache extends AbstractValueAdaptingCache { @Override public void clear() { - clearByPattern("*"); + clear("*"); } /** - *

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")

+ * Clear keys that match the provided {@code keyPattern}. + *

+ * Useful when cache keys are formatted in a style where Redis patterns can be used for matching these. + * * @param keyPattern the pattern of the key + * @since 3.0 */ - public void clearByPattern(String keyPattern) { - byte[] pattern = conversionService.convert(createCacheKey(keyPattern), byte[].class); - cacheWriter.clean(name, pattern); + public void clear(String keyPattern) { + cacheWriter.clean(name, createAndConvertCacheKey(keyPattern)); } /** @@ -288,11 +287,7 @@ public class RedisCache extends AbstractValueAdaptingCache { String convertedKey = convertKey(key); - if (!cacheConfig.usePrefix()) { - return convertedKey; - } - - return prefixCacheKey(convertedKey); + return cacheConfig.usePrefix() ? prefixCacheKey(convertedKey) : convertedKey; } /** 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 99d332854..c08aa81f8 100644 --- a/src/test/java/org/springframework/data/redis/cache/RedisCacheTests.java +++ b/src/test/java/org/springframework/data/redis/cache/RedisCacheTests.java @@ -114,8 +114,8 @@ public class RedisCacheTests { cache.put(key, sample); - final String keyPattern = "*" + key.substring(1); - cache.clearByPattern(keyPattern); + String keyPattern = "*" + key.substring(1); + cache.clear(keyPattern); doWithConnection(connection -> { assertThat(connection.exists(binaryCacheKey)).isFalse(); @@ -127,8 +127,8 @@ public class RedisCacheTests { cache.put(key, sample); - final String keyPattern = "*" + key.substring(1) + "tail"; - cache.clearByPattern(keyPattern); + String keyPattern = "*" + key.substring(1) + "tail"; + cache.clear(keyPattern); doWithConnection(connection -> { assertThat(connection.exists(binaryCacheKey)).isTrue(); @@ -365,9 +365,8 @@ public class RedisCacheTests { void prefixCacheNameCreatesCacheKeyCorrectly() { RedisCache cacheWithCustomPrefix = new RedisCache("cache", - RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory), - RedisCacheConfiguration.defaultCacheConfig().serializeValuesWith(SerializationPair.fromSerializer(serializer)) - .prefixCacheNameWith("redis::")); + RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory), RedisCacheConfiguration.defaultCacheConfig() + .serializeValuesWith(SerializationPair.fromSerializer(serializer)).prefixCacheNameWith("redis::")); cacheWithCustomPrefix.put("key-1", sample); @@ -530,13 +529,13 @@ public class RedisCacheTests { } void doWithConnection(Consumer callback) { - RedisConnection connection = connectionFactory.getConnection(); - try { - callback.accept(connection); - } finally { - connection.close(); - } - } + RedisConnection connection = connectionFactory.getConnection(); + try { + callback.accept(connection); + } finally { + connection.close(); + } + } @Data @NoArgsConstructor