Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ehcache set to eternal but forgets elements anyway?

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());      
}

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?

like image 925
Michael McGowan Avatar asked Jan 22 '12 05:01

Michael McGowan


People also ask

Is Ehcache threadsafe?

It is inherently not thread-safe to modify the value. It is safer to retrieve a value, delete the cache element, and then reinsert the value.

Is Ehcache persistent?

Open-source Ehcache offers a limited version of persistence, as noted in this document. Fast Restart provides enterprise-ready crash resilience with an option to store a fully consistent copy of the cache on the local disk at all times.

What is timeToIdleSeconds in Ehcache?

timeToIdleSeconds – The maximum number of seconds an element can exist in the cache without being accessed. The element expires at this limit and will no longer be returned from the cache. The default value is 0, which means no TTI eviction takes place (infinite lifetime).

How to disable Ehcache?

sf. ehcache. disabled=true in the Java command line) disables caching in ehcache. If disabled, no elements can be added to a cache (puts are silently discarded).


1 Answers

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.

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.

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

like image 105
Alex Snaps Avatar answered Oct 25 '22 00:10

Alex Snaps