Top Description Methods
com.github.benmanes.caffeine.cache

public Interface AsyncLoadingCache<K, V>

extends AsyncCache<K, V>
Known Direct Implementers
com.github.benmanes.caffeine.cache.LocalAsyncLoadingCache
Type Parameters
<K>
the type of keys maintained by this cache
<V>
the type of mapped values
Imports
java.util.Map, java.util.concurrent.CompletableFuture

A semi-persistent mapping from keys to values. Values are automatically loaded by the cache asynchronously and are stored in the cache until either evicted or manually invalidated.

Implementations of this interface are expected to be thread-safe and can be safely accessed by multiple concurrent threads.

Author
[email protected] (Ben Manes)

Method Summary

Modifier and TypeMethod and Description
public CompletableFuture<V>

Returns:

the current (existing or computed) future value associated with the specified key
get
(K
the key with which the specified value is to be associated
key
)

Returns the future associated with the key in this cache, obtaining that value from AsyncCacheLoader#asyncLoad if necessary.

public CompletableFuture<Map<K, V>>

Returns:

a future containing an unmodifiable mapping of keys to values for the specified keys in this cache
getAll
(Iterable<? extends K>
the keys whose associated values are to be returned
keys
)

Returns the future of a map of the values associated with keys, creating or retrieving those values if necessary.

public LoadingCache<K, V>

Returns:

a thread-safe synchronous view of this cache
synchronous
()

Redeclares com.github.benmanes.caffeine.cache.AsyncCache.synchronous.

Returns a view of the entries stored in this cache as a synchronous LoadingCache.
Inherited from com.github.benmanes.caffeine.cache.AsyncCache:
asMapgetgetgetAllgetAllgetIfPresentput

Method Detail

getback to summary
public CompletableFuture<V> get(K key)

Returns the future associated with the key in this cache, obtaining that value from AsyncCacheLoader#asyncLoad if necessary. If the asynchronous computation fails, the entry will be automatically removed from this cache.

If the specified key is not already associated with a value, attempts to compute its value asynchronously and enters it into this cache unless null. The entire method invocation is performed atomically, so the function is applied at most once per key.

Parameters
key:K

the key with which the specified value is to be associated

Returns:CompletableFuture<V>

the current (existing or computed) future value associated with the specified key

Exceptions
NullPointerException:
if the specified key is null or if the future returned by the AsyncCacheLoader is null
RuntimeException:
or Error if the AsyncCacheLoader does when constructing the future, in which case the mapping is left unestablished
getAllback to summary
public CompletableFuture<Map<K, V>> getAll(Iterable<? extends K> keys)

Returns the future of a map of the values associated with keys, creating or retrieving those values if necessary. The returned map contains entries that were already cached, combined with newly loaded entries; it will never contain null keys or values. If any of the asynchronous computations fail, those entries will be automatically removed from this cache.

Caches loaded by a AsyncCacheLoader supporting bulk loading will issue a single request to AsyncCacheLoader#asyncLoadAll for all keys that are not already present in the cache. If another call to get tries to load the value for a key in keys, that thread retrieves a future that is completed by this bulk computation. Caches that do not use a AsyncCacheLoader with an optimized bulk load implementation will sequentially load each key by making individual AsyncCacheLoader#asyncLoad calls. Note that multiple threads can concurrently load values for distinct keys.

Note that duplicate elements in keys, as determined by Object#equals, will be ignored.

Parameters
keys:Iterable<? extends K>

the keys whose associated values are to be returned

Returns:CompletableFuture<Map<K, V>>

a future containing an unmodifiable mapping of keys to values for the specified keys in this cache

Exceptions
NullPointerException:
if the specified collection is null or contains a null element, or if the future returned by the AsyncCacheLoader is null
RuntimeException:
or Error if the AsyncCacheLoader does so, if AsyncCacheLoader#asyncLoadAll returns null, or fails when constructing the future, in which case the mapping is left unestablished
synchronousback to summary
public LoadingCache<K, V> synchronous()

Redeclares com.github.benmanes.caffeine.cache.AsyncCache.synchronous.

Returns a view of the entries stored in this cache as a synchronous LoadingCache. A mapping is not present if the value is currently being loaded. Modifications made to the synchronous cache directly affect the asynchronous cache. If a modification is made to a mapping that is currently loading, the operation blocks until the computation completes.

Returns:LoadingCache<K, V>

a thread-safe synchronous view of this cache

Annotations
@Override