diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/RedisCacheConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/RedisCacheConfiguration.java index 56c1bb6f6d..16fa5dd672 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/RedisCacheConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/RedisCacheConfiguration.java @@ -52,6 +52,7 @@ class RedisCacheConfiguration { @Bean public RedisCacheManager cacheManager(RedisTemplate redisTemplate) { RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate); + cacheManager.setUsePrefix(true); List cacheNames = this.cacheProperties.getCacheNames(); if (!cacheNames.isEmpty()) { cacheManager.setCacheNames(cacheNames); diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java index 2bcafc65f3..7d85a68dd0 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java @@ -200,6 +200,8 @@ public class CacheAutoConfigurationTests { load(RedisCacheConfiguration.class, "spring.cache.type=redis"); RedisCacheManager cacheManager = validateCacheManager(RedisCacheManager.class); assertThat(cacheManager.getCacheNames()).isEmpty(); + assertThat((Boolean) new DirectFieldAccessor(cacheManager) + .getPropertyValue("usePrefix")).isTrue(); } @Test diff --git a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc index 4c71c0a0b4..7ed5749c00 100644 --- a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc +++ b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc @@ -3374,6 +3374,12 @@ If Redis is available and configured, the `RedisCacheManager` is auto-configured also possible to create additional caches on startup using the `spring.cache.cache-names` property. +[NOTE] +==== +By default, a key prefix is added to prevent that if two separate caches use the same +key, Redis would have overlapping keys and be likely to return invalid values. We strongly +recommend to keep this setting enabled if you create your own `RedisCacheManager`. +==== [[boot-features-caching-provider-guava]]