I read this document explaining Guava Cache: CachesExplained. I do understand what refreshAfterWrite and expireAfterWrite are doing. However, while explaining refreshAfterWrite, the document also mentions this:
"So, for example, you can specify both refreshAfterWrite and expireAfterWrite on the same cache, so that the expiration timer on an entry isn't blindly reset whenever an entry becomes eligible for a refresh, so if an entry isn't queried after it comes eligible for refreshing, it is allowed to expire."
This is the part that confuses me. For my understanding, if you want a key to be automatically refreshed, you only need to specify refreshAfterWrite. Why do we even want to use expireAfterWrite when ever refreshAfterWrite is used?
There are scenarios in which you'll want the cached entries to be relevant so you set a refresh duration (which might be lighter to perform (and async), rather then a full fetch after eviction and hence, different), but at the same time, if your cache is bounded, you'll want to evict entries, that's what the
expireAfterWrite
is for. By setting them both, you'll make sure that a entry is evicted after a certain time, even if it was refreshed.Also note that both are differs in the way they are operate: