Is there any third party java cache which provides control over the expiration event?

My use-case is that I need to implement a cache on top of a service should expire entries after a certain amount of time (from their time of creation).

And if the entry is getting expired, then service look up should be done to get the latest entry. lets call is service refresh.

But, lets say if service refresh fails, then I should be able to use the stale data in the cache.

But since the cache is already expired, I don't have that entry.

So, I am thinking of controlling the expiration of the cache and cache entry would only be expired only if service is available to get the latest data, otherwise don't remove that entry.

I was looking into Google Guava cache, but it only provides a removalListener which would just notify me with the event but I am not able to control the expiration event with this.

Is there any third party cache implementation which can serve my purpose?

Upvotes: 1

Views: 128

Answers (1)

cruftex
cruftex

Reputation: 5723

This kind of resilience and robustness semantics are implemented in cache2k. We use this in production for quite some time. The setup looks like

CacheBuilder.newCache(Key.class, Value.class)
  .name("myCache")
  .source(new YourSourceImplementation())
  .backgroundRefresh(true)
  .suppressExceptions(true)
  .expiryDuration(60, TimeUnit.SECONDS)
  .build();

With exceptionExpiryDuration you can actually set a shorter interval for the retry. There was a similar question on SO, which I also answered, see: Is it possible to configure Guava Cache (or other library) behaviour to be: If time to reload, return previous entry, reload in background (see specs) There you find some more details about it.

Regardless what cache you use, you will run into a lot of issues, since exception handling in general and building robust and resilient applications needs some thoughts in the details.

That said, I am not totally happy with the solution yet, since I think we need more control, e.g. how long stale data should be served. Furthermore, the cache needs to have some alerting if there is stale data in it. I put some thoughts on how to improve this here: https://github.com/cache2k/cache2k/issues/26

Feedback is very welcome.

Upvotes: 1

Related Questions