schoeggii
schoeggii

Reputation: 149

ehcache - any solution to access expired element content?

I am using ehcache to cache data, usually 24h expiration time. I want to take element individual actions at the time an element expires. Therefore i neee the element content. I registered a CacheEventListener in order to get a notification (notifyElementExpired) in case of element expiration. Unfortunately at notification time only the key is known - content is already discarded, which is kind of painful!

Any solution to access element content at expiration time?

Upvotes: 1

Views: 785

Answers (2)

mindas
mindas

Reputation: 26713

Look at my answer to a similar question. You could stick in some code inside isExpired method and act accordingly if the result is true.

And yes, this is probably not the cleanest approach but it should work.

Upvotes: 0

hoaz
hoaz

Reputation: 10163

You can create your custom eviction Policy that delegates all calls to the actual policy you use. In your implementation you need to override selectedBasedOnPolicy method, where you can specify your action:

class MyEvictionPolicy extends LruPolicy { // you can subclass FifoPolicy or LfuPolicy here
    @Override
    public Element selectedBasedOnPolicy(Element[] sampledElements, Element justAdded) {
        Element candidate = super.selectedBasedOnPolicy(sampledElements, justAdded);
        if (candidate.isExpired()) {
            // perform your action here
        }
        return candidate;
    }
}

Upvotes: 1

Related Questions