From 416b689359b6a2de370ca4a4f86ef6cb929c6f1e Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Thu, 25 Feb 2016 13:51:59 +0100 Subject: [PATCH] Enable prefix by default on RedisCacheManager An overhaul of the `RedisCacheManager` is expected in Hopper (to be consumed by Spring Boot 1.4). One of those changes is to make sure every key have a decent prefix by default. This commit enables the use of prefix as it is disabled by default. Closes gh-5175 --- .../boot/autoconfigure/cache/RedisCacheConfiguration.java | 1 + .../autoconfigure/cache/CacheAutoConfigurationTests.java | 2 ++ .../src/main/asciidoc/spring-boot-features.adoc | 6 ++++++ 3 files changed, 9 insertions(+) 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]]