我尝试使用JHCache(JSR 107)的EHCache实现,我在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">
<!-- see http://www.ehcache.org/ehcache.xml -->
<defaultCache
maxEntriesLocalHeap="200000"
eternal="false"
timeToIdleSeconds="1200"
timeToLiveSeconds="1200">
<persistence strategy="localTempSwap"/>
</defaultCache>
</ehcache>
以下是我创建缓存的方法:
String providerName = "org.ehcache.jcache.JCacheCachingProvider";
CachingProvider cachingProvider = Caching.getCachingProvider(providerName);
CacheManager cacheManager = cachingProvider.getCacheManager();
CompleteConfiguration<ComposedKey, Object> config = new MutableConfiguration<ComposedKey, Object>()
.setTypes(ComposedKey.class, Object.class)
.setExpiryPolicyFactory(FactoryBuilder.factoryOf(new AccessedExpiryPolicy(new Duration(TimeUnit.MINUTES, 30))))
.setReadThrough(false)
.setWriteThrough(false)
.addCacheEntryListenerConfiguration(
new MutableCacheEntryListenerConfiguration<ComposedKey, Object>(new CacheListenerManagerJSR107Factory(), null, true,
true));
;
Cache<ComposedKey, Object> cache = cacheManager.createCache("cache", config);
但似乎创建的缓存不使用提供的配置(即在ehcache.xml
下),因为这里的调试是ehcache实例的属性:
[ name = orACJ2rF-1 status = STATUS_ALIVE eternal = false overflowToDisk = false maxEntriesLocalHeap = 1000 maxEntriesLocalDisk = 0 memoryStoreEvictionPolicy = LRU timeToLiveSeconds = 0 timeToIdleSeconds = 0 persistence = none diskExpiryThreadIntervalSeconds = 120 cacheEventListeners: org.ehcache.jcache.JCacheListenerAdapter ; orderedCacheEventListeners: maxBytesLocalHeap = 0 overflowToOffHeap = false maxBytesLocalOffHeap = 0 maxBytesLocalDisk = 0 pinned = false ]