我正在使用 Spring boot (1.4.2.RELEASE) 和 Ehcache (2.4.3)
缓存正在开发环境中使用,但在其他环境(测试和生产)中没有使用(命中)。
代码如下:
pom.xml
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
On 主班,添加了以下注释用于缓存
@EnableCaching
public class Application {
Under src/main/resources,ehcache.xml
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd"
updateCheck="true" monitoring="autodetect" dynamicConfig="true">
<cache name="languageCache"
maxEntriesLocalHeap="20"
overflowToDisk="false"
eternal="false"
diskPersistent="false"
memoryStoreEvictionPolicy="LRU"/>
<cache name="countryCache"
maxEntriesLocalHeap="280"
overflowToDisk="false"
eternal="false"
diskPersistent="false"
memoryStoreEvictionPolicy="LRU"/>
..
..
more entries
</ehcache>
缓存配置文件
@Configuration
@EnableCaching
public class CacheConfig {
@Bean
public CacheManager getEhCacheManager() {
(new EhCacheCacheManager(getEhCacheFactory().getObject())).getCache("languageCache");
return new EhCacheCacheManager(getEhCacheFactory().getObject());
}
@Bean
public EhCacheManagerFactoryBean getEhCacheFactory() {
EhCacheManagerFactoryBean factoryBean = new EhCacheManagerFactoryBean();
factoryBean.setConfigLocation(new ClassPathResource("ehcache.xml"));
factoryBean.setShared(true);
return factoryBean;
}
}
关于上面代码的几个问题:
1)是否是由于这条线
factoryBean.setConfigLocation(new ClassPathResource("ehcache.xml"));
除开发环境外,缓存未在任何其他环境中被命中/使用?
2)我们到底需要CacheConfig文件吗?或者Spring boot将在主类上使用注释(@EnableCaching)检测Ehcache?
有什么建议吗,为什么在其他环境中没有拾取缓存(我缺少一些配置?)?
谢谢