Skip to content

Commit e4630f9

Browse files
christophstroblmp911de
authored andcommitted
DATAREDIS-1032 - Improve cache key converter registration.
Original pull request: #475.
1 parent 3a77f6a commit e4630f9

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ protected String convertKey(Object key) {
309309
}
310310

311311
throw new IllegalStateException(String.format(
312-
"Cannot convert cache key %s to String. Please provide a suitable Converter via 'RedisCacheConfiguration.withConversionService(...)' or override '%s.toString()'.",
312+
"Cannot convert cache key %s to String. Please register a suitable Converter via 'RedisCacheConfiguration.configureKeyConverters(...)' or override '%s.toString()'.",
313313
source, key != null ? key.getClass().getSimpleName() : "Object"));
314314
}
315315

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

+33
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818
import java.nio.charset.StandardCharsets;
1919
import java.time.Duration;
2020
import java.util.Optional;
21+
import java.util.function.Consumer;
2122

2223
import org.springframework.cache.Cache;
2324
import org.springframework.cache.interceptor.SimpleKey;
2425
import org.springframework.core.convert.ConversionService;
26+
import org.springframework.core.convert.converter.Converter;
2527
import org.springframework.core.convert.converter.ConverterRegistry;
2628
import org.springframework.data.redis.serializer.RedisSerializationContext.SerializationPair;
2729
import org.springframework.data.redis.serializer.RedisSerializer;
@@ -303,6 +305,37 @@ public ConversionService getConversionService() {
303305
return conversionService;
304306
}
305307

308+
/**
309+
* Add a {@link Converter} for extracting the {@link String} representation of a cache key if no suitable
310+
* {@link Object#toString()} method is present.
311+
*
312+
* @param cacheKeyConverter
313+
* @throws IllegalStateException if {@link #getConversionService()} does not allow converter registration.
314+
* @since 2.2
315+
*/
316+
public void addCacheKeyConverter(Converter<?, String> cacheKeyConverter) {
317+
configureKeyConverters(it -> it.addConverter(cacheKeyConverter));
318+
}
319+
320+
/**
321+
* Configure the underlying conversion system used to extract the cache key.
322+
*
323+
* @param registryConsumer never {@literal null}.
324+
* @throws IllegalStateException if {@link #getConversionService()} does not allow converter registration.
325+
* @since 2.2
326+
*/
327+
public void configureKeyConverters(Consumer<ConverterRegistry> registryConsumer) {
328+
329+
if (!(getConversionService() instanceof ConverterRegistry)) {
330+
throw new IllegalStateException(String.format(
331+
"'%s' returned by getConversionService() does not allow converter registration." //
332+
+ " Please make sure to provide a ConversionService that implements ConverterRegistry.",
333+
getConversionService().getClass().getSimpleName()));
334+
}
335+
336+
registryConsumer.accept((ConverterRegistry) getConversionService());
337+
}
338+
306339
/**
307340
* Registers default cache key converters. The following converters get registered:
308341
* <ul>

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

+24
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919

2020
import org.junit.Test;
2121
import org.springframework.beans.DirectFieldAccessor;
22+
import org.springframework.core.convert.converter.Converter;
2223
import org.springframework.instrument.classloading.ShadowingClassLoader;
24+
import org.springframework.lang.Nullable;
2325

2426
/**
2527
* Unit tests for {@link RedisCacheConfiguration}.
@@ -43,4 +45,26 @@ public void shouldSetClassLoader() {
4345

4446
assertThat(usedClassLoader).isSameAs(classLoader);
4547
}
48+
49+
@Test // DATAREDIS-1032
50+
public void shouldAllowConverterRegistration() {
51+
52+
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
53+
config.configureKeyConverters(registry -> registry.addConverter(new DomainTypeConverter()));
54+
55+
assertThat(config.getConversionService().canConvert(DomainType.class, String.class)).isTrue();
56+
}
57+
58+
private static class DomainType {
59+
60+
}
61+
62+
static class DomainTypeConverter implements Converter<DomainType, String> {
63+
64+
@Nullable
65+
@Override
66+
public String convert(DomainType source) {
67+
return null;
68+
}
69+
}
4670
}

0 commit comments

Comments
 (0)