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
This commit is contained in:
Stephane Nicoll 2016-02-25 13:51:59 +01:00
parent af32d6e8ee
commit 416b689359
3 changed files with 9 additions and 0 deletions

View File

@ -52,6 +52,7 @@ class RedisCacheConfiguration {
@Bean @Bean
public RedisCacheManager cacheManager(RedisTemplate<Object, Object> redisTemplate) { public RedisCacheManager cacheManager(RedisTemplate<Object, Object> redisTemplate) {
RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate); RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
cacheManager.setUsePrefix(true);
List<String> cacheNames = this.cacheProperties.getCacheNames(); List<String> cacheNames = this.cacheProperties.getCacheNames();
if (!cacheNames.isEmpty()) { if (!cacheNames.isEmpty()) {
cacheManager.setCacheNames(cacheNames); cacheManager.setCacheNames(cacheNames);

View File

@ -200,6 +200,8 @@ public class CacheAutoConfigurationTests {
load(RedisCacheConfiguration.class, "spring.cache.type=redis"); load(RedisCacheConfiguration.class, "spring.cache.type=redis");
RedisCacheManager cacheManager = validateCacheManager(RedisCacheManager.class); RedisCacheManager cacheManager = validateCacheManager(RedisCacheManager.class);
assertThat(cacheManager.getCacheNames()).isEmpty(); assertThat(cacheManager.getCacheNames()).isEmpty();
assertThat((Boolean) new DirectFieldAccessor(cacheManager)
.getPropertyValue("usePrefix")).isTrue();
} }
@Test @Test

View File

@ -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` also possible to create additional caches on startup using the `spring.cache.cache-names`
property. 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]] [[boot-features-caching-provider-guava]]