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?
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?
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
LoadingCacheis aCachebuilt with an attachedCacheLoader. (...) The canonical way to query aLoadingCacheis with the methodget(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
CacheLoadermight throw anException,LoadingCache.get(K)throwsExecutionException. (...) You can also choose to usegetUnchecked(K), which wraps all exceptions inUncheckedExecutionException, but this may lead to surprising behavior if the underlyingCacheLoaderwould normally throw checked exceptions.