Will Guava cache retry cache load with another thread when cache load fails?

Viewed 20

If I got it right, threads that call get(key) will be blocked until first thread finishes cache load. But what will happen if it fails to load cache? (exception thrown for example). Will another thread that call get(key) retry cache load?

1 Answers

Yes, all calls to the loading cache either fetch the stored value or try to block and fetch the value from the downstream source from a CacheLoader.

From the Wiki page (emphasis mine):

A LoadingCache is a Cache built with an attached CacheLoader. (...) The canonical way to query a LoadingCache is with the method get(K). This will either return an already cached value, or else use the cache's CacheLoader to atomically load a new value into the cache.

Moreover:

Because CacheLoader might throw an Exception, LoadingCache.get(K) throws ExecutionException. (...) You can also choose to use getUnchecked(K), which wraps all exceptions in UncheckedExecutionException, but this may lead to surprising behavior if the underlying CacheLoader would normally throw checked exceptions.

Related