Ehcache设置为永恒,但无论如何都会忘记元素? [英] Ehcache set to eternal but forgets elements anyway?

查看:132
本文介绍了Ehcache设置为永恒,但无论如何都会忘记元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试配置Ehcache(版本2.5),以便它永远不会忘记项目。我正在以编程方式配置,我没有触及任何配置XML文件。通过将永久设置为 true ,我理解可以从缓存中删除项目的唯一情况是如果我的磁盘空间不足或超过 maxBytesLocalDisk (或者如果应用程序终止)。但是,此测试程序未显示该行为:

I'm trying to configure Ehcache (version 2.5) such that it never forgets items. I'm configuring programmatically and I haven't touched any of the configuration XML files. By setting eternal to true it is my understanding that the only circumstances under which an item could be removed from the cache would be if I run out of disk space or exceed maxBytesLocalDisk (or if the application terminates). However, this test program does not show that behavior:

public static void main(String[] args) {
  CacheManager cacheManager = CacheManager.create(); 
  Cache cache = new Cache(
    new CacheConfiguration().name("test")
    .overflowToDisk(true)
    .eternal(true)
    .maxBytesLocalHeap(1, MemoryUnit.MEGABYTES)
    .overflowToOffHeap(false)
    .maxBytesLocalDisk(100, MemoryUnit.GIGABYTES)
    .maxElementsOnDisk(0)
    .timeToIdleSeconds(0)
    .timeToLiveSeconds(0)
    .diskStorePath("E:\\Data\\Ehcache"));
  cacheManager.addCache(cache);
  for(int i = 0; i < 1000000; i++){
    cache.put(new Element("key_" + i, "value_" + i));
  }
  System.out.println(cache.getSize());      
}

所以在我的缓存中添加了100万个元素后,我告诉它溢出到一个数量级足够大的磁盘,我最后只得到了3276个项目。这里发生了什么?

So after adding 1 million elements to my cache, which I told to overflow to a disk that is large enough by orders of magnitude, I only end up with 3276 items at the end. What is happening here?

推荐答案

当使用ARC或基于字节的缓存配置时,Ehcache将尝试保护您的OOME系统。像你一样配置缓存,告诉ehcache你希望这个缓存最多使用1兆字节的堆。当堆填充到阈值时,溢出到磁盘会告诉Ehcache将元素溢出到磁盘。现在,此缓存的密钥集仍将保留在堆上。并且,由于Ehcache仍然试图保护您免受OOME的攻击,因此只要密钥集无法再保存在内存中,就需要从磁盘中逐出。

When using ARC, or byte based cache configuration, Ehcache will try to protect your system of an OOME. Configuring the cache as you did, tells ehcache that you want this cache to use at most 1 megabyte of heap. Overflowing to disk tells Ehcache to overflow elements to disk when the heap is filled to the threshold. Now, the key set for this cache will still remain on heap. And, as Ehcache still tries to protect you from OOME, it will need to evict from disk, as soon as the key set can't be held in memory anymore.

我稍微改变了你的配置使用10MB,我可以在Cache中获得32K条目。如果我将密钥更改为更小(只有Integer实例),我可以在Cache中获得46K条目。但基本上,你使用的这种配置是限制性的,因为Ehcache永远不能在磁盘上保持那么多,密钥设置在堆上。希望这有点澄清。

I slightly changed your config to use 10MB, I can get 32K entries in the Cache. If I change your key to be smaller (only the Integer instance), I can get 46K entries in the Cache. But basically, this configuration your using is to restrictive, as Ehcache will never be able to hold that much on disk, with the key set on heap. Hope this clarifies a bit.

如果你真的有一个用例需要在磁盘上放很多并最小化堆上存储,你可能想看看 http://ehcache.org/documentation/user-guide/storage-options#enterprise-diskstore

If you really have a use case where you need to put a lot on disk and minimize on-heap storage, you might want to look into http://ehcache.org/documentation/user-guide/storage-options#enterprise-diskstore

这篇关于Ehcache设置为永恒,但无论如何都会忘记元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆