Search code examples
ehcache

EhCache - remove expired element (because of timeToIdleSeconds or timeToLiveSeconds) without try to get it?


The EhCache documentation says:

  • Accessing an entry in myCache that has been idle for more than an hour (timeToIdleSeconds) causes that element to be evicted.
  • If an entry expires but is not accessed, and no resource constraints force eviction, then the expired entry remains in place.
  • getting an expired element will remove it from the cache and return null.

If you implement and register a CacheEventListener to receive that an element is expired, you can see the event is fired when you try to get an expired element, but not after the 'timeToIdleSeconds'.

Is it possible to force removing the expired element after it is expired? Because in my case after this time no one will try to get it again.

Thank you.


Solution

  • I've done it with the following code:

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