EhCache - delete expired item (due to timeToIdleSeconds or timeToLiveSeconds) without trying to get it?

The EhCache documentation says:

  • Access to an entry in myCache that has been idle for more than an hour (timeToIdleSeconds) causes this item to be deleted.
  • If the record expires, but is not available, and no resource restrictions crowd out, then the expired record remains in place.
  • Getting an expired item will remove it from the cache and return null.

If you implement and register a CacheEventListener to get the item to expire, you can see that the event fires when you try to get the expired item, but not after "timeToIdleSeconds".

Is it possible to forcefully delete an expired item after it has expired? Because in my case, after this time, no one will try to get it again.

Thanks.

+6
source share
2 answers

I did this with the following code:

cache.evictExpiredElements(); cache.flush(); 
+6
source

In version 3 of EhCache and above, you can no longer use cache.evictExpiredElements() , and most likely you shouldn't :)

But if you need, here is my solution:

 final Iterator<Entry<String, Bean>> iterator = cache.iterator(); while (iterator.hasNext()) { iterator.next(); } 

This will make the cache get every element in it, return null for expired elements, and delete them. In addition, I created a CacheEventListener to catch the CacheEventListener event to execute a CacheEventListener then a CacheEventListener .

As mentioned earlier, carefully analyze your use case and make sure there is no better way than brute force.

0
source

Source: https://habr.com/ru/post/953333/


All Articles