Question

Is it possible to configure or extend ehCache to satisfy the following requirements?

  1. Cache an element with a time to live.
  2. When an element is requested from the cache and the time to live has been exceeded, attempt to refresh that value, however if the look up fails use the previous value

The first of these is fairly obvious, but I don't see a way of satisfying the second condition.

Was it helpful?

Solution

Not without overriding Cache.searchInStoreWith/WithoutStats methods. Thing is that currently it's implemented the way that firstly element is evicted from underlying Store and only then, if it's configured, CacheListener is called and even then only the key is passed, not the value.
Of course to it would be possible to configure CacheWriter and not to evict expired elements(and even update them), but without overriding Chache.get your call would still return null. So it's kind of possible to hack around and get hold of expired element
Though to me it seems that it would be easy to change implementation so that expired element wouldn't be evicted, but instead CacheLoaded could be invoked. I'm planning on doing just that asynchronously since I'm better off with stale data than waiting too long for response from SOR or having no data at all if SOR wouldn't be reachable.
Seems like something similar is implemented to comply with JSR 107, but it does not distinguish between non existent and expired elements and if look up fails - expired element is gone.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top