views:

295

answers:

1

Is there any to specify a data refresh policy in Ehcache? I am currently migrating an application from OSCache to Ehcache and I can't seem to find any way to specify when an element needs refreshing, besides setting timeToIdle and timeToLive.

What I want is: on accessing an element from the cache, check with it's associated resource to see if it was updated later than the lastUpdateTime of the cache element. If yes, refresh the cache; else serve the content from the cache.

In OSCache this was done by catching NeedsRefreshExceptions and setting custom refresh policies for the elements. I've been digging around in the docs for a while now, but I wasn't able to find any methods or examples of how I could accomplish this in Ehcache.

Any help would be appreciated :).

Alex

+1  A: 

The OSCache's idea of NeedsRefreshException is really flawed. First, this goes against the advice to use exceptions for usual execution flow (and yes, I do consider stale cache hit as a normal flow); secondly creating exception for this is really expensive.

Now back to your question. If I was in your shoes, I would evaluate the possibility either to extend net.sf.ehcache.Element, or wrap your entry value into an object which would be able to check the timeToLive.

But the main question is, why do you need to do that? If you are putting object into the cache and specifying the TTL, why bother checking the TTL on retrieval? The cache should be able to evict the object without any external help. Same applies for cases when you manually remove the entry (e.g. when the object has been changed). Maybe I am missing something here?

The feature you're asking about is called read-through. How to do this is explained here: http://ehcache.org/documentation/concepts.html#read-through

--
another unhappy OSCache user waiting to migrate to EHCache :(

mindas