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?
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