From 61d8bacd236ed4624a71263729eddff9a4868844 Mon Sep 17 00:00:00 2001 From: Dmytro Nosan Date: Mon, 20 Jan 2020 16:27:19 +0200 Subject: [PATCH 1/2] Document RedisCacheManagerBuilderCustomizer See gh-19819 --- .../main/asciidoc/spring-boot-features.adoc | 8 ++++ ...ManagerBuilderCustomizerConfiguration.java | 45 +++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/cache/redis/RedisCacheManagerBuilderCustomizerConfiguration.java diff --git a/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc b/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc index 9dd0dd62ebd..898b3ede13d 100644 --- a/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc +++ b/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc @@ -4771,6 +4771,14 @@ For instance, the following configuration creates `cache1` and `cache2` caches w spring.cache.redis.time-to-live=600000 ---- +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` +programmatically by declaring `RedisCacheManagerBuilderCustomizer` bean(s) as shown in the following example: + +[source,java,indent=0] +---- +include::{code-examples}/cache/redis/RedisCacheManagerBuilderCustomizerConfiguration.java[tag=configuration] +---- + 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. We strongly recommend keeping this setting enabled if you create your own `RedisCacheManager`. diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/cache/redis/RedisCacheManagerBuilderCustomizerConfiguration.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/cache/redis/RedisCacheManagerBuilderCustomizerConfiguration.java new file mode 100644 index 00000000000..3c4d0fc13d1 --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/cache/redis/RedisCacheManagerBuilderCustomizerConfiguration.java @@ -0,0 +1,45 @@ +/* + * Copyright 2012-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.time.Duration; + +import org.springframework.boot.autoconfigure.cache.RedisCacheManagerBuilderCustomizer; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.cache.RedisCacheConfiguration; + +/** + * An example how to customize {@code RedisCacheManagerBuilder} via + * {@code RedisCacheManagerBuilderCustomizer}. + * + * @author Dmytro Nosan + */ +// tag::configuration[] +@Configuration(proxyBeanMethods = false) +public class RedisCacheManagerBuilderCustomizerConfiguration { + + @Bean + public RedisCacheManagerBuilderCustomizer ttlRedisCacheManagerBuilderCustomizer() { + return (builder) -> builder + .withCacheConfiguration("cache1", + RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofSeconds(10))) + .withCacheConfiguration("cache2", + RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofMinutes(1))); + + } + +} +// end::configuration[] From 3dba4c8f4eabba9bfc91717e9feebdd52ba5b99c Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Mon, 20 Jan 2020 16:02:38 +0100 Subject: [PATCH 2/2] Polish "Document RedisCacheManagerBuilderCustomizer" See gh-19819 --- .../main/asciidoc/spring-boot-features.adoc | 21 ++++++++++--------- ...edisCacheManagerCustomizationExample.java} | 10 +++++---- 2 files changed, 17 insertions(+), 14 deletions(-) rename spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/cache/{redis/RedisCacheManagerBuilderCustomizerConfiguration.java => RedisCacheManagerCustomizationExample.java} (85%) diff --git a/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc b/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc index 898b3ede13d..ea9341d0bb0 100644 --- a/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc +++ b/spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc @@ -4771,19 +4771,20 @@ For instance, the following configuration creates `cache1` and `cache2` caches w spring.cache.redis.time-to-live=600000 ---- -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` -programmatically by declaring `RedisCacheManagerBuilderCustomizer` bean(s) as shown in the following example: - -[source,java,indent=0] ----- -include::{code-examples}/cache/redis/RedisCacheManagerBuilderCustomizerConfiguration.java[tag=configuration] ----- - 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. We strongly recommend keeping this setting enabled if you create your own `RedisCacheManager`. -TIP: You can take full control of the configuration by adding a `RedisCacheConfiguration` `@Bean` of your own. -This can be useful if you're looking for customizing the serialization strategy. +TIP: You can take full control of the default configuration by adding a `RedisCacheConfiguration` `@Bean` of your own. +This can be useful if you're looking for customizing the default serialization strategy. + +If you need more control over the configuration, consider registering a `RedisCacheManagerBuilderCustomizer` bean. +The following example shows a customizer that configures a specific time to live for `cache1` and `cache2`: + +[source,java,indent=0] +---- +include::{code-examples}/cache/RedisCacheManagerCustomizationExample.java[tag=configuration] +---- + diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/cache/redis/RedisCacheManagerBuilderCustomizerConfiguration.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/cache/RedisCacheManagerCustomizationExample.java similarity index 85% rename from spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/cache/redis/RedisCacheManagerBuilderCustomizerConfiguration.java rename to spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/cache/RedisCacheManagerCustomizationExample.java index 3c4d0fc13d1..88f56fa93d8 100644 --- a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/cache/redis/RedisCacheManagerBuilderCustomizerConfiguration.java +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/cache/RedisCacheManagerCustomizationExample.java @@ -14,6 +14,8 @@ * limitations under the License. */ +package org.springframework.boot.docs.cache; + import java.time.Duration; import org.springframework.boot.autoconfigure.cache.RedisCacheManagerBuilderCustomizer; @@ -27,12 +29,12 @@ import org.springframework.data.redis.cache.RedisCacheConfiguration; * * @author Dmytro Nosan */ -// tag::configuration[] @Configuration(proxyBeanMethods = false) -public class RedisCacheManagerBuilderCustomizerConfiguration { +public class RedisCacheManagerCustomizationExample { + // tag::configuration[] @Bean - public RedisCacheManagerBuilderCustomizer ttlRedisCacheManagerBuilderCustomizer() { + public RedisCacheManagerBuilderCustomizer myRedisCacheManagerBuilderCustomizer() { return (builder) -> builder .withCacheConfiguration("cache1", RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofSeconds(10))) @@ -40,6 +42,6 @@ public class RedisCacheManagerBuilderCustomizerConfiguration { RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofMinutes(1))); } + // end::configuration[] } -// end::configuration[]