Forcing EHCache to remove expired elements from the memory

Ehcache does not automatically remove elements from your cache that have exceeded timeToLive and timeToIdle. These elements remain in the cache until either:
Ehcache#evictExpiredElements() is called
Or a get or remove operation is called on the cache that passes in the same key as the expired element

Ehcache will only evict elements when putting elements and your cache is above threshold. Otherwise, accessing those expired elements will result in them being expired (and removed from the Cache). There is no thread that collects and removes expired elements from the Cache in the background. Even though I wouldn’t recommend it, as this will affect the Cache’s performance

So this is the code which will guarantee removal of cache element from the memory

cache.evictExpiredElements();
cache.flush();

Uday Ogra

Connect with me at http://facebook.com/tendulkarogra and lets have some healthy discussion :)

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *