Skip to content

Commit 160c94a

Browse files
committed
[DRAFT] Restore Ehcache 3 Support
1 parent bfe9ded commit 160c94a

File tree

8 files changed

+161
-150
lines changed

8 files changed

+161
-150
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ dependencies {
8888
optional("org.eclipse.jetty.websocket:websocket-jetty-server") {
8989
exclude(group: "org.eclipse.jetty", module: "jetty-jndi")
9090
}
91+
optional("org.ehcache:ehcache") {
92+
artifact {
93+
classifier = 'jakarta'
94+
}
95+
}
9196
optional("org.elasticsearch.client:elasticsearch-rest-client") {
9297
exclude group: "commons-logging", module: "commons-logging"
9398
}

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

Lines changed: 0 additions & 145 deletions
This file was deleted.

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

Lines changed: 131 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
package org.springframework.boot.autoconfigure.cache;
1818

1919
import java.util.ArrayList;
20+
import java.util.Arrays;
2021
import java.util.Collections;
2122
import java.util.List;
23+
import java.util.Map;
2224

2325
import javax.cache.Caching;
2426
import javax.cache.configuration.CompleteConfiguration;
@@ -32,6 +34,7 @@
3234
import com.hazelcast.core.Hazelcast;
3335
import com.hazelcast.core.HazelcastInstance;
3436
import com.hazelcast.spring.cache.HazelcastCacheManager;
37+
import org.ehcache.jsr107.EhcacheCachingProvider;
3538
import org.junit.jupiter.api.Test;
3639

3740
import org.springframework.beans.factory.BeanCreationException;
@@ -41,6 +44,8 @@
4144
import org.springframework.boot.autoconfigure.cache.support.MockCachingProvider.MockCacheManager;
4245
import org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration;
4346
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
47+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
48+
import org.springframework.boot.test.context.runner.ContextConsumer;
4449
import org.springframework.boot.testsupport.classpath.ClassPathExclusions;
4550
import org.springframework.cache.Cache;
4651
import org.springframework.cache.CacheManager;
@@ -81,7 +86,10 @@
8186
* @author Ryon Day
8287
*/
8388
@ClassPathExclusions("hazelcast-client-*.jar")
84-
class CacheAutoConfigurationTests extends AbstractCacheAutoConfigurationTests {
89+
class CacheAutoConfigurationTests {
90+
91+
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
92+
.withConfiguration(AutoConfigurations.of(CacheAutoConfiguration.class));
8593

8694
@Test
8795
void noEnableCaching() {
@@ -425,6 +433,34 @@ void jCacheCacheUseBeanClassLoader() {
425433
});
426434
}
427435

436+
@Test
437+
void ehcache3AsJCacheWithCaches() {
438+
String cachingProviderFqn = EhcacheCachingProvider.class.getName();
439+
this.contextRunner.withUserConfiguration(DefaultCacheConfiguration.class)
440+
.withPropertyValues("spring.cache.type=jcache", "spring.cache.jcache.provider=" + cachingProviderFqn,
441+
"spring.cache.cacheNames[0]=foo", "spring.cache.cacheNames[1]=bar")
442+
.run((context) -> {
443+
JCacheCacheManager cacheManager = getCacheManager(context, JCacheCacheManager.class);
444+
assertThat(cacheManager.getCacheNames()).containsOnly("foo", "bar");
445+
});
446+
}
447+
448+
@Test
449+
void ehcache3AsJCacheWithConfig() {
450+
String cachingProviderFqn = EhcacheCachingProvider.class.getName();
451+
String configLocation = "ehcache3.xml";
452+
this.contextRunner.withUserConfiguration(DefaultCacheConfiguration.class)
453+
.withPropertyValues("spring.cache.type=jcache", "spring.cache.jcache.provider=" + cachingProviderFqn,
454+
"spring.cache.jcache.config=" + configLocation)
455+
.run((context) -> {
456+
JCacheCacheManager cacheManager = getCacheManager(context, JCacheCacheManager.class);
457+
458+
Resource configResource = new ClassPathResource(configLocation);
459+
assertThat(cacheManager.getCacheManager().getURI()).isEqualTo(configResource.getURI());
460+
assertThat(cacheManager.getCacheNames()).containsOnly("foo", "bar");
461+
});
462+
}
463+
428464
@Test
429465
void hazelcastCacheExplicit() {
430466
this.contextRunner.withConfiguration(AutoConfigurations.of(HazelcastAutoConfiguration.class))
@@ -620,6 +656,32 @@ private RedisCacheConfiguration getDefaultRedisCacheConfiguration(RedisCacheMana
620656
return (RedisCacheConfiguration) ReflectionTestUtils.getField(cacheManager, "defaultCacheConfig");
621657
}
622658

659+
private <T extends CacheManager> T getCacheManager(AssertableApplicationContext loaded, Class<T> type) {
660+
CacheManager cacheManager = loaded.getBean(CacheManager.class);
661+
assertThat(cacheManager).as("Wrong cache manager type").isInstanceOf(type);
662+
return type.cast(cacheManager);
663+
}
664+
665+
@SuppressWarnings("rawtypes")
666+
private ContextConsumer<AssertableApplicationContext> verifyCustomizers(String... expectedCustomizerNames) {
667+
return (context) -> {
668+
CacheManager cacheManager = getCacheManager(context, CacheManager.class);
669+
List<String> expected = new ArrayList<>(Arrays.asList(expectedCustomizerNames));
670+
Map<String, CacheManagerTestCustomizer> customizer = context
671+
.getBeansOfType(CacheManagerTestCustomizer.class);
672+
customizer.forEach((key, value) -> {
673+
if (expected.contains(key)) {
674+
expected.remove(key);
675+
assertThat(value.cacheManager).isSameAs(cacheManager);
676+
}
677+
else {
678+
assertThat(value.cacheManager).isNull();
679+
}
680+
});
681+
assertThat(expected).hasSize(0);
682+
};
683+
}
684+
623685
@Configuration(proxyBeanMethods = false)
624686
static class EmptyConfiguration {
625687

@@ -878,4 +940,72 @@ public Object postProcessAfterInitialization(Object bean, String beanName) {
878940

879941
}
880942

943+
@Configuration(proxyBeanMethods = false)
944+
static class CacheManagerCustomizersConfiguration {
945+
946+
@Bean
947+
CacheManagerCustomizer<CacheManager> allCacheManagerCustomizer() {
948+
return new CacheManagerTestCustomizer<>() {
949+
950+
};
951+
}
952+
953+
@Bean
954+
CacheManagerCustomizer<ConcurrentMapCacheManager> simpleCacheManagerCustomizer() {
955+
return new CacheManagerTestCustomizer<>() {
956+
957+
};
958+
}
959+
960+
@Bean
961+
CacheManagerCustomizer<SimpleCacheManager> genericCacheManagerCustomizer() {
962+
return new CacheManagerTestCustomizer<>() {
963+
964+
};
965+
}
966+
967+
@Bean
968+
CacheManagerCustomizer<CouchbaseCacheManager> couchbaseCacheManagerCustomizer() {
969+
return new CacheManagerTestCustomizer<CouchbaseCacheManager>() {
970+
971+
};
972+
}
973+
974+
@Bean
975+
CacheManagerCustomizer<RedisCacheManager> redisCacheManagerCustomizer() {
976+
return new CacheManagerTestCustomizer<>() {
977+
978+
};
979+
}
980+
981+
@Bean
982+
CacheManagerCustomizer<HazelcastCacheManager> hazelcastCacheManagerCustomizer() {
983+
return new CacheManagerTestCustomizer<>() {
984+
985+
};
986+
}
987+
988+
@Bean
989+
CacheManagerCustomizer<CaffeineCacheManager> caffeineCacheManagerCustomizer() {
990+
return new CacheManagerTestCustomizer<>() {
991+
992+
};
993+
}
994+
995+
}
996+
997+
abstract static class CacheManagerTestCustomizer<T extends CacheManager> implements CacheManagerCustomizer<T> {
998+
999+
private T cacheManager;
1000+
1001+
@Override
1002+
public void customize(T cacheManager) {
1003+
if (this.cacheManager != null) {
1004+
throw new IllegalStateException("Customized invoked twice");
1005+
}
1006+
this.cacheManager = cacheManager;
1007+
}
1008+
1009+
}
1010+
8811011
}

spring-boot-project/spring-boot-autoconfigure/src/test/resources/ehcache3.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
xmlns='http://www.ehcache.org/v3'
44
xmlns:jsr107='http://www.ehcache.org/v3/jsr107'
55
xsi:schemaLocation="
6-
http://www.ehcache.org/v3 https://www.ehcache.org/schema/ehcache-core-3.1.xsd
7-
http://www.ehcache.org/v3/jsr107 https://www.ehcache.org/schema/ehcache-107-ext-3.1.xsd">
6+
http://www.ehcache.org/v3 https://www.ehcache.org/schema/ehcache-core-3.9.xsd
7+
http://www.ehcache.org/v3/jsr107 https://www.ehcache.org/schema/ehcache-107-ext-3.9.xsd">
88

99
<cache-template name="example">
1010
<heap unit="entries">200</heap>

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,19 @@ bom {
191191
]
192192
}
193193
}
194+
library("Ehcache3", "3.10.0-alpha1") {
195+
group("org.ehcache") {
196+
modules = [
197+
"ehcache" {
198+
classifier = 'jakarta'
199+
},
200+
"ehcache-clustered",
201+
"ehcache-transactions" {
202+
classifier = 'jakarta'
203+
}
204+
]
205+
}
206+
}
194207
library("Elasticsearch", "7.17.0") {
195208
group("org.elasticsearch") {
196209
modules = [

spring-boot-project/spring-boot-docs/src/docs/asciidoc/documentation/io.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
== IO
33
If your application needs IO capabilities, see one or more of the following sections:
44

5-
* *Caching:* <<io#io.caching, Caching support with Hazelcast and more>>
5+
* *Caching:* <<io#io.caching, Caching support with EhCache, Hazelcast and more>>
66
* *Quartz:* <<io#io.quartz, Quartz Scheduling>>
77
* *Mail:* <<io#io.email, Sending Email>>
88
* *Validation:* <<io#io.validation, JSR-303 Validation>>

spring-boot-project/spring-boot-docs/src/docs/asciidoc/io/caching.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ The cache abstraction does not provide an actual store and relies on abstraction
3737
If you have not defined a bean of type `CacheManager` or a `CacheResolver` named `cacheResolver` (see {spring-framework-api}/cache/annotation/CachingConfigurer.html[`CachingConfigurer`]), Spring Boot tries to detect the following providers (in the indicated order):
3838

3939
. <<io#io.caching.provider.generic,Generic>>
40-
. <<io#io.caching.provider.jcache,JCache (JSR-107)>> (Hazelcast and others)
40+
. <<io#io.caching.provider.jcache,JCache (JSR-107)>> (EhCache 3, Hazelcast, and others)
4141
. <<io#io.caching.provider.hazelcast,Hazelcast>>
4242
. <<io#io.caching.provider.couchbase,Couchbase>>
4343
. <<io#io.caching.provider.redis,Redis>>

0 commit comments

Comments
 (0)