Skip to content

Commit 2088381

Browse files
committed
Merge branch '2.7.x'
2 parents 5db1547 + 6805be5 commit 2088381

File tree

19 files changed

+385
-2
lines changed

19 files changed

+385
-2
lines changed

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

+4
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ dependencies {
9393
optional("org.apache.tomcat.embed:tomcat-embed-el")
9494
optional("org.apache.tomcat:tomcat-jdbc")
9595
optional("org.aspectj:aspectjweaver")
96+
optional("org.cache2k:cache2k-micrometer")
97+
optional("org.cache2k:cache2k-spring")
9698
optional("org.eclipse.jetty:jetty-server") {
9799
exclude group: "javax.servlet", module: "javax.servlet-api"
98100
}
@@ -156,6 +158,7 @@ dependencies {
156158
testImplementation("org.aspectj:aspectjrt")
157159
testImplementation("org.assertj:assertj-core")
158160
testImplementation("org.awaitility:awaitility")
161+
testImplementation("org.cache2k:cache2k-api")
159162
testImplementation("org.eclipse.jetty:jetty-webapp") {
160163
exclude group: "javax.servlet", module: "javax.servlet-api"
161164
}
@@ -177,6 +180,7 @@ dependencies {
177180

178181
testRuntimeOnly("jakarta.management.j2ee:jakarta.management.j2ee-api")
179182
testRuntimeOnly("jakarta.transaction:jakarta.transaction-api")
183+
testRuntimeOnly("org.cache2k:cache2k-core")
180184
testRuntimeOnly("org.springframework.security:spring-security-oauth2-jose")
181185
testRuntimeOnly("org.springframework.security:spring-security-oauth2-resource-server")
182186
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

+16-1
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

+11-1
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

+2
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

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ 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")
8081
optional("org.eclipse.jetty:jetty-webapp") {
8182
exclude group: "javax.servlet", module: "javax.servlet-api"
8283
exclude(group: "org.eclipse.jetty", module: "jetty-jndi")
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.autoconfigure.cache;
18+
19+
import org.cache2k.Cache2kBuilder;
20+
21+
/**
22+
* Callback interface that can be implemented by beans wishing to customize the default
23+
* setup for caches added to the manager via addCaches and for dynamically created caches.
24+
*
25+
* @author Jens Wilke
26+
* @author Stephane Nicoll
27+
* @since 2.7.0
28+
*/
29+
public interface Cache2kBuilderCustomizer {
30+
31+
/**
32+
* Customize the default cache settings.
33+
* @param builder the builder to customize
34+
*/
35+
void customize(Cache2kBuilder<?, ?> builder);
36+
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
import java.util.function.Function;
21+
22+
import org.cache2k.Cache2kBuilder;
23+
import org.cache2k.extra.spring.SpringCache2kCacheManager;
24+
25+
import org.springframework.beans.factory.ObjectProvider;
26+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
27+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
28+
import org.springframework.cache.CacheManager;
29+
import org.springframework.context.annotation.Bean;
30+
import org.springframework.context.annotation.Conditional;
31+
import org.springframework.context.annotation.Configuration;
32+
import org.springframework.util.CollectionUtils;
33+
34+
/**
35+
* Cache2k cache configuration.
36+
*
37+
* @author Jens Wilke
38+
* @author Stephane Nicoll
39+
*/
40+
@Configuration(proxyBeanMethods = false)
41+
@ConditionalOnClass({ Cache2kBuilder.class, SpringCache2kCacheManager.class })
42+
@ConditionalOnMissingBean(CacheManager.class)
43+
@Conditional(CacheCondition.class)
44+
class Cache2kCacheConfiguration {
45+
46+
@Bean
47+
SpringCache2kCacheManager cacheManager(CacheProperties cacheProperties, CacheManagerCustomizers customizers,
48+
ObjectProvider<Cache2kBuilderCustomizer> cache2kBuilderCustomizers) {
49+
SpringCache2kCacheManager cacheManager = new SpringCache2kCacheManager();
50+
cacheManager.defaultSetup(configureDefaults(cache2kBuilderCustomizers));
51+
Collection<String> cacheNames = cacheProperties.getCacheNames();
52+
if (!CollectionUtils.isEmpty(cacheNames)) {
53+
cacheManager.setDefaultCacheNames(cacheNames);
54+
}
55+
return customizers.customize(cacheManager);
56+
}
57+
58+
private Function<Cache2kBuilder<?, ?>, Cache2kBuilder<?, ?>> configureDefaults(
59+
ObjectProvider<Cache2kBuilderCustomizer> cache2kBuilderCustomizers) {
60+
return (builder) -> {
61+
cache2kBuilderCustomizers.orderedStream().forEach((customizer) -> customizer.customize(builder));
62+
return builder;
63+
};
64+
}
65+
66+
}

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

+1
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

+5
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

+8
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)