【发布时间】:2020-02-10 08:44:14
【问题描述】:
我一直在寻找如何使用 Spring Boot 2 实现 EhCache3 的解决方案。问题是在版本 3 中,他们将包更改为 org.ehcache,我发现的无 xml 配置示例适用于您声明 net.xml 的 2 版本。 sf.ehcache.config。我想使用第三个版本。
package com.jcg.example.ehcache_no_xml.config;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import net.sf.ehcache.Cache;
import net.sf.ehcache.config.CacheConfiguration;
@Configuration
public class EhCacheConfig {
@Bean
public EhCacheManagerFactoryBean cacheManager() {
return new EhCacheManagerFactoryBean();
}
@Bean
public EhCacheCacheManager testEhCacheManager() {
// testEhCache Configuration - create configuration of cache that previous required XML
CacheConfiguration testEhCacheConfig = new CacheConfiguration()
.eternal(false) // if true, timeouts are ignored
.timeToIdleSeconds(3) // time since last accessed before item is marked for removal
.timeToLiveSeconds(5) // time since inserted before item is marked for removal
.maxEntriesLocalHeap(10) // total items that can be stored in cache
.memoryStoreEvictionPolicy("LRU") // eviction policy for when items exceed cache. LRU = Least Recently Used
.name("testCache");
Cache testCache = new Cache(testEhCacheConfig);
cacheManager().getObject().addCache(testCache);
return new EhCacheCacheManager(cacheManager().getObject());
}
}
有什么解决方案可以用 spring 来创建 ehCache 配置吗?
【问题讨论】:
标签: spring spring-boot ehcache-3