|
18 | 18 | import java.nio.charset.StandardCharsets;
|
19 | 19 | import java.time.Duration;
|
20 | 20 | import java.util.Optional;
|
| 21 | +import java.util.function.Consumer; |
21 | 22 |
|
22 | 23 | import org.springframework.cache.Cache;
|
23 | 24 | import org.springframework.cache.interceptor.SimpleKey;
|
24 | 25 | import org.springframework.core.convert.ConversionService;
|
| 26 | +import org.springframework.core.convert.converter.Converter; |
25 | 27 | import org.springframework.core.convert.converter.ConverterRegistry;
|
26 | 28 | import org.springframework.data.redis.serializer.RedisSerializationContext.SerializationPair;
|
27 | 29 | import org.springframework.data.redis.serializer.RedisSerializer;
|
@@ -303,6 +305,37 @@ public ConversionService getConversionService() {
|
303 | 305 | return conversionService;
|
304 | 306 | }
|
305 | 307 |
|
| 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 | + |
306 | 339 | /**
|
307 | 340 | * Registers default cache key converters. The following converters get registered:
|
308 | 341 | * <ul>
|
|
0 commit comments