Skip to content

Commit 6b1038c

Browse files
committed
Polishing.
Consistently use clear(…) as method name. Use configured cache key serializer instead of the conversion service. See #2379 Original pull request: #2380.
1 parent f65ea0b commit 6b1038c

File tree

2 files changed

+22
-28
lines changed

2 files changed

+22
-28
lines changed

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

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -175,20 +175,19 @@ public void evict(Object key) {
175175

176176
@Override
177177
public void clear() {
178-
clearByPattern("*");
178+
clear("*");
179179
}
180180

181181
/**
182-
* <p>Clear keys that match the provided pattern.</p>
183-
* <br />
184-
* <p>Useful when the cache keys consists of multiple parameters. For example:
185-
* a cache key consists of a brand id and a country id. Now country 42 has relevant data updated. We want to clear all
186-
* the caches that involve country 42, regardless the brand. That can be done by clearByPattern("*42")</p>
182+
* Clear keys that match the provided {@code keyPattern}.
183+
* <p>
184+
* Useful when cache keys are formatted in a style where Redis patterns can be used for matching these.
185+
*
187186
* @param keyPattern the pattern of the key
187+
* @since 3.0
188188
*/
189-
public void clearByPattern(String keyPattern) {
190-
byte[] pattern = conversionService.convert(createCacheKey(keyPattern), byte[].class);
191-
cacheWriter.clean(name, pattern);
189+
public void clear(String keyPattern) {
190+
cacheWriter.clean(name, createAndConvertCacheKey(keyPattern));
192191
}
193192

194193
/**
@@ -288,11 +287,7 @@ protected String createCacheKey(Object key) {
288287

289288
String convertedKey = convertKey(key);
290289

291-
if (!cacheConfig.usePrefix()) {
292-
return convertedKey;
293-
}
294-
295-
return prefixCacheKey(convertedKey);
290+
return cacheConfig.usePrefix() ? prefixCacheKey(convertedKey) : convertedKey;
296291
}
297292

298293
/**

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

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ void cacheShouldBeClearedByPattern() {
114114

115115
cache.put(key, sample);
116116

117-
final String keyPattern = "*" + key.substring(1);
118-
cache.clearByPattern(keyPattern);
117+
String keyPattern = "*" + key.substring(1);
118+
cache.clear(keyPattern);
119119

120120
doWithConnection(connection -> {
121121
assertThat(connection.exists(binaryCacheKey)).isFalse();
@@ -127,8 +127,8 @@ void cacheShouldNotBeClearedIfNoPatternMatch() {
127127

128128
cache.put(key, sample);
129129

130-
final String keyPattern = "*" + key.substring(1) + "tail";
131-
cache.clearByPattern(keyPattern);
130+
String keyPattern = "*" + key.substring(1) + "tail";
131+
cache.clear(keyPattern);
132132

133133
doWithConnection(connection -> {
134134
assertThat(connection.exists(binaryCacheKey)).isTrue();
@@ -365,9 +365,8 @@ void computePrefixCreatesCacheKeyCorrectly() {
365365
void prefixCacheNameCreatesCacheKeyCorrectly() {
366366

367367
RedisCache cacheWithCustomPrefix = new RedisCache("cache",
368-
RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory),
369-
RedisCacheConfiguration.defaultCacheConfig().serializeValuesWith(SerializationPair.fromSerializer(serializer))
370-
.prefixCacheNameWith("redis::"));
368+
RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory), RedisCacheConfiguration.defaultCacheConfig()
369+
.serializeValuesWith(SerializationPair.fromSerializer(serializer)).prefixCacheNameWith("redis::"));
371370

372371
cacheWithCustomPrefix.put("key-1", sample);
373372

@@ -530,13 +529,13 @@ public CacheStatistics getCacheStatistics(String cacheName) {
530529
}
531530

532531
void doWithConnection(Consumer<RedisConnection> callback) {
533-
RedisConnection connection = connectionFactory.getConnection();
534-
try {
535-
callback.accept(connection);
536-
} finally {
537-
connection.close();
538-
}
539-
}
532+
RedisConnection connection = connectionFactory.getConnection();
533+
try {
534+
callback.accept(connection);
535+
} finally {
536+
connection.close();
537+
}
538+
}
540539

541540
@Data
542541
@NoArgsConstructor

0 commit comments

Comments
 (0)