Skip to content

Commit 7227b9f

Browse files
committed
add cache2k support
1 parent 3a65d7b commit 7227b9f

File tree

14 files changed

+382
-2
lines changed

14 files changed

+382
-2
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ dependencies {
9292
optional("org.apache.tomcat.embed:tomcat-embed-el")
9393
optional("org.apache.tomcat:tomcat-jdbc")
9494
optional("org.aspectj:aspectjweaver")
95+
optional("org.cache2k:cache2k-micrometer")
96+
optional("org.cache2k:cache2k-spring")
9597
optional("org.eclipse.jetty:jetty-server") {
9698
exclude group: "javax.servlet", module: "javax.servlet-api"
9799
}
@@ -155,6 +157,7 @@ dependencies {
155157
testImplementation("org.aspectj:aspectjrt")
156158
testImplementation("org.assertj:assertj-core")
157159
testImplementation("org.awaitility:awaitility")
160+
testImplementation("org.cache2k:cache2k-api")
158161
testImplementation("org.eclipse.jetty:jetty-webapp") {
159162
exclude group: "javax.servlet", module: "javax.servlet-api"
160163
}
@@ -176,6 +179,7 @@ dependencies {
176179

177180
testRuntimeOnly("jakarta.management.j2ee:jakarta.management.j2ee-api")
178181
testRuntimeOnly("jakarta.transaction:jakarta.transaction-api")
182+
testRuntimeOnly("org.cache2k:cache2k-core")
179183
testRuntimeOnly("org.springframework.security:spring-security-oauth2-jose")
180184
testRuntimeOnly("org.springframework.security:spring-security-oauth2-resource-server")
181185
testRuntimeOnly("org.springframework.security:spring-security-saml2-service-provider")

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/cache/CacheMeterBinderProvidersConfiguration.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,7 +19,11 @@
1919
import com.hazelcast.core.Hazelcast;
2020
import com.hazelcast.spring.cache.HazelcastCache;
2121
import io.micrometer.core.instrument.binder.MeterBinder;
22+
import org.cache2k.Cache2kBuilder;
23+
import org.cache2k.extra.micrometer.Cache2kCacheMetrics;
24+
import org.cache2k.extra.spring.SpringCache2kCache;
2225

26+
import org.springframework.boot.actuate.metrics.cache.Cache2kCacheMeterBinderProvider;
2327
import org.springframework.boot.actuate.metrics.cache.CacheMeterBinderProvider;
2428
import org.springframework.boot.actuate.metrics.cache.CaffeineCacheMeterBinderProvider;
2529
import org.springframework.boot.actuate.metrics.cache.HazelcastCacheMeterBinderProvider;
@@ -41,6 +45,17 @@
4145
@ConditionalOnClass(MeterBinder.class)
4246
class CacheMeterBinderProvidersConfiguration {
4347

48+
@Configuration(proxyBeanMethods = false)
49+
@ConditionalOnClass({ Cache2kBuilder.class, SpringCache2kCache.class, Cache2kCacheMetrics.class })
50+
static class Cache2kCacheMeterBinderProviderConfiguration {
51+
52+
@Bean
53+
Cache2kCacheMeterBinderProvider cache2kCacheMeterBinderProvider() {
54+
return new Cache2kCacheMeterBinderProvider();
55+
}
56+
57+
}
58+
4459
@Configuration(proxyBeanMethods = false)
4560
@ConditionalOnClass({ CaffeineCache.class, com.github.benmanes.caffeine.cache.Cache.class })
4661
static class CaffeineCacheMeterBinderProviderConfiguration {

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/cache/CacheMetricsAutoConfigurationTests.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -39,6 +39,16 @@ class CacheMetricsAutoConfigurationTests {
3939
.withUserConfiguration(CachingConfiguration.class).withConfiguration(
4040
AutoConfigurations.of(CacheAutoConfiguration.class, CacheMetricsAutoConfiguration.class));
4141

42+
@Test
43+
void autoConfiguredCache2kIsInstrumented() {
44+
this.contextRunner.withPropertyValues("spring.cache.type=cache2k", "spring.cache.cache-names=cache1,cache2")
45+
.run((context) -> {
46+
MeterRegistry registry = context.getBean(MeterRegistry.class);
47+
registry.get("cache.gets").tags("name", "cache1").tags("cacheManager", "cacheManager").meter();
48+
registry.get("cache.gets").tags("name", "cache2").tags("cacheManager", "cacheManager").meter();
49+
});
50+
}
51+
4252
@Test
4353
void autoConfiguredCacheManagerIsInstrumented() {
4454
this.contextRunner.withPropertyValues("spring.cache.type=caffeine", "spring.cache.cache-names=cache1,cache2")

spring-boot-project/spring-boot-actuator/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ dependencies {
4040
}
4141
optional("org.apache.tomcat.embed:tomcat-embed-core")
4242
optional("org.aspectj:aspectjweaver")
43+
optional("org.cache2k:cache2k-micrometer")
44+
optional("org.cache2k:cache2k-spring")
4345
optional("org.eclipse.jetty:jetty-server") {
4446
exclude(group: "javax.servlet", module: "javax.servlet-api")
4547
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2012-2022 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+
package org.springframework.boot.actuate.metrics.cache;
18+
19+
import io.micrometer.core.instrument.Tag;
20+
import io.micrometer.core.instrument.binder.MeterBinder;
21+
import org.cache2k.extra.micrometer.Cache2kCacheMetrics;
22+
import org.cache2k.extra.spring.SpringCache2kCache;
23+
24+
/**
25+
* {@link CacheMeterBinderProvider} implementation for cache2k.
26+
*
27+
* @author Jens Wilke
28+
* @since 2.7.0
29+
*/
30+
public class Cache2kCacheMeterBinderProvider implements CacheMeterBinderProvider<SpringCache2kCache> {
31+
32+
@Override
33+
public MeterBinder getMeterBinder(SpringCache2kCache cache, Iterable<Tag> tags) {
34+
return new Cache2kCacheMetrics(cache.getNativeCache(), tags);
35+
}
36+
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2012-2022 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+
package org.springframework.boot.actuate.metrics.cache;
18+
19+
import java.util.Collections;
20+
21+
import io.micrometer.core.instrument.binder.MeterBinder;
22+
import org.cache2k.extra.micrometer.Cache2kCacheMetrics;
23+
import org.cache2k.extra.spring.SpringCache2kCacheManager;
24+
import org.junit.jupiter.api.Test;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
28+
/**
29+
* Tests for {@link Cache2kCacheMeterBinderProvider}.
30+
*
31+
* @author Stephane Nicoll
32+
*/
33+
class Cache2kCacheMeterBinderProviderTests {
34+
35+
@Test
36+
void cache2kCacheProvider() {
37+
SpringCache2kCacheManager cacheManager = new SpringCache2kCacheManager()
38+
.addCaches((builder) -> builder.name("test"));
39+
MeterBinder meterBinder = new Cache2kCacheMeterBinderProvider().getMeterBinder(cacheManager.getCache("test"),
40+
Collections.emptyList());
41+
assertThat(meterBinder).isInstanceOf(Cache2kCacheMetrics.class);
42+
}
43+
44+
}

spring-boot-project/spring-boot-autoconfigure/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ dependencies {
7777
optional("com.zaxxer:HikariCP")
7878
optional("nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect")
7979
optional("org.aspectj:aspectjweaver")
80+
optional("org.cache2k:cache2k-spring")
81+
optional("org.cache2k:cache2k-micrometer")
8082
optional("org.eclipse.jetty:jetty-webapp") {
8183
exclude group: "javax.servlet", module: "javax.servlet-api"
8284
exclude(group: "org.eclipse.jetty", module: "jetty-jndi")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2012-2022 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+
package org.springframework.boot.autoconfigure.cache;
18+
19+
import java.util.Collection;
20+
21+
import org.cache2k.Cache2kBuilder;
22+
import org.cache2k.extra.spring.SpringCache2kCacheManager;
23+
24+
import org.springframework.beans.factory.ObjectProvider;
25+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
26+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
27+
import org.springframework.cache.CacheManager;
28+
import org.springframework.context.annotation.Bean;
29+
import org.springframework.context.annotation.Conditional;
30+
import org.springframework.context.annotation.Configuration;
31+
import org.springframework.util.CollectionUtils;
32+
33+
/**
34+
* Cache2k cache configuration.
35+
*
36+
* @author Jens Wilke
37+
*/
38+
@Configuration(proxyBeanMethods = false)
39+
@ConditionalOnClass({ Cache2kBuilder.class, SpringCache2kCacheManager.class })
40+
@ConditionalOnMissingBean(CacheManager.class)
41+
@Conditional(CacheCondition.class)
42+
class Cache2kCacheConfiguration {
43+
44+
@Bean
45+
SpringCache2kCacheManager cacheManager(CacheProperties cacheProperties, CacheManagerCustomizers customizers, ObjectProvider<Cache2kDefaults> defaults) {
46+
SpringCache2kCacheManager cacheManager = new SpringCache2kCacheManager();
47+
Cache2kDefaults specifiedDefaults = defaults.getIfAvailable();
48+
if (specifiedDefaults != null) {
49+
cacheManager.defaultSetup((builder) -> {
50+
specifiedDefaults.customize(builder);
51+
return builder;
52+
});
53+
}
54+
Collection<String> cacheNames = cacheProperties.getCacheNames();
55+
if (!CollectionUtils.isEmpty(cacheNames)) {
56+
cacheManager.setDefaultCacheNames(cacheNames);
57+
}
58+
return customizers.customize(cacheManager);
59+
}
60+
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2012-2022 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+
package org.springframework.boot.autoconfigure.cache;
18+
19+
import org.cache2k.Cache2kBuilder;
20+
21+
/**
22+
* Default configuration for cache2k when Spring boot auto configuration is creating the
23+
* Cache Manager.
24+
*
25+
* @author Jens Wilke
26+
* @since 2.7.0
27+
*/
28+
public interface Cache2kDefaults {
29+
30+
void customize(Cache2kBuilder<?, ?> builder);
31+
32+
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/CacheConfigurations.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ final class CacheConfigurations {
4141
mappings.put(CacheType.COUCHBASE, CouchbaseCacheConfiguration.class.getName());
4242
mappings.put(CacheType.REDIS, RedisCacheConfiguration.class.getName());
4343
mappings.put(CacheType.CAFFEINE, CaffeineCacheConfiguration.class.getName());
44+
mappings.put(CacheType.CACHE2K, Cache2kCacheConfiguration.class.getName());
4445
mappings.put(CacheType.SIMPLE, SimpleCacheConfiguration.class.getName());
4546
mappings.put(CacheType.NONE, NoOpCacheConfiguration.class.getName());
4647
MAPPINGS = Collections.unmodifiableMap(mappings);

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/CacheType.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ public enum CacheType {
5151
*/
5252
REDIS,
5353

54+
/**
55+
* Cache2k backed caching.
56+
*/
57+
CACHE2K,
58+
5459
/**
5560
* Caffeine backed caching.
5661
*/

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/AbstractCacheAutoConfigurationTests.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.Map;
2323

2424
import com.hazelcast.spring.cache.HazelcastCacheManager;
25+
import org.cache2k.extra.spring.SpringCache2kCacheManager;
2526

2627
import org.springframework.boot.autoconfigure.AutoConfigurations;
2728
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
@@ -119,6 +120,13 @@ CacheManagerCustomizer<HazelcastCacheManager> hazelcastCacheManagerCustomizer()
119120
};
120121
}
121122

123+
@Bean
124+
CacheManagerCustomizer<SpringCache2kCacheManager> cache2kCacheManagerCustomizer() {
125+
return new CacheManagerTestCustomizer<SpringCache2kCacheManager>() {
126+
127+
};
128+
}
129+
122130
@Bean
123131
CacheManagerCustomizer<CaffeineCacheManager> caffeineCacheManagerCustomizer() {
124132
return new CacheManagerTestCustomizer<>() {

0 commit comments

Comments
 (0)