Interface LocalAsyncCache<K,V>
-
- All Superinterfaces:
AsyncCache<K,V>
- All Known Implementing Classes:
BoundedLocalCache.BoundedLocalAsyncCache
,BoundedLocalCache.BoundedLocalAsyncLoadingCache
,LocalAsyncLoadingCache
,UnboundedLocalCache.UnboundedLocalAsyncCache
,UnboundedLocalCache.UnboundedLocalAsyncLoadingCache
interface LocalAsyncCache<K,V> extends AsyncCache<K,V>
This class provides a skeletal implementation of theAsyncCache
interface to minimize the effort required to implement aLocalCache
.
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static class
LocalAsyncCache.AbstractCacheView<K,V>
static class
LocalAsyncCache.AsMapView<K,V>
static class
LocalAsyncCache.AsyncAsMapView<K,V>
static class
LocalAsyncCache.AsyncBulkCompleter<K,V>
A function executed asynchronously after a bulk load completes.static class
LocalAsyncCache.CacheView<K,V>
-
Field Summary
Fields Modifier and Type Field Description static java.util.logging.Logger
logger
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description LocalCache<K,java.util.concurrent.CompletableFuture<V>>
cache()
Returns the backingLocalCache
data store.default java.util.concurrent.CompletableFuture<java.util.Map<K,V>>
composeResult(java.util.Map<K,java.util.concurrent.CompletableFuture<V>> futures)
Returns a future that waits for all of the dependent futures to complete and returns the combined mapping if successful.default java.util.concurrent.CompletableFuture<V>
get(@NonNull K key, @NonNull java.util.function.Function<? super K,? extends V> mappingFunction)
Returns the future associated withkey
in this cache, obtaining that value frommappingFunction
if necessary.default java.util.concurrent.CompletableFuture<V>
get(K key, java.util.function.BiFunction<? super K,java.util.concurrent.Executor,java.util.concurrent.CompletableFuture<V>> mappingFunction)
Returns the future associated withkey
in this cache, obtaining that value frommappingFunction
if necessary.default java.util.concurrent.CompletableFuture<V>
get(K key, java.util.function.BiFunction<? super K,java.util.concurrent.Executor,java.util.concurrent.CompletableFuture<V>> mappingFunction, boolean recordStats)
default java.util.concurrent.CompletableFuture<java.util.Map<K,V>>
getAll(java.lang.Iterable<? extends @NonNull K> keys, java.util.function.BiFunction<java.lang.Iterable<? extends K>,java.util.concurrent.Executor,java.util.concurrent.CompletableFuture<java.util.Map<K,V>>> mappingFunction)
Returns the future of a map of the values associated withkeys
, creating or retrieving those values if necessary.default java.util.concurrent.CompletableFuture<java.util.Map<K,V>>
getAll(java.lang.Iterable<? extends @NonNull K> keys, java.util.function.Function<java.lang.Iterable<? extends K>,java.util.Map<K,V>> mappingFunction)
Returns the future of a map of the values associated withkeys
, creating or retrieving those values if necessary.default @Nullable java.util.concurrent.CompletableFuture<V>
getIfPresent(@NonNull java.lang.Object key)
Returns the future associated withkey
in this cache, ornull
if there is no cached future forkey
.default void
handleCompletion(K key, java.util.concurrent.CompletableFuture<V> valueFuture, long startTime, boolean recordMiss)
Policy<K,V>
policy()
Returns the policy supported by this implementation and its configuration.default void
put(K key, java.util.concurrent.CompletableFuture<V> valueFuture)
Associatesvalue
withkey
in this cache.-
Methods inherited from interface com.github.benmanes.caffeine.cache.AsyncCache
asMap, synchronous
-
-
-
-
Method Detail
-
cache
LocalCache<K,java.util.concurrent.CompletableFuture<V>> cache()
Returns the backingLocalCache
data store.
-
policy
Policy<K,V> policy()
Returns the policy supported by this implementation and its configuration.
-
getIfPresent
default @Nullable java.util.concurrent.CompletableFuture<V> getIfPresent(@NonNull java.lang.Object key)
Description copied from interface:AsyncCache
Returns the future associated withkey
in this cache, ornull
if there is no cached future forkey
.- Specified by:
getIfPresent
in interfaceAsyncCache<K,V>
- Parameters:
key
- key whose associated value is to be returned- Returns:
- the current (existing or computed) future value to which the specified key is mapped,
or
null
if this cache contains no mapping for the key
-
get
default java.util.concurrent.CompletableFuture<V> get(@NonNull K key, @NonNull java.util.function.Function<? super K,? extends V> mappingFunction)
Description copied from interface:AsyncCache
Returns the future associated withkey
in this cache, obtaining that value frommappingFunction
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(K)
,mappingFunction
must not attempt to update any other mappings of this cache.- Specified by:
get
in interfaceAsyncCache<K,V>
- Parameters:
key
- key with which the specified value is to be associatedmappingFunction
- the function to asynchronously compute a value- Returns:
- the current (existing or computed) future value associated with the specified key
-
get
default java.util.concurrent.CompletableFuture<V> get(K key, java.util.function.BiFunction<? super K,java.util.concurrent.Executor,java.util.concurrent.CompletableFuture<V>> mappingFunction)
Description copied from interface:AsyncCache
Returns the future associated withkey
in this cache, obtaining that value frommappingFunction
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(K)
,mappingFunction
must not attempt to update any other mappings of this cache.- Specified by:
get
in interfaceAsyncCache<K,V>
- Parameters:
key
- key with which the specified value is to be associatedmappingFunction
- the function to asynchronously compute a value- Returns:
- the current (existing or computed) future value associated with the specified key
-
get
default java.util.concurrent.CompletableFuture<V> get(K key, java.util.function.BiFunction<? super K,java.util.concurrent.Executor,java.util.concurrent.CompletableFuture<V>> mappingFunction, boolean recordStats)
-
getAll
default java.util.concurrent.CompletableFuture<java.util.Map<K,V>> getAll(java.lang.Iterable<? extends @NonNull K> keys, java.util.function.Function<java.lang.Iterable<? extends K>,java.util.Map<K,V>> mappingFunction)
Description copied from interface:AsyncCache
Returns the future of a map of the values associated withkeys
, 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 toAsyncCache.get(K, java.util.function.Function<? super K, ? extends V>)
tries to load the value for a key inkeys
, 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 byObject.equals(java.lang.Object)
, will be ignored.- Specified by:
getAll
in interfaceAsyncCache<K,V>
- Parameters:
keys
- the keys whose associated values are to be returnedmappingFunction
- the function to asynchronously compute the values- Returns:
- the future containing an unmodifiable mapping of keys to values for the specified keys in this cache
-
getAll
default java.util.concurrent.CompletableFuture<java.util.Map<K,V>> getAll(java.lang.Iterable<? extends @NonNull K> keys, java.util.function.BiFunction<java.lang.Iterable<? extends K>,java.util.concurrent.Executor,java.util.concurrent.CompletableFuture<java.util.Map<K,V>>> mappingFunction)
Description copied from interface:AsyncCache
Returns the future of a map of the values associated withkeys
, 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 toAsyncCache.get(K, java.util.function.Function<? super K, ? extends V>)
tries to load the value for a key inkeys
, 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 byObject.equals(java.lang.Object)
, will be ignored.- Specified by:
getAll
in interfaceAsyncCache<K,V>
- Parameters:
keys
- the keys whose associated values are to be returnedmappingFunction
- the function to asynchronously compute the values- Returns:
- the future containing an unmodifiable mapping of keys to values for the specified keys in this cache
-
composeResult
default java.util.concurrent.CompletableFuture<java.util.Map<K,V>> composeResult(java.util.Map<K,java.util.concurrent.CompletableFuture<V>> futures)
Returns a future that waits for all of the dependent futures to complete and returns the combined mapping if successful. If any future fails then it is automatically removed from the cache if still present.
-
put
default void put(K key, java.util.concurrent.CompletableFuture<V> valueFuture)
Description copied from interface:AsyncCache
Associatesvalue
withkey
in this cache. If the cache previously contained a value associated withkey
, the old value is replaced byvalue
. If the asynchronous computation fails, the entry will be automatically removed.Prefer
AsyncCache.get(Object, Function)
when using the conventional "if cached, return; otherwise create, cache and return" pattern.- Specified by:
put
in interfaceAsyncCache<K,V>
- Parameters:
key
- key with which the specified value is to be associatedvalueFuture
- value to be associated with the specified key
-
-