Skip to content

Commit 61d8bac

Browse files
nosansnicoll
authored andcommitted
Document RedisCacheManagerBuilderCustomizer
See gh-19819
1 parent 7693d33 commit 61d8bac

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4771,6 +4771,14 @@ For instance, the following configuration creates `cache1` and `cache2` caches w
47714771
spring.cache.redis.time-to-live=600000
47724772
----
47734773

4774+
If you require more control over `RedisCacheManager` e.g. set _time to live_ for the particular caches, you can customize `org.springframework.data.redis.cache.RedisCacheManager$RedisCacheManagerBuilder`
4775+
programmatically by declaring `RedisCacheManagerBuilderCustomizer` bean(s) as shown in the following example:
4776+
4777+
[source,java,indent=0]
4778+
----
4779+
include::{code-examples}/cache/redis/RedisCacheManagerBuilderCustomizerConfiguration.java[tag=configuration]
4780+
----
4781+
47744782
NOTE: By default, a key prefix is added so that, if two separate caches use the same key, Redis does not have overlapping keys and cannot return invalid values.
47754783
We strongly recommend keeping this setting enabled if you create your own `RedisCacheManager`.
47764784

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2012-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import java.time.Duration;
18+
19+
import org.springframework.boot.autoconfigure.cache.RedisCacheManagerBuilderCustomizer;
20+
import org.springframework.context.annotation.Bean;
21+
import org.springframework.context.annotation.Configuration;
22+
import org.springframework.data.redis.cache.RedisCacheConfiguration;
23+
24+
/**
25+
* An example how to customize {@code RedisCacheManagerBuilder} via
26+
* {@code RedisCacheManagerBuilderCustomizer}.
27+
*
28+
* @author Dmytro Nosan
29+
*/
30+
// tag::configuration[]
31+
@Configuration(proxyBeanMethods = false)
32+
public class RedisCacheManagerBuilderCustomizerConfiguration {
33+
34+
@Bean
35+
public RedisCacheManagerBuilderCustomizer ttlRedisCacheManagerBuilderCustomizer() {
36+
return (builder) -> builder
37+
.withCacheConfiguration("cache1",
38+
RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofSeconds(10)))
39+
.withCacheConfiguration("cache2",
40+
RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofMinutes(1)));
41+
42+
}
43+
44+
}
45+
// end::configuration[]

0 commit comments

Comments
 (0)