Is ehcache set up for eternity but forget about the elements?

I am trying to configure Ehcache (version 2.5) so that it never forgets the elements. I am programming programmatically and I have not touched any XML configuration files. By setting eternal to true , I understand that the only circumstances that can cause an item to be removed from the cache will be if I run out of disk space or exceed maxBytesLocalDisk (or if the application terminates). However, this test program 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()); } 

So, after adding 1 million elements to my cache, which, as I said, is full on a disk that is quite large by orders of magnitude, I only end up with 3276 points at the end. What's going on here?

+7
source share
1 answer

When using ARC or a byte-based cache configuration, Ehcache will attempt to protect your OOME system. Setting the cache, like you, tells ehcache that you want this cache to use no more than 1 megabyte of heap. Overflow to disk tells Ehcache to overflow elements to disk when the heap is full. Now the key set for this cache will still remain on the heap. And, since Ehcache is still trying to protect you from OOME, it will need to be evicted from disk as soon as the key set can no longer be stored in memory.

I changed your configuration a bit to use 10MB, I can get 32K of cache entries. If I changed your key to a smaller one (only an Integer instance), I can get 46K cache entries. But basically, this configuration you use is restrictive, since Ehcache will never be able to keep this on disk, with the key installed in the heap. Hope this clarifies a bit.

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

+6
source

All Articles