LoadingCache with async loading
caching, guava, java
Solution
The Caffeine library is a Java 8 rewrite of Guava's cache that allows asynchronous automatic loading of entries into a cache, returning CompletableFutures. It is written by people who were directly involved in creating the Guava cache, and uses a Guava-inspired API (including an adapter to Guava's interfaces).
According to posts on the Guava mailing list, it is based on the original proposal for the Guava caching library, and includes changes that were originally intended for Guava itself, but were not included for various reasons (including Guava's need to be compatible with older versions of Java).
In fact, some projects now consider the Guava cache deprecated and use Caffeine instead, e.g. Spring has switched to Caffeine and the author states that "Caffeine is the Java 8 successor to ConcurrentLinkedHashMap and Guava's cache. Projects should prefer Caffeine and migrate when requiring JDK8 or higher."
Problem
In guava, when using LoadingCache CacheLoader is called synchronously. However, my `load()` operation may take too long (~1 sec), I want to take a default action in case it takes too long (>200 ms) and load the value asynchronously. Is there a way to achieve this? Or are there any other approaches you can recommend?