Returns the value associated with
key in this cache, obtaining that value from
loader if necessary. The method improves upon the conventional "if cached, return; otherwise
create, cache and return" pattern. For further improvements, use
LoadingCache and its
LoadingCache#get(Object) method instead of this one.
Among the improvements that this method and
LoadingCache.get(K) both provide are:
-
LoadingCache#get(Object) rather than
starting a redundant one
- eliminating the error-prone caching boilerplate
- tracking load
#stats
Among the further improvements that
LoadingCache can provide but this method cannot:
- consolidation of the loader logic to
CacheBuilder#build(CacheLoader)
-
LoadingCache#refresh, including
CacheBuilder#refreshAfterWrite
-
LoadingCache#getAll, including
CacheLoader#loadAll
Warning: For any given key, every
loader used with it should compute the same
value. Otherwise, a call that passes one
loader may return the result of another call
with a differently behaving
loader. For example, a call that requests a short timeout
for an RPC may wait for a similar call that requests a long timeout, or a call by an
unprivileged user may return a resource accessible only to a privileged user making a similar
call. To prevent this problem, create a key object that includes all values that affect the
result of the query. Or use
LoadingCache.get(K), which lacks the ability to refer to
state other than that in the key.
Warning: as with
CacheLoader#load,
loader must not return
null; it may either return a non-null value or throw an exception.
No observable state associated with this cache is modified until loading completes.