I'm just a starter with Caffeine cache and needs clarification for few use cases.
asyncCache.synchronous().invalidate(key) //supported for synchronous cache
asyncCache.invalidate(key)// not supported for async cache
Can someone please elaborate on this?
Since the method is available in the synchronous()
view, it would only be a convenience default method making that same call. Since invalidate
does not return a value, it does not need to block after the future was removed from the underlying Map
. If a blocking removal was desired, that is available through synchronous().asMap().remove(key)
. It didn't seem to carry its weight.
An AsyncCache
stores a CompletableFuture
as the entry's value. A put
of a future only blocks long enough to perform the ConcurrentHashMap
operation, allowing the future to be resolved independently. If the future completes as an error or null result, then a whenComplete
handler removes it from the cache. This way establishing the Map entry is synchronous, but loading the future's value is asynchronous.
Caffeine can be thought of as a bounded Map with convenience APIs to orient towards caching problems. It should behave how you might expect when using ConcurrentHashMap
explicitly instead. Therefore, Cache<K, V>
~= Map<K, V>
and AsyncCache<K, V>
~= Map<K, CompletableFuture<V>>
. When the cache apis are too restrictive then using an asMap
view provides all of the functionality the cache interface implementation classes use.