Skip to content

Commit 085ba06

Browse files
committed
cache2k: Fix checkstyle issues, improve JavaDoc comments, tests
1 parent d55609a commit 085ba06

File tree

10 files changed

+62
-50
lines changed

10 files changed

+62
-50
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ dependencies {
4242
}
4343
optional("com.fasterxml.jackson.dataformat:jackson-dataformat-xml")
4444
optional("com.github.ben-manes.caffeine:caffeine")
45-
optional("org.cache2k:cache2k-api")
4645
optional("org.cache2k:cache2k-spring")
4746
optional("org.cache2k:cache2k-micrometer")
4847
optional("com.hazelcast:hazelcast")

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@
1919
import com.hazelcast.core.Hazelcast;
2020
import com.hazelcast.spring.cache.HazelcastCache;
2121
import io.micrometer.core.instrument.binder.MeterBinder;
22-
2322
import org.cache2k.Cache2kBuilder;
23+
import org.cache2k.extra.micrometer.Cache2kCacheMetrics;
2424
import org.cache2k.extra.spring.SpringCache2kCache;
25+
2526
import org.springframework.boot.actuate.metrics.cache.Cache2kCacheMeterBinderProvider;
2627
import org.springframework.boot.actuate.metrics.cache.CacheMeterBinderProvider;
2728
import org.springframework.boot.actuate.metrics.cache.CaffeineCacheMeterBinderProvider;
@@ -45,7 +46,7 @@
4546
class CacheMeterBinderProvidersConfiguration {
4647

4748
@Configuration(proxyBeanMethods = false)
48-
@ConditionalOnClass({ SpringCache2kCache.class, Cache2kBuilder.class, org.cache2k.extra.micrometer.Cache2kCacheMetrics.class })
49+
@ConditionalOnClass({ SpringCache2kCache.class, Cache2kBuilder.class, Cache2kCacheMetrics.class })
4950
static class Cache2kCacheMeterBinderProviderConfiguration {
5051

5152
@Bean

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ class CacheMetricsAutoConfigurationTests {
4242
@Test
4343
void autoConfiguredCache2kIsInstrumented() {
4444
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-
});
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+
});
5050
}
5151

5252
@Test

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ dependencies {
1717
}
1818
optional("com.fasterxml.jackson.core:jackson-databind")
1919
optional("com.fasterxml.jackson.datatype:jackson-datatype-jsr310")
20-
optional("org.cache2k:cache2k-api")
2120
optional("org.cache2k:cache2k-spring")
2221
optional("org.cache2k:cache2k-micrometer")
2322
optional("com.github.ben-manes.caffeine:caffeine")

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
* {@link CacheMeterBinderProvider} implementation for cache2k.
2626
*
2727
* @author Jens Wilke
28+
* @since 2.7.0
2829
*/
2930
public class Cache2kCacheMeterBinderProvider implements CacheMeterBinderProvider<SpringCache2kCache> {
3031

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ dependencies {
6363
optional("org.apache.tomcat:tomcat-jdbc")
6464
optional("org.apiguardian:apiguardian-api")
6565
optional("org.codehaus.groovy:groovy-templates")
66-
optional("org.cache2k:cache2k-api")
6766
optional("org.cache2k:cache2k-spring")
6867
optional("org.cache2k:cache2k-micrometer")
6968
optional("com.github.ben-manes.caffeine:caffeine")

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616

1717
package org.springframework.boot.autoconfigure.cache;
1818

19+
import java.util.Collection;
20+
1921
import org.cache2k.Cache2kBuilder;
2022
import org.cache2k.extra.spring.SpringCache2kCacheManager;
23+
2124
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2225
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2326
import org.springframework.cache.CacheManager;
@@ -26,9 +29,9 @@
2629
import org.springframework.context.annotation.Configuration;
2730
import org.springframework.util.CollectionUtils;
2831

29-
import java.util.Collection;
30-
3132
/**
33+
* Support for cache2k auto configuration.
34+
*
3235
* @author Jens Wilke
3336
*/
3437
@Configuration(proxyBeanMethods = false)
@@ -40,25 +43,26 @@ class Cache2kCacheConfiguration {
4043
@Bean
4144
SpringCache2kCacheManager cacheManager(CacheProperties cacheProperties, CacheManagerCustomizers customizers) {
4245
String managerName = cacheProperties.getCache2k().getManagerName();
43-
SpringCache2kCacheManager cacheManager =
44-
managerName == null ? new SpringCache2kCacheManager() : new SpringCache2kCacheManager(managerName);
46+
SpringCache2kCacheManager cacheManager = (managerName != null) ? new SpringCache2kCacheManager(managerName)
47+
: new SpringCache2kCacheManager();
4548
customizers.customize(cacheManager);
4649
addListedButUnknownCaches(cacheManager, cacheProperties.getCacheNames());
4750
return cacheManager;
4851
}
4952

5053
/**
51-
* Add caches yet unknown that are configured via the {@code CacheProperties}.
52-
* Don't add those before customization to be able the add the cache with programmatic configuration
53-
* via customization. These caches are created with default parameters provided either via the
54-
* customizer or the XML configuration
54+
* Add caches yet unknown that are configured via the {@code CacheProperties}. Don't
55+
* add those before customization to be able the add the cache with programmatic
56+
* configuration via customization. These caches are created with default parameters
57+
* provided either via the customizer or the XML configuration
58+
* @param cacheManager the cache manger to add the caches
59+
* @param cacheNames the cache names
5560
*/
5661
private void addListedButUnknownCaches(SpringCache2kCacheManager cacheManager, Collection<String> cacheNames) {
5762
Collection<String> knownCaches = cacheManager.getCacheNames();
5863
if (!CollectionUtils.isEmpty(cacheNames)) {
59-
cacheNames.stream()
60-
.filter(n -> !knownCaches.contains(n))
61-
.forEach(name -> cacheManager.addCaches(builder -> builder.name(name)));
64+
cacheNames.stream().filter((n) -> !knownCaches.contains(n))
65+
.forEach((name) -> cacheManager.addCaches((builder) -> builder.name(name)));
6266
}
6367
}
6468

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,21 +109,33 @@ public Resource resolveConfigLocation(Resource config) {
109109
}
110110

111111
/**
112-
* Cache2k specific cache properties.
113-
*/
112+
* Cache2k specific cache properties.
113+
*/
114114
public static class Cache2k {
115115

116116
/**
117-
* Alternative cache2k cache manager name to use.
118-
*/
117+
* The cache2k cache manager name to use, if unset/{@code null}, the manager name
118+
* {@value org.cache2k.extra.spring.SpringCache2kCacheManager#DEFAULT_SPRING_CACHE_MANAGER_NAME}
119+
* will be used. For better overview, it can be useful to separate a group of
120+
* caches into different cache managers, e.g. {@code "spring"} for caches used by
121+
* the Spring cache abstraction or {@code "hibernate"} for caches used by
122+
* hibernate. This avoids naming conflicts, since a cache manager defines a
123+
* separate scope. Per cache manager a separate default configuration can be used
124+
* to simplify cache configuration.
125+
*
126+
* @see org.cache2k.extra.spring.SpringCache2kCacheManager
127+
* @see org.cache2k.CacheManager
128+
*/
119129
private String managerName = null;
120130

121131
public String getManagerName() {
122-
return managerName;
132+
return this.managerName;
123133
}
134+
124135
public void setManagerName(String managerName) {
125136
this.managerName = managerName;
126137
}
138+
127139
}
128140

129141
/**

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import com.hazelcast.spring.cache.HazelcastCacheManager;
2525
import org.cache2k.extra.spring.SpringCache2kCacheManager;
26+
2627
import org.springframework.boot.autoconfigure.AutoConfigurations;
2728
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
2829
import org.springframework.boot.test.context.runner.ApplicationContextRunner;

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

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.util.ArrayList;
2020
import java.util.Collections;
2121
import java.util.List;
22-
import java.util.concurrent.TimeUnit;
2322

2423
import javax.cache.Caching;
2524
import javax.cache.configuration.CompleteConfiguration;
@@ -494,22 +493,23 @@ void jCacheCacheWithCachesAndCustomizer() {
494493
void cache2kCacheWithExplicitCaches() {
495494
this.contextRunner.withUserConfiguration(DefaultCacheConfiguration.class)
496495
.withPropertyValues("spring.cache.type=cache2k", "spring.cache.cacheNames=foo").run((context) -> {
497-
SpringCache2kCacheManager manager = getCacheManager(context, SpringCache2kCacheManager.class);
498-
assertThat(manager.getCacheNames()).containsOnly("foo");
499-
Cache foo = manager.getCache("foo");
500-
foo.get("1");
501-
assertThat(manager.getNativeCacheManager().getName()).isEqualTo("springDefault");
502-
});
496+
SpringCache2kCacheManager manager = getCacheManager(context, SpringCache2kCacheManager.class);
497+
assertThat(manager.getCacheNames()).containsOnly("foo");
498+
Cache foo = manager.getCache("foo");
499+
foo.get("1");
500+
assertThat(manager.getNativeCacheManager().getName()).isEqualTo("springDefault");
501+
});
503502
}
504503

505504
@Test
506505
void cache2kDifferentCacheManagerName() {
507506
this.contextRunner.withUserConfiguration(DefaultCacheConfiguration.class)
508507
.withPropertyValues("spring.cache.type=cache2k", "spring.cache.cacheNames=foo",
509-
"spring.cache.cache2k.managerName=separate").run((context) -> {
510-
SpringCache2kCacheManager manager = getCacheManager(context, SpringCache2kCacheManager.class);
511-
assertThat(manager.getNativeCacheManager().getName()).isEqualTo("separate");
512-
});
508+
"spring.cache.cache2k.managerName=separate")
509+
.run((context) -> {
510+
SpringCache2kCacheManager manager = getCacheManager(context, SpringCache2kCacheManager.class);
511+
assertThat(manager.getNativeCacheManager().getName()).isEqualTo("separate");
512+
});
513513
}
514514

515515
@Test
@@ -523,9 +523,12 @@ void cache2kCacheWithCustomizers() {
523523
void cache2kCacheWithExplicitCachesAndCustomizer() {
524524
this.contextRunner.withUserConfiguration(Cache2kAddCacheWithCustomizerConfiguration.class)
525525
.withPropertyValues("spring.cache.type=cache2k", "spring.cache.cacheNames=foo,bar").run((context) -> {
526-
SpringCache2kCacheManager manager = getCacheManager(context, SpringCache2kCacheManager.class);
527-
assertThat(manager.getCacheNames()).containsExactlyInAnyOrder("foo", "bar", "viaCustomizer");
528-
});
526+
SpringCache2kCacheManager manager = getCacheManager(context, SpringCache2kCacheManager.class);
527+
assertThat(manager.getCacheNames()).containsExactlyInAnyOrder("foo", "bar", "viaCustomizer");
528+
// check that default from customizer is applied
529+
assertThat(manager.getCache("bar").get("any").get()).isEqualTo("defaultSetup");
530+
assertThat(manager.getCache("viaCustomizer").get("any").get()).isEqualTo("defaultSetup");
531+
});
529532
}
530533

531534
@Test
@@ -788,16 +791,9 @@ static class Cache2kAddCacheWithCustomizerConfiguration {
788791

789792
@Bean
790793
CacheManagerCustomizer<SpringCache2kCacheManager> cache2kCustomizer() {
791-
return new CacheManagerCustomizer<SpringCache2kCacheManager>() {
792-
@Override
793-
public void customize(SpringCache2kCacheManager cacheManager) {
794-
cacheManager.defaultSetup(b -> b
795-
.entryCapacity(1000)
796-
.expireAfterWrite(5, TimeUnit.MINUTES)
797-
)
798-
.addCaches(b -> b.name("viaCustomizer"));
799-
}
800-
};
794+
return (cacheManager) -> cacheManager
795+
.defaultSetup((b) -> b.valueType(String.class).loader((key) -> "defaultSetup"))
796+
.addCaches((b) -> b.name("viaCustomizer"));
801797
}
802798

803799
}

0 commit comments

Comments
 (0)