mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
Polish "Add support for cache2k in memory caching"
See gh-28498
This commit is contained in:
parent
774f61fcb5
commit
a2959bbcf2
@ -93,7 +93,6 @@ dependencies {
|
|||||||
optional("nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect")
|
optional("nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect")
|
||||||
optional("org.aspectj:aspectjweaver")
|
optional("org.aspectj:aspectjweaver")
|
||||||
optional("org.cache2k:cache2k-spring")
|
optional("org.cache2k:cache2k-spring")
|
||||||
optional("org.cache2k:cache2k-micrometer")
|
|
||||||
optional("org.eclipse.jetty:jetty-webapp") {
|
optional("org.eclipse.jetty:jetty-webapp") {
|
||||||
exclude group: "javax.servlet", module: "javax.servlet-api"
|
exclude group: "javax.servlet", module: "javax.servlet-api"
|
||||||
}
|
}
|
||||||
|
@ -19,14 +19,19 @@ package org.springframework.boot.autoconfigure.cache;
|
|||||||
import org.cache2k.Cache2kBuilder;
|
import org.cache2k.Cache2kBuilder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Default configuration for cache2k when Spring boot auto configuration is creating the
|
* Callback interface that can be implemented by beans wishing to customize the default
|
||||||
* Cache Manager.
|
* setup for caches added to the manager via addCaches and for dynamically created caches.
|
||||||
*
|
*
|
||||||
* @author Jens Wilke
|
* @author Jens Wilke
|
||||||
|
* @author Stephane Nicoll
|
||||||
* @since 2.7.0
|
* @since 2.7.0
|
||||||
*/
|
*/
|
||||||
public interface Cache2kDefaults {
|
public interface Cache2kBuilderCustomizer {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Customize the default cache settings.
|
||||||
|
* @param builder the builder to customize
|
||||||
|
*/
|
||||||
void customize(Cache2kBuilder<?, ?> builder);
|
void customize(Cache2kBuilder<?, ?> builder);
|
||||||
|
|
||||||
}
|
}
|
@ -17,6 +17,7 @@
|
|||||||
package org.springframework.boot.autoconfigure.cache;
|
package org.springframework.boot.autoconfigure.cache;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
import org.cache2k.Cache2kBuilder;
|
import org.cache2k.Cache2kBuilder;
|
||||||
import org.cache2k.extra.spring.SpringCache2kCacheManager;
|
import org.cache2k.extra.spring.SpringCache2kCacheManager;
|
||||||
@ -34,6 +35,7 @@ import org.springframework.util.CollectionUtils;
|
|||||||
* Cache2k cache configuration.
|
* Cache2k cache configuration.
|
||||||
*
|
*
|
||||||
* @author Jens Wilke
|
* @author Jens Wilke
|
||||||
|
* @author Stephane Nicoll
|
||||||
*/
|
*/
|
||||||
@Configuration(proxyBeanMethods = false)
|
@Configuration(proxyBeanMethods = false)
|
||||||
@ConditionalOnClass({ Cache2kBuilder.class, SpringCache2kCacheManager.class })
|
@ConditionalOnClass({ Cache2kBuilder.class, SpringCache2kCacheManager.class })
|
||||||
@ -43,15 +45,9 @@ class Cache2kCacheConfiguration {
|
|||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
SpringCache2kCacheManager cacheManager(CacheProperties cacheProperties, CacheManagerCustomizers customizers,
|
SpringCache2kCacheManager cacheManager(CacheProperties cacheProperties, CacheManagerCustomizers customizers,
|
||||||
ObjectProvider<Cache2kDefaults> defaults) {
|
ObjectProvider<Cache2kBuilderCustomizer> cache2kBuilderCustomizers) {
|
||||||
SpringCache2kCacheManager cacheManager = new SpringCache2kCacheManager();
|
SpringCache2kCacheManager cacheManager = new SpringCache2kCacheManager();
|
||||||
Cache2kDefaults specifiedDefaults = defaults.getIfAvailable();
|
cacheManager.defaultSetup(configureDefaults(cache2kBuilderCustomizers));
|
||||||
if (specifiedDefaults != null) {
|
|
||||||
cacheManager.defaultSetup((builder) -> {
|
|
||||||
specifiedDefaults.customize(builder);
|
|
||||||
return builder;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
Collection<String> cacheNames = cacheProperties.getCacheNames();
|
Collection<String> cacheNames = cacheProperties.getCacheNames();
|
||||||
if (!CollectionUtils.isEmpty(cacheNames)) {
|
if (!CollectionUtils.isEmpty(cacheNames)) {
|
||||||
cacheManager.setDefaultCacheNames(cacheNames);
|
cacheManager.setDefaultCacheNames(cacheNames);
|
||||||
@ -59,4 +55,12 @@ class Cache2kCacheConfiguration {
|
|||||||
return customizers.customize(cacheManager);
|
return customizers.customize(cacheManager);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Function<Cache2kBuilder<?, ?>, Cache2kBuilder<?, ?>> configureDefaults(
|
||||||
|
ObjectProvider<Cache2kBuilderCustomizer> cache2kBuilderCustomizers) {
|
||||||
|
return (builder) -> {
|
||||||
|
cache2kBuilderCustomizers.orderedStream().forEach((customizer) -> customizer.customize(builder));
|
||||||
|
return builder;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2012-2021 the original author or authors.
|
* Copyright 2012-2022 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2012-2019 the original author or authors.
|
* Copyright 2012-2022 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2012-2020 the original author or authors.
|
* Copyright 2012-2022 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -19,6 +19,7 @@ package org.springframework.boot.autoconfigure.cache;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
import javax.cache.Caching;
|
import javax.cache.Caching;
|
||||||
import javax.cache.configuration.CompleteConfiguration;
|
import javax.cache.configuration.CompleteConfiguration;
|
||||||
@ -48,6 +49,7 @@ import org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfigurati
|
|||||||
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
|
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
|
||||||
import org.springframework.boot.testsupport.classpath.ClassPathExclusions;
|
import org.springframework.boot.testsupport.classpath.ClassPathExclusions;
|
||||||
import org.springframework.cache.Cache;
|
import org.springframework.cache.Cache;
|
||||||
|
import org.springframework.cache.Cache.ValueWrapper;
|
||||||
import org.springframework.cache.CacheManager;
|
import org.springframework.cache.CacheManager;
|
||||||
import org.springframework.cache.annotation.CachingConfigurer;
|
import org.springframework.cache.annotation.CachingConfigurer;
|
||||||
import org.springframework.cache.annotation.EnableCaching;
|
import org.springframework.cache.annotation.EnableCaching;
|
||||||
@ -74,7 +76,6 @@ import org.springframework.data.redis.connection.RedisConnectionFactory;
|
|||||||
import org.springframework.test.util.ReflectionTestUtils;
|
import org.springframework.test.util.ReflectionTestUtils;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.assertj.core.api.Assertions.assertThatCode;
|
|
||||||
import static org.mockito.BDDMockito.given;
|
import static org.mockito.BDDMockito.given;
|
||||||
import static org.mockito.BDDMockito.then;
|
import static org.mockito.BDDMockito.then;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
@ -624,137 +625,60 @@ class CacheAutoConfigurationTests extends AbstractCacheAutoConfigurationTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void cache2kCacheWithDynamicCacheCreation() {
|
void cache2kCacheWithExplicitCaches() {
|
||||||
this.contextRunner.withUserConfiguration(DefaultCacheConfiguration.class)
|
|
||||||
.withPropertyValues("spring.cache.type=cache2k").run((context) -> {
|
|
||||||
SpringCache2kCacheManager manager = getCacheManager(context, SpringCache2kCacheManager.class);
|
|
||||||
assertThat(manager.getCacheNames()).isEmpty();
|
|
||||||
assertThat(manager.getNativeCacheManager().getName()).isEqualTo("springDefault");
|
|
||||||
Cache foo = manager.getCache("dynamic");
|
|
||||||
foo.get("1");
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void cache2kCacheWithCacheNamesInProperties() {
|
|
||||||
this.contextRunner.withUserConfiguration(DefaultCacheConfiguration.class)
|
this.contextRunner.withUserConfiguration(DefaultCacheConfiguration.class)
|
||||||
.withPropertyValues("spring.cache.type=cache2k", "spring.cache.cacheNames=foo,bar").run((context) -> {
|
.withPropertyValues("spring.cache.type=cache2k", "spring.cache.cacheNames=foo,bar").run((context) -> {
|
||||||
SpringCache2kCacheManager manager = getCacheManager(context, SpringCache2kCacheManager.class);
|
SpringCache2kCacheManager manager = getCacheManager(context, SpringCache2kCacheManager.class);
|
||||||
assertThat(manager.getCacheNames()).containsExactlyInAnyOrder("foo", "bar");
|
assertThat(manager.getCacheNames()).containsExactlyInAnyOrder("foo", "bar");
|
||||||
assertThat(manager.getNativeCacheManager().getName()).isEqualTo("springDefault");
|
|
||||||
manager.getCache("foo").get("1");
|
|
||||||
manager.getCache("bar").get("2");
|
|
||||||
assertThat(manager.getCache("unknown")).isNull();
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void cache2kCacheWithDynamicCacheCreationAndDefaults() {
|
void cache2kCacheWithCustomizedDefaults() {
|
||||||
this.contextRunner.withUserConfiguration(DefaultCacheConfiguration.class)
|
this.contextRunner.withUserConfiguration(DefaultCacheConfiguration.class)
|
||||||
.withPropertyValues("spring.cache.type=cache2k")
|
.withPropertyValues("spring.cache.type=cache2k")
|
||||||
.withBean(Cache2kDefaults.class, () -> (b) -> b.valueType(String.class).loader((key) -> "default"))
|
.withBean(Cache2kBuilderCustomizer.class,
|
||||||
|
() -> (builder) -> builder.valueType(String.class).loader((key) -> "default"))
|
||||||
.run((context) -> {
|
.run((context) -> {
|
||||||
SpringCache2kCacheManager manager = getCacheManager(context, SpringCache2kCacheManager.class);
|
SpringCache2kCacheManager manager = getCacheManager(context, SpringCache2kCacheManager.class);
|
||||||
assertThat(manager.getCacheNames()).isEmpty();
|
assertThat(manager.getCacheNames()).isEmpty();
|
||||||
assertThat(manager.getNativeCacheManager().getName()).isEqualTo("springDefault");
|
Cache dynamic = manager.getCache("dynamic");
|
||||||
Cache foo = manager.getCache("fooDynamic");
|
assertThat(dynamic.get("1")).satisfies(hasEntry("default"));
|
||||||
assertThat(foo.get("1").get()).isEqualTo("default");
|
assertThat(dynamic.get("2")).satisfies(hasEntry("default"));
|
||||||
Cache bar = manager.getCache("barDynamic");
|
|
||||||
assertThat(bar.get("1").get()).isEqualTo("default");
|
|
||||||
assertThat(manager.getCache("barDynamic")).isSameAs(bar);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void cache2kCacheWithNamesInPropertiesAndDefaults() {
|
void cache2kCacheWithCustomizedDefaultsAndExplicitCaches() {
|
||||||
this.contextRunner.withUserConfiguration(DefaultCacheConfiguration.class)
|
this.contextRunner.withUserConfiguration(DefaultCacheConfiguration.class)
|
||||||
.withPropertyValues("spring.cache.type=cache2k", "spring.cache.cacheNames=foo,bar")
|
.withPropertyValues("spring.cache.type=cache2k", "spring.cache.cacheNames=foo,bar")
|
||||||
.withBean(Cache2kDefaults.class, () -> (b) -> b.valueType(String.class).loader((key) -> "default"))
|
.withBean(Cache2kBuilderCustomizer.class,
|
||||||
|
() -> (builder) -> builder.valueType(String.class).loader((key) -> "default"))
|
||||||
.run((context) -> {
|
.run((context) -> {
|
||||||
SpringCache2kCacheManager manager = getCacheManager(context, SpringCache2kCacheManager.class);
|
SpringCache2kCacheManager manager = getCacheManager(context, SpringCache2kCacheManager.class);
|
||||||
assertThat(manager.getCacheNames()).containsExactlyInAnyOrder("foo", "bar");
|
assertThat(manager.getCacheNames()).containsExactlyInAnyOrder("foo", "bar");
|
||||||
assertThat(manager.getNativeCacheManager().getName()).isEqualTo("springDefault");
|
assertThat(manager.getCache("foo").get("1")).satisfies(hasEntry("default"));
|
||||||
Cache foo = manager.getCache("foo");
|
assertThat(manager.getCache("bar").get("1")).satisfies(hasEntry("default"));
|
||||||
assertThat(foo.get("1").get()).isEqualTo("default");
|
|
||||||
Cache bar = manager.getCache("bar");
|
|
||||||
assertThat(bar.get("1").get()).isEqualTo("default");
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void cache2kCacheWithDynamicCacheCreationAndDefaultAndCustomCachesViaCacheManagerCustomizer() {
|
void cache2kCacheWithCacheManagerCustomizer() {
|
||||||
this.contextRunner.withUserConfiguration(DefaultCacheConfiguration.class)
|
this.contextRunner.withUserConfiguration(DefaultCacheConfiguration.class)
|
||||||
.withPropertyValues("spring.cache.type=cache2k").withBean(CacheManagerCustomizer.class,
|
.withPropertyValues("spring.cache.type=cache2k")
|
||||||
() -> (CacheManagerCustomizer<SpringCache2kCacheManager>) (cm) -> {
|
.withBean(CacheManagerCustomizer.class,
|
||||||
cm.defaultSetup((b) -> b.valueType(String.class).loader((key) -> "default"));
|
() -> cache2kCacheManagerCustomizer((cacheManager) -> cacheManager.addCache("custom",
|
||||||
cm.addCache("custom", (b) -> b.valueType(String.class).loader((key) -> "custom"));
|
(builder) -> builder.valueType(String.class).loader((key) -> "custom"))))
|
||||||
})
|
|
||||||
.run((context) -> {
|
.run((context) -> {
|
||||||
SpringCache2kCacheManager manager = getCacheManager(context, SpringCache2kCacheManager.class);
|
SpringCache2kCacheManager manager = getCacheManager(context, SpringCache2kCacheManager.class);
|
||||||
assertThat(manager.getCacheNames()).containsExactlyInAnyOrder("custom");
|
assertThat(manager.getCacheNames()).containsExactlyInAnyOrder("custom");
|
||||||
assertThat(manager.getNativeCacheManager().getName()).isEqualTo("springDefault");
|
assertThat(manager.getCache("custom").get("1")).satisfies(hasEntry("custom"));
|
||||||
Cache foo = manager.getCache("fooDynamic");
|
|
||||||
assertThat(foo.get("1").get()).isEqualTo("default");
|
|
||||||
Cache custom = manager.getCache("custom");
|
|
||||||
assertThat(custom.get("1").get()).isEqualTo("custom");
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
private CacheManagerCustomizer<SpringCache2kCacheManager> cache2kCacheManagerCustomizer(
|
||||||
void cache2kCacheWithNamesInPropertiesAndDefaultsAndCacheManagerCustomizer() {
|
Consumer<SpringCache2kCacheManager> cacheManager) {
|
||||||
this.contextRunner.withUserConfiguration(DefaultCacheConfiguration.class)
|
return cacheManager::accept;
|
||||||
.withPropertyValues("spring.cache.type=cache2k",
|
|
||||||
"spring.cache.cacheNames=foo,bar")
|
|
||||||
.withBean(Cache2kDefaults.class, () -> (b) -> b.valueType(String.class).loader((key) -> "default"))
|
|
||||||
.withBean(CacheManagerCustomizer.class,
|
|
||||||
() -> (CacheManagerCustomizer<SpringCache2kCacheManager>) (cm) -> cm.addCache("custom",
|
|
||||||
(b) -> b.valueType(String.class).loader((key) -> "custom")))
|
|
||||||
.run((context) -> {
|
|
||||||
SpringCache2kCacheManager manager = getCacheManager(context, SpringCache2kCacheManager.class);
|
|
||||||
assertThat(manager.getCacheNames()).containsExactlyInAnyOrder("foo", "bar", "custom");
|
|
||||||
assertThat(manager.getNativeCacheManager().getName()).isEqualTo("springDefault");
|
|
||||||
Cache foo = manager.getCache("foo");
|
|
||||||
assertThat(foo.get("1").get()).isEqualTo("default");
|
|
||||||
Cache bar = manager.getCache("bar");
|
|
||||||
assertThat(bar.get("1").get()).isEqualTo("default");
|
|
||||||
assertThat(manager.isAllowUnknownCache()).isFalse();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Default cannot be changed in CacheManagerCustomizer, if cache names are present in
|
|
||||||
* the properties
|
|
||||||
*/
|
|
||||||
@Test
|
|
||||||
void cache2kCacheNamesAndCacheManagerDefaultsYieldsException() {
|
|
||||||
this.contextRunner.withUserConfiguration(DefaultCacheConfiguration.class)
|
|
||||||
.withPropertyValues("spring.cache.type=cache2k", "spring.cache.cacheNames=foo,bar")
|
|
||||||
.withBean(CacheManagerCustomizer.class,
|
|
||||||
() -> (CacheManagerCustomizer<SpringCache2kCacheManager>) (cm) -> cm
|
|
||||||
.defaultSetup((b) -> b.entryCapacity(1234)))
|
|
||||||
.run((context) -> {
|
|
||||||
assertThatCode(() -> getCacheManager(context, SpringCache2kCacheManager.class)).getRootCause()
|
|
||||||
.isInstanceOf(IllegalStateException.class);
|
|
||||||
// close underlying cache manager directly since Spring bean was not
|
|
||||||
// created
|
|
||||||
org.cache2k.CacheManager.getInstance(SpringCache2kCacheManager.DEFAULT_SPRING_CACHE_MANAGER_NAME)
|
|
||||||
.close();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Applying defaults via customizer and cache manager is not possible
|
|
||||||
*/
|
|
||||||
@Test
|
|
||||||
void cache2kCacheDefaultsTwiceYieldsException() {
|
|
||||||
this.contextRunner.withUserConfiguration(DefaultCacheConfiguration.class)
|
|
||||||
.withPropertyValues("spring.cache.type=cache2k")
|
|
||||||
.withBean(Cache2kDefaults.class, () -> (b) -> b.entryCapacity(1234))
|
|
||||||
.withBean(CacheManagerCustomizer.class,
|
|
||||||
() -> (CacheManagerCustomizer<SpringCache2kCacheManager>) (cm) -> cm
|
|
||||||
.defaultSetup((b) -> b.entryCapacity(1234)))
|
|
||||||
.run((context) -> assertThatCode(() -> getCacheManager(context, SpringCache2kCacheManager.class))
|
|
||||||
.getRootCause().isInstanceOf(IllegalStateException.class));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -818,6 +742,10 @@ class CacheAutoConfigurationTests extends AbstractCacheAutoConfigurationTests {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Consumer<ValueWrapper> hasEntry(Object value) {
|
||||||
|
return (valueWrapper) -> assertThat(valueWrapper.get()).isEqualTo(value);
|
||||||
|
}
|
||||||
|
|
||||||
private void validateCaffeineCacheWithStats(AssertableApplicationContext context) {
|
private void validateCaffeineCacheWithStats(AssertableApplicationContext context) {
|
||||||
CaffeineCacheManager manager = getCacheManager(context, CaffeineCacheManager.class);
|
CaffeineCacheManager manager = getCacheManager(context, CaffeineCacheManager.class);
|
||||||
assertThat(manager.getCacheNames()).containsOnly("foo", "bar");
|
assertThat(manager.getCacheNames()).containsOnly("foo", "bar");
|
||||||
|
@ -100,6 +100,7 @@ dependencies {
|
|||||||
}
|
}
|
||||||
implementation("org.apache.tomcat.embed:tomcat-embed-core")
|
implementation("org.apache.tomcat.embed:tomcat-embed-core")
|
||||||
implementation("org.assertj:assertj-core")
|
implementation("org.assertj:assertj-core")
|
||||||
|
implementation("org.cache2k:cache2k-spring")
|
||||||
implementation("org.glassfish.jersey.core:jersey-server")
|
implementation("org.glassfish.jersey.core:jersey-server")
|
||||||
implementation("org.glassfish.jersey.containers:jersey-container-servlet-core")
|
implementation("org.glassfish.jersey.containers:jersey-container-servlet-core")
|
||||||
implementation("org.hibernate:hibernate-jcache") {
|
implementation("org.hibernate:hibernate-jcache") {
|
||||||
|
@ -850,6 +850,7 @@ Additional, cache-specific metrics are also available.
|
|||||||
|
|
||||||
The following cache libraries are supported:
|
The following cache libraries are supported:
|
||||||
|
|
||||||
|
* Cache2k
|
||||||
* Caffeine
|
* Caffeine
|
||||||
* EhCache 2
|
* EhCache 2
|
||||||
* Hazelcast
|
* Hazelcast
|
||||||
|
@ -44,6 +44,7 @@ If you have not defined a bean of type `CacheManager` or a `CacheResolver` named
|
|||||||
. <<io#io.caching.provider.couchbase,Couchbase>>
|
. <<io#io.caching.provider.couchbase,Couchbase>>
|
||||||
. <<io#io.caching.provider.redis,Redis>>
|
. <<io#io.caching.provider.redis,Redis>>
|
||||||
. <<io#io.caching.provider.caffeine,Caffeine>>
|
. <<io#io.caching.provider.caffeine,Caffeine>>
|
||||||
|
. <<io#io.caching.provider.cache2k,Cache2k>>
|
||||||
. <<io#io.caching.provider.simple,Simple>>
|
. <<io#io.caching.provider.simple,Simple>>
|
||||||
|
|
||||||
Additionally, {spring-boot-for-apache-geode}[Spring Boot for Apache Geode] provides {spring-boot-for-apache-geode-docs}#geode-caching-provider[auto-configuration for using Apache Geode as a cache provider].
|
Additionally, {spring-boot-for-apache-geode}[Spring Boot for Apache Geode] provides {spring-boot-for-apache-geode-docs}#geode-caching-provider[auto-configuration for using Apache Geode as a cache provider].
|
||||||
@ -230,6 +231,19 @@ The auto-configuration ignores any other generic type.
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
[[io.caching.provider.cache2k]]
|
||||||
|
==== Cache2k
|
||||||
|
https://cache2k.org/[Cache2k] is an in-memory cache.
|
||||||
|
If the Cache2k spring integration is present, a `SpringCache2kCacheManager` is auto-configured.
|
||||||
|
|
||||||
|
Caches can be created on startup by setting the configprop:spring.cache.cache-names[] property.
|
||||||
|
Cache defaults can be customized using a `Cache2kBuilderCustomizer` bean.
|
||||||
|
The following example shows a customizer that configures the capacity of the cache to 200 entries, with an expiration of 5 minutes:
|
||||||
|
|
||||||
|
include::code:MyCache2kDefaultsConfiguration[]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
[[io.caching.provider.simple]]
|
[[io.caching.provider.simple]]
|
||||||
==== Simple
|
==== Simple
|
||||||
If none of the other providers can be found, a simple implementation using a `ConcurrentHashMap` as the cache store is configured.
|
If none of the other providers can be found, a simple implementation using a `ConcurrentHashMap` as the cache store is configured.
|
||||||
|
@ -0,0 +1,36 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2012-2022 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.io.caching.provider.cache2k;
|
||||||
|
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import org.springframework.boot.autoconfigure.cache.Cache2kBuilderCustomizer;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@Configuration(proxyBeanMethods = false)
|
||||||
|
public class MyCache2kDefaultsConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public Cache2kBuilderCustomizer myCache2kDefaultsCustomizer() {
|
||||||
|
// @formatter:off
|
||||||
|
return (builder) -> builder.entryCapacity(200)
|
||||||
|
.expireAfterWrite(5, TimeUnit.MINUTES);
|
||||||
|
// @formatter:on
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,18 @@
|
|||||||
|
package org.springframework.boot.docs.io.caching.provider.cache2k
|
||||||
|
|
||||||
|
import org.springframework.boot.autoconfigure.cache.Cache2kBuilderCustomizer
|
||||||
|
import org.springframework.context.annotation.Bean
|
||||||
|
import org.springframework.context.annotation.Configuration
|
||||||
|
import java.util.concurrent.TimeUnit
|
||||||
|
|
||||||
|
@Configuration(proxyBeanMethods = false)
|
||||||
|
class MyCache2kDefaultsConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
fun myCache2kDefaultsCustomizer(): Cache2kBuilderCustomizer {
|
||||||
|
return Cache2kBuilderCustomizer { builder ->
|
||||||
|
builder.entryCapacity(200)
|
||||||
|
.expireAfterWrite(5, TimeUnit.MINUTES)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user