Restore customization of the Couchbase cache manager
With the upgrade to the new Couchbase SDK and the related changes in Spring Data Couchbase, CacheManagerCustomizer can no longer be used to customize the Couchbase cache manager as it is an immutable class. This commit introduces a dedicated callback for the CouchbaseCacheManagerBuilder that is used by the auto-configuration and update the documentation to refer to it with a sample usage. Closes gh-22573
This commit is contained in:
@@ -5083,49 +5083,24 @@ See https://github.com/infinispan/infinispan-spring-boot[Infinispan's documentat
|
||||
|
||||
[[boot-features-caching-provider-couchbase]]
|
||||
==== Couchbase
|
||||
If the https://www.couchbase.com/[Couchbase] Java client and the `couchbase-spring-cache` implementation are available and Couchbase is <<boot-features-couchbase,configured>>, a `CouchbaseCacheManager` is auto-configured.
|
||||
It is also possible to create additional caches on startup by setting the configprop:spring.cache.cache-names[] property.
|
||||
These caches operate on the `Bucket` that was auto-configured.
|
||||
You can _also_ create additional caches on another `Bucket` by using the customizer.
|
||||
Assume you need two caches (`cache1` and `cache2`) on the "main" `Bucket` and one (`cache3`) cache with a custom time to live of 2 seconds on the "`another`" `Bucket`.
|
||||
You can create the first two caches through configuration, as follows:
|
||||
If Spring Data Couchbase is available and Couchbase is <<boot-features-couchbase,configured>>, a `CouchbaseCacheManager` is auto-configured.
|
||||
It is possible to create additional caches on startup by setting the configprop:spring.cache.cache-names[] property and cache defaults can be configured by using `spring.cache.couchbase.*` properties.
|
||||
For instance, the following configuration creates `cache1` and `cache2` caches with an entry _expiration_ of 10 minutes:
|
||||
|
||||
[source,properties,indent=0,configprops]
|
||||
----
|
||||
spring.cache.cache-names=cache1,cache2
|
||||
spring.cache.couchbase.expiration=10m
|
||||
----
|
||||
|
||||
Then you can define a `@Configuration` class to configure the extra `Bucket` and the `cache3` cache, as follows:
|
||||
If you need more control over the configuration, consider registering a `CouchbaseCacheManagerBuilderCustomizer` bean.
|
||||
The following example shows a customizer that configures a specific entry expiration for `cache1` and `cache2`:
|
||||
|
||||
[source,java,indent=0]
|
||||
----
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
public class CouchbaseCacheConfiguration {
|
||||
|
||||
private final Cluster cluster;
|
||||
|
||||
public CouchbaseCacheConfiguration(Cluster cluster) {
|
||||
this.cluster = cluster;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Bucket anotherBucket() {
|
||||
return this.cluster.openBucket("another", "secret");
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CacheManagerCustomizer<CouchbaseCacheManager> cacheManagerCustomizer() {
|
||||
return c -> {
|
||||
c.prepareCache("cache3", CacheBuilder.newInstance(anotherBucket())
|
||||
.withExpiration(2));
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
include::{code-examples}/cache/CouchbaseCacheManagerCustomizationExample.java[tag=configuration]
|
||||
----
|
||||
|
||||
This sample configuration reuses the `Cluster` that was created through auto-configuration.
|
||||
|
||||
|
||||
|
||||
[[boot-features-caching-provider-redis]]
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.docs.cache;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
import org.springframework.boot.autoconfigure.cache.CouchbaseCacheManagerBuilderCustomizer;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.couchbase.cache.CouchbaseCacheConfiguration;
|
||||
|
||||
/**
|
||||
* An example how to customize {@code CouchbaseCacheManagerBuilder} via
|
||||
* {@code CouchbaseCacheManagerBuilderCustomizer}.
|
||||
*
|
||||
* @author Dmytro Nosan
|
||||
*/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
public class CouchbaseCacheManagerCustomizationExample {
|
||||
|
||||
// tag::configuration[]
|
||||
@Bean
|
||||
public CouchbaseCacheManagerBuilderCustomizer myCouchbaseCacheManagerBuilderCustomizer() {
|
||||
return (builder) -> builder
|
||||
.withCacheConfiguration("cache1",
|
||||
CouchbaseCacheConfiguration.defaultCacheConfig().entryExpiry(Duration.ofSeconds(10)))
|
||||
.withCacheConfiguration("cache2",
|
||||
CouchbaseCacheConfiguration.defaultCacheConfig().entryExpiry(Duration.ofMinutes(1)));
|
||||
|
||||
}
|
||||
// end::configuration[]
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user