Add support for EhCache
Include auto-configuration support for EhCache with auto-detection of the default `ehcache.xml` at the root of the classpath. EhCache configuration can also be set via `spring.cache.config`. See gh-2633
This commit is contained in:
parent
151220f41d
commit
48959f2553
|
|
@ -430,6 +430,11 @@
|
|||
<artifactId>javax.mail-api</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.sf.ehcache</groupId>
|
||||
<artifactId>ehcache</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.aspectj</groupId>
|
||||
<artifactId>aspectjweaver</artifactId>
|
||||
|
|
|
|||
|
|
@ -27,6 +27,7 @@ import org.springframework.util.Assert;
|
|||
* Configuration properties for the cache abstraction.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @author Eddú Meléndez
|
||||
* @since 1.3.0
|
||||
*/
|
||||
@ConfigurationProperties(prefix = "spring.cache")
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ package org.springframework.boot.autoconfigure.cache;
|
|||
*
|
||||
* @author Stephane Nicoll
|
||||
* @author Phillip Webb
|
||||
* @author Eddú Meléndez
|
||||
* @since 1.3.0
|
||||
*/
|
||||
public enum CacheType {
|
||||
|
|
@ -30,6 +31,11 @@ public enum CacheType {
|
|||
*/
|
||||
GENERIC(GenericCacheConfiguration.class),
|
||||
|
||||
/**
|
||||
* EhCache backed caching.
|
||||
*/
|
||||
EHCACHE(EhCacheCacheConfiguration.class),
|
||||
|
||||
/**
|
||||
* Haezelcast backed caching
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -0,0 +1,70 @@
|
|||
/*
|
||||
* Copyright 2012-2015 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
|
||||
*
|
||||
* http://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.autoconfigure.cache;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.cache.CacheManager;
|
||||
import org.springframework.cache.ehcache.EhCacheCacheManager;
|
||||
import org.springframework.cache.ehcache.EhCacheManagerUtils;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Conditional;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
/**
|
||||
* EhCache cache configuration. Only kick in if a configuration file location is set or if
|
||||
* a default configuration file exists.
|
||||
*
|
||||
* @author Eddú Meléndez
|
||||
* @since 1.3.0
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnClass(EhCacheCacheManager.class)
|
||||
@ConditionalOnMissingBean(CacheManager.class)
|
||||
@Conditional({ CacheCondition.class,
|
||||
EhCacheCacheConfiguration.ConfigAvailableCondition.class })
|
||||
class EhCacheCacheConfiguration {
|
||||
|
||||
@Autowired
|
||||
private CacheProperties properties;
|
||||
|
||||
@Bean
|
||||
public EhCacheCacheManager cacheManager() {
|
||||
Resource location = this.properties.resolveConfigLocation();
|
||||
if (location != null) {
|
||||
return new EhCacheCacheManager(
|
||||
EhCacheManagerUtils.buildCacheManager(location));
|
||||
}
|
||||
return new EhCacheCacheManager(EhCacheManagerUtils.buildCacheManager());
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the EhCache configuration is available. This either kick in if a
|
||||
* default configuration has been found or if property referring to the file to use
|
||||
* has been set.
|
||||
*/
|
||||
static class ConfigAvailableCondition extends CacheConfigFileCondition {
|
||||
|
||||
public ConfigAvailableCondition() {
|
||||
super("EhCache", "classpath:/ehcache.xml");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -38,6 +38,7 @@ import org.springframework.cache.annotation.CachingConfigurerSupport;
|
|||
import org.springframework.cache.annotation.EnableCaching;
|
||||
import org.springframework.cache.concurrent.ConcurrentMapCache;
|
||||
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
|
||||
import org.springframework.cache.ehcache.EhCacheCacheManager;
|
||||
import org.springframework.cache.guava.GuavaCache;
|
||||
import org.springframework.cache.guava.GuavaCacheManager;
|
||||
import org.springframework.cache.interceptor.CacheOperationInvocationContext;
|
||||
|
|
@ -71,6 +72,7 @@ import static org.mockito.Mockito.verify;
|
|||
* Tests for {@link CacheAutoConfiguration}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @author Eddú Meléndez
|
||||
*/
|
||||
public class CacheAutoConfigurationTests {
|
||||
|
||||
|
|
@ -248,6 +250,41 @@ public class CacheAutoConfigurationTests {
|
|||
"spring.cache.jcache.provider=" + wrongCachingProviderFqn);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ehCacheCacheWithCaches() {
|
||||
load(DefaultCacheConfiguration.class, "spring.cache.type=ehcache");
|
||||
EhCacheCacheManager cacheManager = null;
|
||||
try {
|
||||
cacheManager = validateCacheManager(EhCacheCacheManager.class);
|
||||
assertThat(cacheManager.getCacheNames(),
|
||||
containsInAnyOrder("cacheTest1", "cacheTest2"));
|
||||
assertThat(cacheManager.getCacheNames(), hasSize(2));
|
||||
}
|
||||
finally {
|
||||
if (cacheManager != null) {
|
||||
cacheManager.getCacheManager().shutdown();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ehCacheCacheWithLocation() {
|
||||
load(DefaultCacheConfiguration.class, "spring.cache.type=ehcache",
|
||||
"spring.cache.config=cache/ehcache-override.xml");
|
||||
EhCacheCacheManager cacheManager = null;
|
||||
try {
|
||||
cacheManager = validateCacheManager(EhCacheCacheManager.class);
|
||||
assertThat(cacheManager.getCacheNames(),
|
||||
containsInAnyOrder("cacheOverrideTest1", "cacheOverrideTest2"));
|
||||
assertThat(cacheManager.getCacheNames(), hasSize(2));
|
||||
}
|
||||
finally {
|
||||
if (cacheManager != null) {
|
||||
cacheManager.getCacheManager().shutdown();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hazelcastCacheExplicit() {
|
||||
load(DefaultCacheConfiguration.class, "spring.cache.type=hazelcast");
|
||||
|
|
|
|||
|
|
@ -0,0 +1,10 @@
|
|||
<ehcache>
|
||||
<cache name="cacheOverrideTest1"
|
||||
maxBytesLocalHeap="50m"
|
||||
timeToLiveSeconds="100">
|
||||
</cache>
|
||||
<cache name="cacheOverrideTest2"
|
||||
maxBytesLocalHeap="50m"
|
||||
timeToLiveSeconds="100">
|
||||
</cache>
|
||||
</ehcache>
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
<ehcache>
|
||||
|
||||
<cache name="cacheTest1" maxBytesLocalHeap="50m" timeToLiveSeconds="100">
|
||||
</cache>
|
||||
|
||||
<cache name="cacheTest2" maxBytesLocalHeap="50m" timeToLiveSeconds="100">
|
||||
</cache>
|
||||
|
||||
</ehcache>
|
||||
|
|
@ -60,6 +60,7 @@
|
|||
<crashub.version>1.3.1</crashub.version>
|
||||
<derby.version>10.10.2.0</derby.version>
|
||||
<dropwizard-metrics.version>3.1.1</dropwizard-metrics.version>
|
||||
<ehcache.version>2.9.1</ehcache.version>
|
||||
<flyway.version>3.2</flyway.version>
|
||||
<freemarker.version>2.3.22</freemarker.version>
|
||||
<gemfire.version>7.0.2</gemfire.version>
|
||||
|
|
@ -671,6 +672,11 @@
|
|||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>${mysql.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.sf.ehcache</groupId>
|
||||
<artifactId>ehcache</artifactId>
|
||||
<version>${ehcache.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>nz.net.ultraq.thymeleaf</groupId>
|
||||
<artifactId>thymeleaf-layout-dialect</artifactId>
|
||||
|
|
|
|||
Loading…
Reference in New Issue