0
votes

I have started using ehcache for caching purpose.

I would like a cache that will use the disk in case it does not have enough memory space.

I have wrote this code:

    CacheManager manager = CacheManager.getInstance();
    CacheConfiguration config =  new CacheConfiguration() 
    .name("mycache")
    .maxBytesLocalHeap(5,MemoryUnit.MEGABYTES)
    .memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LFU) //least frequntly used
    .eternal(false) //should expire
    .timeToLiveSeconds(43200)//12 hours
    .timeToIdleSeconds(0)  
    .diskExpiryThreadIntervalSeconds(300)   
    .maxEntriesLocalDisk(0)//unlimited
    .persistence(new PersistenceConfiguration().strategy(Strategy.LOCALTEMPSWAP));
     cache = new Cache(config);
     CacheWrapperEventListener listener =  new CacheWrapperEventListener(name);
     cache.getCacheEventNotificationService().registerListener(listener); 
     manager.addCache(cache);

Now I'm starting to add items to cache, at certain time ( I guess after reaching the memory limit size) i'm getting in my listener notifyElementEvicted event

*notifyElementEvicted [ name = mycache status = STATUS_ALIVE eternal = false overflowToDisk = true maxEntriesLocalHeap = 0 maxEntriesLocalDisk = 0 memoryStoreEvictionPolicy = LFU timeToLiveSeconds = 43200 timeToIdleSeconds = 0 persistence = LOCALTEMPSWAP diskExpiryThreadIntervalSeconds = 300 cacheEventListeners: com.jobsin.server.infra.cache.CacheWrapperEventListener ; orderedCacheEventListeners: maxBytesLocalHeap = 5242880 overflowToOffHeap = false maxBytesLocalOffHeap = 0 maxBytesLocalDisk = 0 pinned = false ]

According to what I have read in the documents this event should spool the element to disk, however when I'm trying afterward to read this item from the cache I'm getting back null.

What am I missing here in my configuration in order to be able to use the cache when my cache heap memory is full?

1
Ehcache has had different patterns for splitting entries between disk and memory across its versions. Can you indicate which version you are using? - Louis Jacomet
I have downloaded the latest version 2.9.0, but i faced the same problem also with 2.8.0 - user2022561
In these versions of Ehcache, all entries will be present on disk, and frequently accessed entries will also be present in memory. Can you test by looking at statistics what happens after you put one entry to the cache? And potentially indicate if the issue you are seeing is in test code (a tight loop that fills the cache) or under application use (cache access as part of business code executing) - Louis Jacomet
This is in my test environment simulating application use. I see the data is saved to disk since in the statistics i get getLocalDiskSizeInBytes() > 0 . The question is why after some element is being evicted from heap memory (and now should only on disk) i cannot get it any more and get from cache is null. Also strange i'm not getting any notifyElementRemoved event on this item - user2022561

1 Answers

0
votes

Given the answers provided to comments on the question, the most likely explanation is that you are getting evictions because the queue writing entries to disk reaches its maximum capacity.

When that happens, entries that do not make it into the queue are dropped on the floor - being evicted inline of the put happening. A quick check is to make sure you do not put entries in a tight loop for testing.

See EHC-1059 for details.