我已经开始使用ehcache进行缓存 .
我想要一个缓存,它将使用磁盘,以防它没有足够的内存空间 .
我写了这段代码:
CacheManager manager = CacheManager.getInstance();
CacheConfiguration config = new CacheConfiguration()
.name("mycache")
.maxBytesLocalHeap(5,MemoryUnit.MEGABYTES)
.memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LFU) //least frequntly used
.eternal(false) //should expire
.timeToLiveSeconds(43200)//12 hours
.timeToIdleSeconds(0)
.diskExpiryThreadIntervalSeconds(300)
.maxEntriesLocalDisk(0)//unlimited
.persistence(new PersistenceConfiguration().strategy(Strategy.LOCALTEMPSWAP));
cache = new Cache(config);
CacheWrapperEventListener listener = new CacheWrapperEventListener(name);
cache.getCacheEventNotificationService().registerListener(listener);
manager.addCache(cache);
现在我开始在特定时间添加项目到缓存(我想在达到内存限制大小后)我进入我的监听器notifyElementEvicted事件
- notifyElementEvicted [name = mycache status = STATUS_ALIVE eternal = false overflowToDisk = true maxEntriesLocalHeap = 0 maxEntriesLocalDisk = 0 memoryStoreEvictionPolicy = LFU timeToLiveSeconds = 43200 timeToIdleSeconds = 0 persistence = LOCALTEMPSWAP diskExpiryThreadIntervalSeconds = 300 cacheEventListeners:com.jobsin.server.infra.cache.CacheWrapperEventListener; orderedCacheEventListeners:maxBytesLocalHeap = 5242880 overflowToOffHeap = false maxBytesLocalOffHeap = 0 maxBytesLocalDisk = 0 pinned = false]
根据我在文档中读到的内容,此事件应将元素假脱机到磁盘,但是当我尝试从缓存中读取此项时,我将返回null .
我的配置中缺少什么,以便在我的缓存堆内存已满时能够使用缓存?
1 回答
鉴于对该问题的评论提供了答案,最可能的解释是您正在进行驱逐,因为将条目写入磁盘的队列达到其最大容量 .
当发生这种情况时,没有进入队列的条目被丢弃在地板上 - 被放逐内联发生的内联 . 快速检查是为了确保您不要将条目放在紧密的循环中进行测试 .
有关详细信息,请参阅EHC-1059 .