Java – ehcache set to eternal but forget elements anyway?

I am trying to configure ehcache (version 2.5) so that it will never forget the project I'm configuring programmatically and I haven't touched any configuration XML files By setting eternity to true, I understand that if my disk space is insufficient or exceeds maxbyteslocal disk (or if the application terminates), the only case where items can be deleted from the cache However, this test procedure does not show this 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());      
}

Therefore, after adding 1 million elements to my cache, I told it to overflow to a disk large enough by order of magnitude, and finally I got only 3276 items What happened here?

Solution

When using arc or byte based cache configuration, ehcache will try to protect your oome system Configure the cache as you do and tell ehcache that you want the cache to use a maximum of 1 Megabyte heap When the heap is filled to the threshold, overflow to disk will tell ehcache to overflow elements to disk The cached keyset will now remain on the heap Moreover, since ehcache is still trying to protect you from oome attacks, it needs to be evicted from disk as long as the keyset can no longer be saved in memory

I slightly changed your configuration to use 10MB. I can get 32K entries in the cache If I change the key to smaller (only integer instance), I can get 46K entries in the cache But basically, your use of this configuration is restrictive, because ehcache can never keep so much on disk, and the key is set on the heap I hope this is a little clarified

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

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>