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:
Stephane Nicoll
2020-08-11 13:28:16 +02:00
parent a9200b5b03
commit dc4de06b35
7 changed files with 127 additions and 39 deletions

View File

@@ -28,6 +28,7 @@ import org.springframework.util.Assert;
* @author Phillip Webb
* @author Eddú Meléndez
*/
@SuppressWarnings("deprecation")
final class CacheConfigurations {
private static final Map<CacheType, Class<?>> MAPPINGS;

View File

@@ -21,6 +21,7 @@ import java.util.List;
import com.couchbase.client.java.Cluster;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.cache.CacheProperties.Couchbase;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
@@ -39,16 +40,20 @@ import org.springframework.util.ObjectUtils;
*
* @author Stephane Nicoll
* @since 1.4.0
* @deprecated since 2.3.3 as this class is not intended for public use. It will be made
* package-private in a future release
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass({ Cluster.class, CouchbaseClientFactory.class, CouchbaseCacheManager.class })
@ConditionalOnMissingBean(CacheManager.class)
@ConditionalOnSingleCandidate(CouchbaseClientFactory.class)
@Conditional(CacheCondition.class)
@Deprecated
public class CouchbaseCacheConfiguration {
@Bean
public CouchbaseCacheManager cacheManager(CacheProperties cacheProperties, CacheManagerCustomizers customizers,
ObjectProvider<CouchbaseCacheManagerBuilderCustomizer> couchbaseCacheManagerBuilderCustomizers,
CouchbaseClientFactory clientFactory) {
List<String> cacheNames = cacheProperties.getCacheNames();
CouchbaseCacheManagerBuilder builder = CouchbaseCacheManager.builder(clientFactory);
@@ -62,6 +67,7 @@ public class CouchbaseCacheConfiguration {
if (!ObjectUtils.isEmpty(cacheNames)) {
builder.initialCacheNames(new LinkedHashSet<>(cacheNames));
}
couchbaseCacheManagerBuilderCustomizers.orderedStream().forEach((customizer) -> customizer.customize(builder));
CouchbaseCacheManager cacheManager = builder.build();
return customizers.customize(cacheManager);
}

View File

@@ -0,0 +1,39 @@
/*
* Copyright 2012-2019 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.autoconfigure.cache;
import org.springframework.data.couchbase.cache.CouchbaseCacheManager;
import org.springframework.data.couchbase.cache.CouchbaseCacheManager.CouchbaseCacheManagerBuilder;
/**
* Callback interface that can be implemented by beans wishing to customize the
* {@link CouchbaseCacheManagerBuilder} before it is used to build the auto-configured
* {@link CouchbaseCacheManager}.
*
* @author Stephane Nicoll
* @since 2.3.3
*/
@FunctionalInterface
public interface CouchbaseCacheManagerBuilderCustomizer {
/**
* Customize the {@link CouchbaseCacheManagerBuilder}.
* @param builder the builder to customize
*/
void customize(CouchbaseCacheManagerBuilder builder);
}

View File

@@ -64,6 +64,7 @@ import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.data.couchbase.CouchbaseClientFactory;
import org.springframework.data.couchbase.cache.CouchbaseCache;
import org.springframework.data.couchbase.cache.CouchbaseCacheConfiguration;
import org.springframework.data.couchbase.cache.CouchbaseCacheManager;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
@@ -195,7 +196,7 @@ class CacheAutoConfigurationTests extends AbstractCacheAutoConfigurationTests {
@Test
void couchbaseCacheExplicit() {
this.contextRunner.withUserConfiguration(CouchbaseCacheConfiguration.class)
this.contextRunner.withUserConfiguration(CouchbaseConfiguration.class)
.withPropertyValues("spring.cache.type=couchbase").run((context) -> {
CouchbaseCacheManager cacheManager = getCacheManager(context, CouchbaseCacheManager.class);
assertThat(cacheManager.getCacheNames()).isEmpty();
@@ -204,14 +205,14 @@ class CacheAutoConfigurationTests extends AbstractCacheAutoConfigurationTests {
@Test
void couchbaseCacheWithCustomizers() {
this.contextRunner.withUserConfiguration(CouchbaseCacheAndCustomizersConfiguration.class)
this.contextRunner.withUserConfiguration(CouchbaseWithCustomizersConfiguration.class)
.withPropertyValues("spring.cache.type=couchbase")
.run(verifyCustomizers("allCacheManagerCustomizer", "couchbaseCacheManagerCustomizer"));
}
@Test
void couchbaseCacheExplicitWithCaches() {
this.contextRunner.withUserConfiguration(CouchbaseCacheConfiguration.class)
this.contextRunner.withUserConfiguration(CouchbaseConfiguration.class)
.withPropertyValues("spring.cache.type=couchbase", "spring.cache.cacheNames[0]=foo",
"spring.cache.cacheNames[1]=bar")
.run((context) -> {
@@ -225,7 +226,7 @@ class CacheAutoConfigurationTests extends AbstractCacheAutoConfigurationTests {
@Test
void couchbaseCacheExplicitWithTtl() {
this.contextRunner.withUserConfiguration(CouchbaseCacheConfiguration.class)
this.contextRunner.withUserConfiguration(CouchbaseConfiguration.class)
.withPropertyValues("spring.cache.type=couchbase", "spring.cache.cacheNames=foo,bar",
"spring.cache.couchbase.expiration=2000")
.run((context) -> {
@@ -237,6 +238,20 @@ class CacheAutoConfigurationTests extends AbstractCacheAutoConfigurationTests {
});
}
@Test
void couchbaseCacheWithCouchbaseCacheManagerBuilderCustomizer() {
this.contextRunner.withUserConfiguration(CouchbaseConfiguration.class)
.withPropertyValues("spring.cache.type=couchbase", "spring.cache.couchbase.expiration=15s")
.withBean(CouchbaseCacheManagerBuilderCustomizer.class, () -> (builder) -> builder.cacheDefaults(
CouchbaseCacheConfiguration.defaultCacheConfig().entryExpiry(java.time.Duration.ofSeconds(10))))
.run((context) -> {
CouchbaseCacheManager cacheManager = getCacheManager(context, CouchbaseCacheManager.class);
CouchbaseCacheConfiguration couchbaseCacheConfiguration = getDefaultCouchbaseCacheConfiguration(
cacheManager);
assertThat(couchbaseCacheConfiguration.getExpiry()).isEqualTo(java.time.Duration.ofSeconds(10));
});
}
@Test
void redisCacheExplicit() {
this.contextRunner.withUserConfiguration(RedisConfiguration.class)
@@ -666,6 +681,10 @@ class CacheAutoConfigurationTests extends AbstractCacheAutoConfigurationTests {
assertThat(((CaffeineCache) foo).getNativeCache().stats().missCount()).isEqualTo(1L);
}
private CouchbaseCacheConfiguration getDefaultCouchbaseCacheConfiguration(CouchbaseCacheManager cacheManager) {
return (CouchbaseCacheConfiguration) ReflectionTestUtils.getField(cacheManager, "defaultCacheConfig");
}
private RedisCacheConfiguration getDefaultRedisCacheConfiguration(RedisCacheManager cacheManager) {
return (RedisCacheConfiguration) ReflectionTestUtils.getField(cacheManager, "defaultCacheConfig");
}
@@ -719,7 +738,7 @@ class CacheAutoConfigurationTests extends AbstractCacheAutoConfigurationTests {
@Configuration(proxyBeanMethods = false)
@EnableCaching
static class CouchbaseCacheConfiguration {
static class CouchbaseConfiguration {
@Bean
CouchbaseClientFactory couchbaseClientFactory() {
@@ -729,8 +748,8 @@ class CacheAutoConfigurationTests extends AbstractCacheAutoConfigurationTests {
}
@Configuration(proxyBeanMethods = false)
@Import({ CouchbaseCacheConfiguration.class, CacheManagerCustomizersConfiguration.class })
static class CouchbaseCacheAndCustomizersConfiguration {
@Import({ CouchbaseConfiguration.class, CacheManagerCustomizersConfiguration.class })
static class CouchbaseWithCustomizersConfiguration {
}