javaasynchronouscaffeine

Manual load and removal of entries with AsyncCache in Caffeine


I'm just a starter with Caffeine cache and needs clarification for few use cases.

  1. I see Caffeine supports invalidate/invalidateAll function only for Synchronous cache. I would like to know why invalidate function is not supported for AsyncCache?
asyncCache.synchronous().invalidate(key) //supported for synchronous cache                                    
asyncCache.invalidate(key)// not supported for async cache
  1. Also wanted to clarify whether the put method in AsyncCache, loads entry into the cache Asynchronously? I wanted to manually add the list of entries into the AsyncCache. Not sure either asyncCache.asMap().putAll() or iterating and adding the entries using asyncCache.put() does the job asynchronously?

Can someone please elaborate on this?


Solution

    1. 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.

    2. 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.