简体   繁体   中英

Ehcache: How to conditionally return stale data

Usually, when an entry in an Ehcache cache exceeds its ttl/tti, it will be marked as stale. Then, when the cache is asked to return this stale entry, the entry will be deleted and the cache will return null. The calling method will have to go out to the datastore to get a fresh entry to store in the cache.

In my use case, if the datastore goes down it would be better to return stale data than to return no data. The problem is, by the time I make the datastore call to check whether it's up, my stale entry has already been deleted.

Is there any way to instruct Ehcache to purposefully return stale data under certain boolean circumstances (ie DatastoreMonitor returns isDown)?

In the end, I implemented my own ConditionallyStaleCache extends Object . The constructor takes three arguments - shortTermCache , backUpCache , and Dao . I myself implemented all the "if the Dao is down then return from backup" logic, because I don't think it's possible for EhCache to do this logic for me.

I'd be curious to know if there is a better approach, but this is what I'm using for now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM