Polish "Improve cache auto-configuration for Redis"

Closes gh-10944
This commit is contained in:
Stephane Nicoll 2017-11-10 14:42:50 +01:00
parent 9ffda68af8
commit 46ef178f04
5 changed files with 39 additions and 41 deletions

View File

@ -16,7 +16,6 @@
package org.springframework.boot.autoconfigure.cache; package org.springframework.boot.autoconfigure.cache;
import java.time.Duration;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -242,25 +241,22 @@ public class CacheProperties {
} }
/** /**
* Redis-specific cache properties. Properties set will be used as the defaults for * Redis-specific cache properties.
* all Redis caches.
*/ */
public static class Redis { public static class Redis {
/** /**
* Specifies the TTL (ultimately converted to seconds) for keys written to Redis. * Entry expiration in milliseconds. By default the entries never expire.
* By default, entries do not expire, and a value of {@link Duration#ZERO} disables the TTL.
*/ */
private Duration ttl = Duration.ZERO; private long timeToLive = 0;
/** /**
* Whether to allow caching of {@literal null} values. * Allow caching null values.
*/ */
private boolean cacheNullValues = true; private boolean cacheNullValues = true;
/** /**
* Specifies an override for the default Redis key prefix. A value of {@literal null} results * Key prefix.
* in usage of the default key prefix.
*/ */
private String keyPrefix; private String keyPrefix;
@ -269,41 +265,38 @@ public class CacheProperties {
*/ */
private boolean useKeyPrefix = true; private boolean useKeyPrefix = true;
public Duration getTtl() { public long getTimeToLive() {
return this.ttl; return this.timeToLive;
} }
public Redis setTtl(Duration ttl) { public void setTimeToLive(long timeToLive) {
this.ttl = ttl; this.timeToLive = timeToLive;
return this;
} }
public boolean isCacheNullValues() { public boolean isCacheNullValues() {
return this.cacheNullValues; return this.cacheNullValues;
} }
public Redis setCacheNullValues(boolean cacheNullValues) { public void setCacheNullValues(boolean cacheNullValues) {
this.cacheNullValues = cacheNullValues; this.cacheNullValues = cacheNullValues;
return this;
} }
public String getKeyPrefix() { public String getKeyPrefix() {
return this.keyPrefix; return this.keyPrefix;
} }
public Redis setKeyPrefix(String keyPrefix) { public void setKeyPrefix(String keyPrefix) {
this.keyPrefix = keyPrefix; this.keyPrefix = keyPrefix;
return this;
} }
public boolean isUseKeyPrefix() { public boolean isUseKeyPrefix() {
return this.useKeyPrefix; return this.useKeyPrefix;
} }
public Redis setUseKeyPrefix(boolean useKeyPrefix) { public void setUseKeyPrefix(boolean useKeyPrefix) {
this.useKeyPrefix = useKeyPrefix; this.useKeyPrefix = useKeyPrefix;
return this;
} }
} }
} }

View File

@ -16,6 +16,7 @@
package org.springframework.boot.autoconfigure.cache; package org.springframework.boot.autoconfigure.cache;
import java.time.Duration;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
@ -69,24 +70,19 @@ class RedisCacheConfiguration {
} }
private org.springframework.data.redis.cache.RedisCacheConfiguration getConfiguration() { private org.springframework.data.redis.cache.RedisCacheConfiguration getConfiguration() {
Redis redisProperties = this.cacheProperties.getRedis(); Redis redisProperties = this.cacheProperties.getRedis();
org.springframework.data.redis.cache.RedisCacheConfiguration config = org.springframework.data.redis.cache.RedisCacheConfiguration org.springframework.data.redis.cache.RedisCacheConfiguration config = org.springframework.data.redis.cache.RedisCacheConfiguration
.defaultCacheConfig().entryTtl(redisProperties.getTtl()); .defaultCacheConfig();
config = config.entryTtl(Duration.ofMillis(redisProperties.getTimeToLive()));
if (redisProperties.getKeyPrefix() != null) { if (redisProperties.getKeyPrefix() != null) {
config = config.prefixKeysWith(redisProperties.getKeyPrefix()); config = config.prefixKeysWith(redisProperties.getKeyPrefix());
} }
if (!redisProperties.isCacheNullValues()) { if (!redisProperties.isCacheNullValues()) {
config = config.disableCachingNullValues(); config = config.disableCachingNullValues();
} }
if (!redisProperties.isUseKeyPrefix()) { if (!redisProperties.isUseKeyPrefix()) {
config = config.disableKeyPrefix(); config = config.disableKeyPrefix();
} }
return config; return config;
} }

View File

@ -282,24 +282,22 @@ public class CacheAutoConfigurationTests {
public void redisCacheExplicit() { public void redisCacheExplicit() {
this.contextRunner.withUserConfiguration(RedisCacheConfiguration.class) this.contextRunner.withUserConfiguration(RedisCacheConfiguration.class)
.withPropertyValues("spring.cache.type=redis", .withPropertyValues("spring.cache.type=redis",
"spring.cache.redis.ttl=PT15M", "spring.cache.redis.time-to-live=15000",
"spring.cache.redis.cacheNullValues=false",
"spring.cache.redis.keyPrefix=foo", "spring.cache.redis.keyPrefix=foo",
"spring.cache.redis.useKeyPrefix=false", "spring.cache.redis.useKeyPrefix=false")
"spring.cache.redis.cacheNullValues=false")
.run((context) -> { .run((context) -> {
RedisCacheManager cacheManager = getCacheManager(context, RedisCacheManager cacheManager = getCacheManager(context,
RedisCacheManager.class); RedisCacheManager.class);
assertThat(cacheManager.getCacheNames()).isEmpty(); assertThat(cacheManager.getCacheNames()).isEmpty();
org.springframework.data.redis.cache.RedisCacheConfiguration redisCacheConfiguration = (org.springframework.data.redis.cache.RedisCacheConfiguration) new DirectFieldAccessor( org.springframework.data.redis.cache.RedisCacheConfiguration redisCacheConfiguration = (org.springframework.data.redis.cache.RedisCacheConfiguration) new DirectFieldAccessor(
cacheManager).getPropertyValue("defaultCacheConfig"); cacheManager).getPropertyValue("defaultCacheConfig");
assertThat(redisCacheConfiguration.usePrefix()).isFalse();
assertThat(redisCacheConfiguration.getKeyPrefix()).contains("foo");
assertThat(redisCacheConfiguration.getTtl()) assertThat(redisCacheConfiguration.getTtl())
.isEqualTo(java.time.Duration.ofMinutes(15)); .isEqualTo(java.time.Duration.ofSeconds(15));
assertThat(redisCacheConfiguration.getAllowCacheNullValues()) assertThat(redisCacheConfiguration.getAllowCacheNullValues())
.isFalse(); .isFalse();
assertThat(redisCacheConfiguration.getKeyPrefix()).contains("foo");
assertThat(redisCacheConfiguration.usePrefix()).isFalse();
}); });
} }
@ -321,15 +319,14 @@ public class CacheAutoConfigurationTests {
RedisCacheManager cacheManager = getCacheManager(context, RedisCacheManager cacheManager = getCacheManager(context,
RedisCacheManager.class); RedisCacheManager.class);
assertThat(cacheManager.getCacheNames()).containsOnly("foo", "bar"); assertThat(cacheManager.getCacheNames()).containsOnly("foo", "bar");
org.springframework.data.redis.cache.RedisCacheConfiguration redisCacheConfiguration = (org.springframework.data.redis.cache.RedisCacheConfiguration) new DirectFieldAccessor( org.springframework.data.redis.cache.RedisCacheConfiguration redisCacheConfiguration = (org.springframework.data.redis.cache.RedisCacheConfiguration) new DirectFieldAccessor(
cacheManager).getPropertyValue("defaultCacheConfig"); cacheManager).getPropertyValue("defaultCacheConfig");
assertThat(redisCacheConfiguration.usePrefix()).isTrue();
assertThat(redisCacheConfiguration.getKeyPrefix()).isEmpty();
assertThat(redisCacheConfiguration.getTtl()) assertThat(redisCacheConfiguration.getTtl())
.isEqualTo(java.time.Duration.ofMinutes(0)); .isEqualTo(java.time.Duration.ofMinutes(0));
assertThat(redisCacheConfiguration.getAllowCacheNullValues()) assertThat(redisCacheConfiguration.getAllowCacheNullValues())
.isTrue(); .isTrue();
assertThat(redisCacheConfiguration.getKeyPrefix()).isEmpty();
assertThat(redisCacheConfiguration.usePrefix()).isTrue();
}); });
} }

View File

@ -77,6 +77,10 @@ content into your application; rather pick only the properties that you need.
spring.cache.infinispan.config= # The location of the configuration file to use to initialize Infinispan. spring.cache.infinispan.config= # The location of the configuration file to use to initialize Infinispan.
spring.cache.jcache.config= # The location of the configuration file to use to initialize the cache manager. spring.cache.jcache.config= # The location of the configuration file to use to initialize the cache manager.
spring.cache.jcache.provider= # Fully qualified name of the CachingProvider implementation to use to retrieve the JSR-107 compliant cache manager. Only needed if more than one JSR-107 implementation is available on the classpath. spring.cache.jcache.provider= # Fully qualified name of the CachingProvider implementation to use to retrieve the JSR-107 compliant cache manager. Only needed if more than one JSR-107 implementation is available on the classpath.
spring.cache.redis.cache-null-values=true # Allow caching null values.
spring.cache.redis.key-prefix= # Key prefix.
spring.cache.redis.time-to-live=0 # Entry expiration in milliseconds. By default the entries never expire.
spring.cache.redis.use-key-prefix=true # Whether to use the key prefix when writing to Redis.
spring.cache.type= # Cache type, auto-detected according to the environment by default. spring.cache.type= # Cache type, auto-detected according to the environment by default.
# SPRING CONFIG - using environment property only ({sc-spring-boot}/context/config/ConfigFileApplicationListener.{sc-ext}[ConfigFileApplicationListener]) # SPRING CONFIG - using environment property only ({sc-spring-boot}/context/config/ConfigFileApplicationListener.{sc-ext}[ConfigFileApplicationListener])

View File

@ -4546,9 +4546,17 @@ This sample configuration reuses the `Cluster` that was created via auto-configu
[[boot-features-caching-provider-redis]] [[boot-features-caching-provider-redis]]
==== Redis ==== Redis
If Redis is available and configured, the `RedisCacheManager` is auto-configured. It is If Redis is available and configured, a `RedisCacheManager` is auto-configured. It is
also possible to create additional caches on startup by setting the possible to create additional caches on startup by setting the
`spring.cache.cache-names` property. `spring.cache.cache-names` property and cache defaults can be configured using
`spring.redis.cache.*` properties. For instance, the following configuration creates
`cache1` and `cache2` caches with a _time to live_ of 10 minutes:
[source,properties,indent=0]
----
spring.cache.cache-names=cache1,cache2
spring.cache.redis.time-to-live=600000
----
[NOTE] [NOTE]
==== ====