Polish cache code
This commit is contained in:
@@ -51,8 +51,8 @@ abstract class CacheConfigFileCondition extends SpringBootCondition {
|
||||
RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(
|
||||
context.getEnvironment(), this.configPrefix);
|
||||
if (resolver.containsProperty("config")) {
|
||||
return ConditionOutcome.match("A '" + this.configPrefix + ".config' " +
|
||||
"property is specified");
|
||||
return ConditionOutcome.match("A '" + this.configPrefix + ".config' "
|
||||
+ "property is specified");
|
||||
}
|
||||
return getResourceOutcome(context, metadata);
|
||||
}
|
||||
|
||||
@@ -92,14 +92,15 @@ public class CacheProperties {
|
||||
|
||||
/**
|
||||
* Resolve the config location if set.
|
||||
* @param config the config resource
|
||||
* @return the location or {@code null} if it is not set
|
||||
* @throws IllegalArgumentException if the config attribute is set to a unknown
|
||||
* location
|
||||
*/
|
||||
public Resource resolveConfigLocation(Resource config) {
|
||||
if (config != null) {
|
||||
Assert.isTrue(config.exists(), "Cache configuration does not " +
|
||||
"exist '" + config.getDescription() + "'");
|
||||
Assert.isTrue(config.exists(), "Cache configuration does not " + "exist '"
|
||||
+ config.getDescription() + "'");
|
||||
return config;
|
||||
}
|
||||
return null;
|
||||
@@ -116,12 +117,13 @@ public class CacheProperties {
|
||||
private Resource config;
|
||||
|
||||
public Resource getConfig() {
|
||||
return config;
|
||||
return this.config;
|
||||
}
|
||||
|
||||
public void setConfig(Resource config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -135,7 +137,7 @@ public class CacheProperties {
|
||||
private Resource config;
|
||||
|
||||
public Resource getConfig() {
|
||||
return config;
|
||||
return this.config;
|
||||
}
|
||||
|
||||
public void setConfig(Resource config) {
|
||||
@@ -155,7 +157,7 @@ public class CacheProperties {
|
||||
private Resource config;
|
||||
|
||||
public Resource getConfig() {
|
||||
return config;
|
||||
return this.config;
|
||||
}
|
||||
|
||||
public void setConfig(Resource config) {
|
||||
@@ -170,8 +172,8 @@ public class CacheProperties {
|
||||
public static class JCache {
|
||||
|
||||
/**
|
||||
* The location of the configuration file to use to initialize the cache manager. The
|
||||
* configuration file is dependent of the underlying cache implementation.
|
||||
* The location of the configuration file to use to initialize the cache manager.
|
||||
* The configuration file is dependent of the underlying cache implementation.
|
||||
*/
|
||||
private Resource config;
|
||||
|
||||
@@ -191,12 +193,13 @@ public class CacheProperties {
|
||||
}
|
||||
|
||||
public Resource getConfig() {
|
||||
return config;
|
||||
return this.config;
|
||||
}
|
||||
|
||||
public void setConfig(Resource config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -55,8 +55,8 @@ class EhCacheCacheConfiguration {
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public CacheManager ehCacheCacheManager() {
|
||||
Resource location = this.cacheProperties.resolveConfigLocation(
|
||||
this.cacheProperties.getEhcache().getConfig());
|
||||
Resource location = this.cacheProperties
|
||||
.resolveConfigLocation(this.cacheProperties.getEhcache().getConfig());
|
||||
if (location != null) {
|
||||
return EhCacheManagerUtils.buildCacheManager(location);
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ import com.google.common.cache.CacheLoader;
|
||||
* @since 1.3.0
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnClass({CacheBuilder.class,GuavaCacheManager.class})
|
||||
@ConditionalOnClass({ CacheBuilder.class, GuavaCacheManager.class })
|
||||
@ConditionalOnMissingBean(CacheManager.class)
|
||||
@Conditional(CacheCondition.class)
|
||||
class GuavaCacheConfiguration {
|
||||
|
||||
@@ -64,8 +64,8 @@ class HazelcastCacheConfiguration {
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public HazelcastInstance hazelcastInstance() throws IOException {
|
||||
Resource location = this.cacheProperties.resolveConfigLocation(
|
||||
this.cacheProperties.getHazelcast().getConfig());
|
||||
Resource location = this.cacheProperties
|
||||
.resolveConfigLocation(this.cacheProperties.getHazelcast().getConfig());
|
||||
if (location != null) {
|
||||
Config cfg = new XmlConfigBuilder(location.getURL()).build();
|
||||
return Hazelcast.newHazelcastInstance(cfg);
|
||||
@@ -81,8 +81,8 @@ class HazelcastCacheConfiguration {
|
||||
static class ConfigAvailableCondition extends CacheConfigFileCondition {
|
||||
|
||||
public ConfigAvailableCondition() {
|
||||
super("Hazelcast", "spring.config.hazelcast",
|
||||
"file:./hazelcast.xml", "classpath:/hazelcast.xml");
|
||||
super("Hazelcast", "spring.config.hazelcast", "file:./hazelcast.xml",
|
||||
"classpath:/hazelcast.xml");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -24,7 +24,6 @@ import org.infinispan.configuration.cache.ConfigurationBuilder;
|
||||
import org.infinispan.manager.DefaultCacheManager;
|
||||
import org.infinispan.manager.EmbeddedCacheManager;
|
||||
import org.infinispan.spring.provider.SpringEmbeddedCacheManager;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
@@ -62,19 +61,20 @@ public class InfinispanCacheConfiguration {
|
||||
@Bean(destroyMethod = "stop")
|
||||
@ConditionalOnMissingBean
|
||||
public EmbeddedCacheManager infinispanCacheManager() throws IOException {
|
||||
EmbeddedCacheManager infinispanCacheManager = createEmbeddedCacheManager();
|
||||
EmbeddedCacheManager cacheManager = createEmbeddedCacheManager();
|
||||
List<String> cacheNames = this.cacheProperties.getCacheNames();
|
||||
if (!CollectionUtils.isEmpty(cacheNames)) {
|
||||
for (String cacheName : cacheNames) {
|
||||
infinispanCacheManager.defineConfiguration(cacheName, getDefaultCacheConfiguration());
|
||||
cacheManager.defineConfiguration(cacheName,
|
||||
getDefaultCacheConfiguration());
|
||||
}
|
||||
}
|
||||
return infinispanCacheManager;
|
||||
return cacheManager;
|
||||
}
|
||||
|
||||
private EmbeddedCacheManager createEmbeddedCacheManager() throws IOException {
|
||||
Resource location = this.cacheProperties.resolveConfigLocation(
|
||||
this.cacheProperties.getInfinispan().getConfig());
|
||||
Resource location = this.cacheProperties
|
||||
.resolveConfigLocation(this.cacheProperties.getInfinispan().getConfig());
|
||||
if (location != null) {
|
||||
InputStream in = location.getInputStream();
|
||||
try {
|
||||
@@ -89,7 +89,7 @@ public class InfinispanCacheConfiguration {
|
||||
|
||||
private org.infinispan.configuration.cache.Configuration getDefaultCacheConfiguration() {
|
||||
if (this.defaultConfigurationBuilder != null) {
|
||||
return defaultConfigurationBuilder.build();
|
||||
return this.defaultConfigurationBuilder.build();
|
||||
}
|
||||
return new ConfigurationBuilder().build();
|
||||
}
|
||||
|
||||
@@ -91,8 +91,8 @@ class JCacheCacheConfiguration {
|
||||
private CacheManager createCacheManager() throws IOException {
|
||||
CachingProvider cachingProvider = getCachingProvider(this.cacheProperties
|
||||
.getJcache().getProvider());
|
||||
Resource configLocation = this.cacheProperties.resolveConfigLocation(
|
||||
this.cacheProperties.getJcache().getConfig());
|
||||
Resource configLocation = this.cacheProperties
|
||||
.resolveConfigLocation(this.cacheProperties.getJcache().getConfig());
|
||||
if (configLocation != null) {
|
||||
return cachingProvider.getCacheManager(configLocation.getURI(),
|
||||
cachingProvider.getDefaultClassLoader(),
|
||||
@@ -135,8 +135,8 @@ class JCacheCacheConfiguration {
|
||||
|
||||
/**
|
||||
* Determine if JCache is available. This either kicks in if a provider is available
|
||||
* as defined per {@link JCacheProviderAvailableCondition} or if a {@link CacheManager}
|
||||
* has already been defined.
|
||||
* as defined per {@link JCacheProviderAvailableCondition} or if a
|
||||
* {@link CacheManager} has already been defined.
|
||||
*/
|
||||
@Order(Ordered.LOWEST_PRECEDENCE)
|
||||
static class JCacheAvailableCondition extends AnyNestedCondition {
|
||||
@@ -146,10 +146,12 @@ class JCacheCacheConfiguration {
|
||||
}
|
||||
|
||||
@Conditional(JCacheProviderAvailableCondition.class)
|
||||
static class JCacheProvider {}
|
||||
static class JCacheProvider {
|
||||
}
|
||||
|
||||
@ConditionalOnSingleCandidate(CacheManager.class)
|
||||
static class CustomJCacheCacheManager {}
|
||||
static class CustomJCacheCacheManager {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -19,16 +19,14 @@ package org.springframework.boot.autoconfigure.cache;
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
|
||||
import javax.cache.configuration.CompleteConfiguration;
|
||||
import javax.cache.configuration.MutableConfiguration;
|
||||
import javax.cache.expiry.CreatedExpiryPolicy;
|
||||
import javax.cache.expiry.Duration;
|
||||
|
||||
import com.google.common.cache.CacheBuilder;
|
||||
import com.hazelcast.cache.HazelcastCachingProvider;
|
||||
import com.hazelcast.core.HazelcastInstance;
|
||||
import com.hazelcast.spring.cache.HazelcastCacheManager;
|
||||
import net.sf.ehcache.Status;
|
||||
|
||||
import org.infinispan.configuration.cache.ConfigurationBuilder;
|
||||
import org.infinispan.jcache.embedded.JCachingProvider;
|
||||
import org.infinispan.spring.provider.SpringEmbeddedCacheManager;
|
||||
@@ -36,7 +34,6 @@ import org.junit.After;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import org.springframework.beans.DirectFieldAccessor;
|
||||
import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
@@ -65,6 +62,11 @@ import org.springframework.core.io.Resource;
|
||||
import org.springframework.data.redis.cache.RedisCacheManager;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
|
||||
import com.google.common.cache.CacheBuilder;
|
||||
import com.hazelcast.cache.HazelcastCachingProvider;
|
||||
import com.hazelcast.core.HazelcastInstance;
|
||||
import com.hazelcast.spring.cache.HazelcastCacheManager;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.instanceOf;
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
@@ -257,7 +259,8 @@ public class CacheAutoConfigurationTests {
|
||||
public void jCacheCacheWithExistingJCacheManager() {
|
||||
load(JCacheCustomCacheManager.class, "spring.cache.type=jcache");
|
||||
JCacheCacheManager cacheManager = validateCacheManager(JCacheCacheManager.class);
|
||||
assertThat(cacheManager.getCacheManager(), is(this.context.getBean("customJCacheCacheManager")));
|
||||
assertThat(cacheManager.getCacheManager(),
|
||||
is(this.context.getBean("customJCacheCacheManager")));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -318,7 +321,8 @@ public class CacheAutoConfigurationTests {
|
||||
public void ehCacheCacheWithExistingCacheManager() {
|
||||
load(EhCacheCustomCacheManager.class, "spring.cache.type=ehcache");
|
||||
EhCacheCacheManager cacheManager = validateCacheManager(EhCacheCacheManager.class);
|
||||
assertThat(cacheManager.getCacheManager(), is(this.context.getBean("customEhCacheCacheManager")));
|
||||
assertThat(cacheManager.getCacheManager(),
|
||||
is(this.context.getBean("customEhCacheCacheManager")));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -330,12 +334,14 @@ public class CacheAutoConfigurationTests {
|
||||
assertThat(cacheManager.getCacheNames(), containsInAnyOrder("defaultCache"));
|
||||
assertThat(cacheManager.getCacheNames(), hasSize(1));
|
||||
assertThat(this.context.getBean(HazelcastInstance.class),
|
||||
is(new DirectFieldAccessor(cacheManager).getPropertyValue("hazelcastInstance")));
|
||||
is(new DirectFieldAccessor(cacheManager)
|
||||
.getPropertyValue("hazelcastInstance")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hazelcastCacheWithConfig() {
|
||||
load(DefaultCacheConfiguration.class, "spring.cache.type=hazelcast",
|
||||
load(DefaultCacheConfiguration.class,
|
||||
"spring.cache.type=hazelcast",
|
||||
"spring.cache.hazelcast.config=org/springframework/boot/autoconfigure/cache/hazelcast-specific.xml");
|
||||
HazelcastCacheManager cacheManager = validateCacheManager(HazelcastCacheManager.class);
|
||||
cacheManager.getCache("foobar");
|
||||
@@ -355,7 +361,9 @@ public class CacheAutoConfigurationTests {
|
||||
public void hazelcastCacheWithExistingHazelcastInstance() {
|
||||
load(HazelcastCustomHazelcastInstance.class, "spring.cache.type=hazelcast");
|
||||
HazelcastCacheManager cacheManager = validateCacheManager(HazelcastCacheManager.class);
|
||||
assertThat(new DirectFieldAccessor(cacheManager).getPropertyValue("hazelcastInstance"),
|
||||
assertThat(
|
||||
new DirectFieldAccessor(cacheManager)
|
||||
.getPropertyValue("hazelcastInstance"),
|
||||
is(this.context.getBean("customHazelcastInstance")));
|
||||
}
|
||||
|
||||
@@ -380,8 +388,7 @@ public class CacheAutoConfigurationTests {
|
||||
JCacheCacheManager cacheManager = validateCacheManager(JCacheCacheManager.class);
|
||||
|
||||
Resource configResource = new ClassPathResource(configLocation);
|
||||
assertThat(cacheManager.getCacheManager().getURI(),
|
||||
is(configResource.getURI()));
|
||||
assertThat(cacheManager.getCacheManager().getURI(), is(configResource.getURI()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -435,8 +442,7 @@ public class CacheAutoConfigurationTests {
|
||||
JCacheCacheManager cacheManager = validateCacheManager(JCacheCacheManager.class);
|
||||
|
||||
Resource configResource = new ClassPathResource(configLocation);
|
||||
assertThat(cacheManager.getCacheManager().getURI(),
|
||||
is(configResource.getURI()));
|
||||
assertThat(cacheManager.getCacheManager().getURI(), is(configResource.getURI()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -562,7 +568,8 @@ public class CacheAutoConfigurationTests {
|
||||
@Bean
|
||||
public javax.cache.CacheManager customJCacheCacheManager() {
|
||||
javax.cache.CacheManager cacheManager = mock(javax.cache.CacheManager.class);
|
||||
when(cacheManager.getCacheNames()).thenReturn(Collections.<String>emptyList());
|
||||
when(cacheManager.getCacheNames()).thenReturn(
|
||||
Collections.<String> emptyList());
|
||||
return cacheManager;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user