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.
source share