有没有办法在Spring 4或Spring Boot中初始化没有xml的EhCache?
我注意到Spring Boot 1.0.0.RC3没有任何ehcache依赖,但Spring 4.0GA发布文章提到它改进了对EhCache的支持.此外,Spring 3有类,org.springframework.cache.ehcache.EhCacheCacheManager但它不再是依赖项的一部分.
编辑: Spring 4确实有EhCache支持.您必须添加依赖项:
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
Run Code Online (Sandbox Code Playgroud)
编辑2: 我尝试了以下内容,我认为我很接近但是我收到了一个错误:
@Bean
@Override
public CacheManager cacheManager() {
CacheConfiguration cacheConfiguration = new CacheConfiguration();
cacheConfiguration.setName("primary");
cacheConfiguration.setMemoryStoreEvictionPolicy("LRU");
cacheConfiguration.setMaxEntriesLocalHeap(0);
net.sf.ehcache.config.Configuration config = new net.sf.ehcache.config.Configuration();
config.addCache(cacheConfiguration);
net.sf.ehcache.CacheManager cacheManager = new net.sf.ehcache.CacheManager(config);
cacheManager.setName("EhCache");
return new EhCacheCacheManager(cacheManager);
}
@Bean
public EhCacheManagerFactoryBean factoryBean() {
return new EhCacheManagerFactoryBean();
}
Run Code Online (Sandbox Code Playgroud)
错误
Caused by: net.sf.ehcache.CacheException: Another unnamed CacheManager already exists in the same VM. Please provide unique names for each CacheManager in the …Run Code Online (Sandbox Code Playgroud) 我有一个ehcache设置,除了每次重新启动我的应用程序/服务器(TcServer/Tomcat上的Spring应用程序)时都删除了持久性磁盘存储数据,它的工作正常.使用持久性磁盘库的重点是,尽管应用程序重新启动,仍能保留缓存.
这是我的ehcache.xml
<?xml version="1.0" encoding="UTF-8"?><ehcache><diskStore path="java.io.tmpdir/ehcache"/><cache name="clusterCache"
maxElementsInMemory="1"
maxElementsOnDisk="50"
eternal="true"
overflowToDisk="true"
diskPersistent="true"
memoryStoreEvictionPolicy="LFU"/></ehcache>
Run Code Online (Sandbox Code Playgroud)
任何想法为什么会这样?