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

public Interface AsyncCache<K, V>

Known Direct Subinterfaces
com.github.benmanes.caffeine.cache.AsyncLoadingCache, com.github.benmanes.caffeine.cache.LocalAsyncCache
Type Parameters
<K>
the type of keys maintained by this cache
<V>
the type of mapped values
Imports
java.util.Map, .Set, java.util.concurrent.CompletableFuture, .ConcurrentMap, .Executor, java.util.function.BiFunction, .Function, org.checkerframework.checker.nullness.qual.Nullable

A semi-persistent mapping from keys to values. Cache entries are manually added using get(Object, Function) or put(Object, CompletableFuture), 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 ConcurrentMap<K, CompletableFuture<V>>

Returns:

a thread-safe view of this cache supporting all of the optional Map operations
asMap
()

Returns a view of the entries stored in this cache as a thread-safe map.

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
,
Function<? super K, ? extends V>
the function to asynchronously compute a value
mappingFunction
)

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

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
,
BiFunction<? super K, ? super Executor, ? extends CompletableFuture<? extends V>>
the function to asynchronously compute a value, optionally using the given executor
mappingFunction
)

Returns the future associated with the key in this cache, obtaining that value from mappingFunction 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
,
Function<? super Set<? extends K>, ? extends Map<? extends K, ? extends V>>
the function to asynchronously compute the values
mappingFunction
)

Returns the future of a map of the values associated with the keys, creating or retrieving those values 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
,
BiFunction<? super Set<? extends K>, ? super Executor, ? extends CompletableFuture<? extends Map<? extends K, ? extends V>>>
the function to asynchronously compute the values, optionally using the given executor
mappingFunction
)

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

public @Nullable CompletableFuture<V>

Returns:

the future value to which the specified key is mapped, or null if this cache does not contain a mapping for the key
getIfPresent
(K
the key whose associated value is to be returned
key
)

Returns the future associated with the key in this cache, or null if there is no cached future for the key.

public void
put(K
the key with which the specified value is to be associated
key
,
CompletableFuture<? extends V>
the value to be associated with the specified key
valueFuture
)

Associates value with key in this cache.

public Cache<K, V>

Returns:

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

Returns a view of the entries stored in this cache as a synchronous Cache.

Method Detail

asMapback to summary
public ConcurrentMap<K, CompletableFuture<V>> asMap()

Returns a view of the entries stored in this cache as a thread-safe map. Modifications made to the map directly affect the cache.

A computation operation, such as ConcurrentMap#compute, performs the entire method invocation atomically, so the function is applied at most once per key. Some attempted update operations by other threads may be blocked while computation is in progress. The computation must not attempt to update any other mappings of this cache.

Iterators from the returned map are at least weakly consistent: they are safe for concurrent use, but if the cache is modified (including by eviction) after the iterator is created, it is undefined which of the changes (if any) will be reflected in that iterator.

Returns:ConcurrentMap<K, CompletableFuture<V>>

a thread-safe view of this cache supporting all of the optional Map operations

getback to summary
public CompletableFuture<V> get(K key, Function<? super K, ? extends V> mappingFunction)

Returns the future associated with the key in this cache, obtaining that value from mappingFunction if necessary. This method provides a simple substitute for the conventional "if cached, return; otherwise create, cache, and return" pattern.

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. If the asynchronous computation fails, the entry will be automatically removed from this cache.

Warning

as with CacheLoader#load, mappingFunction must not attempt to update any other mappings of this cache.

Parameters
key:K

the key with which the specified value is to be associated

mappingFunction:Function<? super K, ? extends V>

the function to asynchronously compute a value

Returns:CompletableFuture<V>

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

Exceptions
NullPointerException:
if the specified key or mappingFunction is null
getback to summary
public CompletableFuture<V> get(K key, BiFunction<? super K, ? super Executor, ? extends CompletableFuture<? extends V>> mappingFunction)

Returns the future associated with the key in this cache, obtaining that value from mappingFunction if necessary. This method provides a simple substitute for the conventional "if cached, return; otherwise create, cache, and return" pattern. The instance returned from the mappingFunction will be stored directly into the 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. If the asynchronous computation fails, the entry will be automatically removed from this cache.

Warning

as with CacheLoader#load, mappingFunction must not attempt to update any other mappings of this cache.

Parameters
key:K

the key with which the specified value is to be associated

mappingFunction:BiFunction<? super K, ? super Executor, ? extends CompletableFuture<? extends V>>

the function to asynchronously compute a value, optionally using the given executor

Returns:CompletableFuture<V>

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

Exceptions
NullPointerException:
if the specified key or mappingFunction is null, or if the future returned by the mappingFunction is null
RuntimeException:
or Error if the mappingFunction 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, Function<? super Set<? extends K>, ? extends Map<? extends K, ? extends V>> mappingFunction)

Returns the future of a map of the values associated with the 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 the any of the asynchronous computations fail, those entries will be automatically removed from this cache.

A single request to the mappingFunction is performed for all keys which 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. Any loaded values for keys that were not specifically requested will not be returned, but will be stored in the cache. 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

mappingFunction:Function<? super Set<? extends K>, ? extends Map<? extends K, ? extends V>>

the function to asynchronously compute the values

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 mappingFunction is null
RuntimeException:
or Error if the mappingFunction does so, in which case the mapping is left unestablished
getAllback to summary
public CompletableFuture<Map<K, V>> getAll(Iterable<? extends K> keys, BiFunction<? super Set<? extends K>, ? super Executor, ? extends CompletableFuture<? extends Map<? extends K, ? extends V>>> mappingFunction)

Returns the future of a map of the values associated with the 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 the any of the asynchronous computations fail, those entries will be automatically removed from this cache. The instances returned from the mappingFunction will be stored directly into the cache.

A single request to the mappingFunction is performed for all keys which 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. Any loaded values for keys that were not specifically requested will not be returned, but will be stored in the cache. 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

mappingFunction:BiFunction<? super Set<? extends K>, ? super Executor, ? extends CompletableFuture<? extends Map<? extends K, ? extends V>>>

the function to asynchronously compute the values, optionally using the given executor

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 mappingFunction is null
RuntimeException:
or Error if the mappingFunction does so, in which case the mapping is left unestablished
getIfPresentback to summary
public @Nullable CompletableFuture<V> getIfPresent(K key)

Returns the future associated with the key in this cache, or null if there is no cached future for the key.

Parameters
key:K

the key whose associated value is to be returned

Returns:@Nullable CompletableFuture<V>

the future value to which the specified key is mapped, or null if this cache does not contain a mapping for the key

Exceptions
NullPointerException:
if the specified key is null
putback to summary
public void put(K key, CompletableFuture<? extends V> valueFuture)

Associates value with key in this cache. If the cache previously contained a value associated with key, the old value is replaced by value. If the asynchronous computation fails, the entry will be automatically removed.

Prefer get(Object, Function) when using the conventional "if cached, return; otherwise create, cache, and return" pattern.

Parameters
key:K

the key with which the specified value is to be associated

valueFuture:CompletableFuture<? extends V>

the value to be associated with the specified key

Exceptions
NullPointerException:
if the specified key or value is null
synchronousback to summary
public Cache<K, V> synchronous()

Returns a view of the entries stored in this cache as a synchronous Cache. 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:Cache<K, V>

a thread-safe synchronous view of this cache