Skip to content

Commit 46ef178

Browse files
committed
Polish "Improve cache auto-configuration for Redis"
Closes gh-10944
1 parent 9ffda68 commit 46ef178

File tree

5 files changed

+39
-41
lines changed

5 files changed

+39
-41
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/CacheProperties.java

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.boot.autoconfigure.cache;
1818

19-
import java.time.Duration;
2019
import java.util.ArrayList;
2120
import java.util.List;
2221
import java.util.concurrent.TimeUnit;
@@ -242,25 +241,22 @@ public void setConfig(Resource config) {
242241
}
243242

244243
/**
245-
* Redis-specific cache properties. Properties set will be used as the defaults for
246-
* all Redis caches.
244+
* Redis-specific cache properties.
247245
*/
248246
public static class Redis {
249247

250248
/**
251-
* Specifies the TTL (ultimately converted to seconds) for keys written to Redis.
252-
* By default, entries do not expire, and a value of {@link Duration#ZERO} disables the TTL.
249+
* Entry expiration in milliseconds. By default the entries never expire.
253250
*/
254-
private Duration ttl = Duration.ZERO;
251+
private long timeToLive = 0;
255252

256253
/**
257-
* Whether to allow caching of {@literal null} values.
254+
* Allow caching null values.
258255
*/
259256
private boolean cacheNullValues = true;
260257

261258
/**
262-
* Specifies an override for the default Redis key prefix. A value of {@literal null} results
263-
* in usage of the default key prefix.
259+
* Key prefix.
264260
*/
265261
private String keyPrefix;
266262

@@ -269,41 +265,38 @@ public static class Redis {
269265
*/
270266
private boolean useKeyPrefix = true;
271267

272-
public Duration getTtl() {
273-
return this.ttl;
268+
public long getTimeToLive() {
269+
return this.timeToLive;
274270
}
275271

276-
public Redis setTtl(Duration ttl) {
277-
this.ttl = ttl;
278-
return this;
272+
public void setTimeToLive(long timeToLive) {
273+
this.timeToLive = timeToLive;
279274
}
280275

281276
public boolean isCacheNullValues() {
282277
return this.cacheNullValues;
283278
}
284279

285-
public Redis setCacheNullValues(boolean cacheNullValues) {
280+
public void setCacheNullValues(boolean cacheNullValues) {
286281
this.cacheNullValues = cacheNullValues;
287-
return this;
288282
}
289283

290284
public String getKeyPrefix() {
291285
return this.keyPrefix;
292286
}
293287

294-
public Redis setKeyPrefix(String keyPrefix) {
288+
public void setKeyPrefix(String keyPrefix) {
295289
this.keyPrefix = keyPrefix;
296-
return this;
297290
}
298291

299292
public boolean isUseKeyPrefix() {
300293
return this.useKeyPrefix;
301294
}
302295

303-
public Redis setUseKeyPrefix(boolean useKeyPrefix) {
296+
public void setUseKeyPrefix(boolean useKeyPrefix) {
304297
this.useKeyPrefix = useKeyPrefix;
305-
return this;
306298
}
299+
307300
}
308301

309302
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/RedisCacheConfiguration.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.autoconfigure.cache;
1818

19+
import java.time.Duration;
1920
import java.util.LinkedHashSet;
2021
import java.util.List;
2122

@@ -69,24 +70,19 @@ public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFact
6970
}
7071

7172
private org.springframework.data.redis.cache.RedisCacheConfiguration getConfiguration() {
72-
7373
Redis redisProperties = this.cacheProperties.getRedis();
74-
7574
org.springframework.data.redis.cache.RedisCacheConfiguration config = org.springframework.data.redis.cache.RedisCacheConfiguration
76-
.defaultCacheConfig().entryTtl(redisProperties.getTtl());
77-
75+
.defaultCacheConfig();
76+
config = config.entryTtl(Duration.ofMillis(redisProperties.getTimeToLive()));
7877
if (redisProperties.getKeyPrefix() != null) {
7978
config = config.prefixKeysWith(redisProperties.getKeyPrefix());
8079
}
81-
8280
if (!redisProperties.isCacheNullValues()) {
8381
config = config.disableCachingNullValues();
8482
}
85-
8683
if (!redisProperties.isUseKeyPrefix()) {
8784
config = config.disableKeyPrefix();
8885
}
89-
9086
return config;
9187
}
9288

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -282,24 +282,22 @@ public void couchbaseCacheExplicitWithTtl() {
282282
public void redisCacheExplicit() {
283283
this.contextRunner.withUserConfiguration(RedisCacheConfiguration.class)
284284
.withPropertyValues("spring.cache.type=redis",
285-
"spring.cache.redis.ttl=PT15M",
285+
"spring.cache.redis.time-to-live=15000",
286+
"spring.cache.redis.cacheNullValues=false",
286287
"spring.cache.redis.keyPrefix=foo",
287-
"spring.cache.redis.useKeyPrefix=false",
288-
"spring.cache.redis.cacheNullValues=false")
288+
"spring.cache.redis.useKeyPrefix=false")
289289
.run((context) -> {
290290
RedisCacheManager cacheManager = getCacheManager(context,
291291
RedisCacheManager.class);
292292
assertThat(cacheManager.getCacheNames()).isEmpty();
293-
294293
org.springframework.data.redis.cache.RedisCacheConfiguration redisCacheConfiguration = (org.springframework.data.redis.cache.RedisCacheConfiguration) new DirectFieldAccessor(
295294
cacheManager).getPropertyValue("defaultCacheConfig");
296-
297-
assertThat(redisCacheConfiguration.usePrefix()).isFalse();
298-
assertThat(redisCacheConfiguration.getKeyPrefix()).contains("foo");
299295
assertThat(redisCacheConfiguration.getTtl())
300-
.isEqualTo(java.time.Duration.ofMinutes(15));
296+
.isEqualTo(java.time.Duration.ofSeconds(15));
301297
assertThat(redisCacheConfiguration.getAllowCacheNullValues())
302298
.isFalse();
299+
assertThat(redisCacheConfiguration.getKeyPrefix()).contains("foo");
300+
assertThat(redisCacheConfiguration.usePrefix()).isFalse();
303301
});
304302
}
305303

@@ -321,15 +319,14 @@ public void redisCacheExplicitWithCaches() {
321319
RedisCacheManager cacheManager = getCacheManager(context,
322320
RedisCacheManager.class);
323321
assertThat(cacheManager.getCacheNames()).containsOnly("foo", "bar");
324-
325322
org.springframework.data.redis.cache.RedisCacheConfiguration redisCacheConfiguration = (org.springframework.data.redis.cache.RedisCacheConfiguration) new DirectFieldAccessor(
326323
cacheManager).getPropertyValue("defaultCacheConfig");
327-
assertThat(redisCacheConfiguration.usePrefix()).isTrue();
328-
assertThat(redisCacheConfiguration.getKeyPrefix()).isEmpty();
329324
assertThat(redisCacheConfiguration.getTtl())
330325
.isEqualTo(java.time.Duration.ofMinutes(0));
331326
assertThat(redisCacheConfiguration.getAllowCacheNullValues())
332327
.isTrue();
328+
assertThat(redisCacheConfiguration.getKeyPrefix()).isEmpty();
329+
assertThat(redisCacheConfiguration.usePrefix()).isTrue();
333330
});
334331
}
335332

spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ content into your application; rather pick only the properties that you need.
7777
spring.cache.infinispan.config= # The location of the configuration file to use to initialize Infinispan.
7878
spring.cache.jcache.config= # The location of the configuration file to use to initialize the cache manager.
7979
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.
80+
spring.cache.redis.cache-null-values=true # Allow caching null values.
81+
spring.cache.redis.key-prefix= # Key prefix.
82+
spring.cache.redis.time-to-live=0 # Entry expiration in milliseconds. By default the entries never expire.
83+
spring.cache.redis.use-key-prefix=true # Whether to use the key prefix when writing to Redis.
8084
spring.cache.type= # Cache type, auto-detected according to the environment by default.
8185
8286
# SPRING CONFIG - using environment property only ({sc-spring-boot}/context/config/ConfigFileApplicationListener.{sc-ext}[ConfigFileApplicationListener])

spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4546,9 +4546,17 @@ This sample configuration reuses the `Cluster` that was created via auto-configu
45464546

45474547
[[boot-features-caching-provider-redis]]
45484548
==== Redis
4549-
If Redis is available and configured, the `RedisCacheManager` is auto-configured. It is
4550-
also possible to create additional caches on startup by setting the
4551-
`spring.cache.cache-names` property.
4549+
If Redis is available and configured, a `RedisCacheManager` is auto-configured. It is
4550+
possible to create additional caches on startup by setting the
4551+
`spring.cache.cache-names` property and cache defaults can be configured using
4552+
`spring.redis.cache.*` properties. For instance, the following configuration creates
4553+
`cache1` and `cache2` caches with a _time to live_ of 10 minutes:
4554+
4555+
[source,properties,indent=0]
4556+
----
4557+
spring.cache.cache-names=cache1,cache2
4558+
spring.cache.redis.time-to-live=600000
4559+
----
45524560

45534561
[NOTE]
45544562
====

0 commit comments

Comments
 (0)