Top Description Inners Fields Constructors Methods
com.github.benmanes.caffeine.cache

pack-priv abstract Class LocalAsyncLoadingCache<K, V>

extends Object
implements LocalAsyncCache<K, V>, AsyncLoadingCache<K, V>
Class Inheritance
All Implemented Interfaces
com.github.benmanes.caffeine.cache.AsyncLoadingCache, com.github.benmanes.caffeine.cache.AsyncCache, com.github.benmanes.caffeine.cache.LocalAsyncCache
Known Direct Subclasses
com.github.benmanes.caffeine.cache.UnboundedLocalCache.UnboundedLocalAsyncLoadingCache, com.github.benmanes.caffeine.cache.BoundedLocalCache.BoundedLocalAsyncLoadingCache
Static Imports
com.github.benmanes.caffeine.cache.Caffeine.calculateHashMapCapacity, .LocalAsyncCache.composeResult, java.util.Objects.requireNonNull

This class provides a skeletal implementation of the AsyncLoadingCache interface to minimize the effort required to implement a LocalCache.
Author
[email protected] (Ben Manes)

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class

Field Summary

Modifier and TypeField and Description
pack-priv final @Nullable BiFunction<? super Set<? extends K>, ? super Executor, ? extends CompletableFuture<? extends Map<? extends K, ? extends V>>>
pack-priv final AsyncCacheLoader<K, V>
pack-priv @Nullable LocalAsyncLoadingCache.LoadingCacheView<K, V>
pack-priv static final System.Logger
pack-priv final BiFunction<? super K, ? super Executor, ? extends CompletableFuture<? extends V>>

Constructor Summary

AccessConstructor and Description
pack-priv
LocalAsyncLoadingCache(AsyncCacheLoader<? super K, V> cacheLoader)

Method Summary

Modifier and TypeMethod and Description
pack-priv boolean
canBulkLoad(AsyncCacheLoader<?, ?> loader)

Returns whether the supplied cache loader has bulk load functionality.

public CompletableFuture<V>
get(K
the key with which the specified value is to be associated
key
)

Implements com.github.benmanes.caffeine.cache.AsyncLoadingCache.get.

Returns the future associated with the key in this cache, obtaining that value from AsyncCacheLoader#asyncLoad if necessary.
public CompletableFuture<Map<K, V>>
getAll(Iterable<? extends K>
the keys whose associated values are to be returned
keys
)

Implements com.github.benmanes.caffeine.cache.AsyncLoadingCache.getAll.

Returns the future of a map of the values associated with keys, creating or retrieving those values if necessary.
pack-priv @Nullable BiFunction<Set<? extends K>, Executor, CompletableFuture<Map<K, V>>>
newBulkMappingFunction(AsyncCacheLoader<? super K, V> cacheLoader)

Returns a mapping function that adapts to AsyncCacheLoader#asyncLoadAll, if implemented.

pack-priv BiFunction<? super K, ? super Executor, ? extends CompletableFuture<? extends V>>
newMappingFunction(AsyncCacheLoader<? super K, V> cacheLoader)

Returns a mapping function that adapts to AsyncCacheLoader#asyncLoad.

public LoadingCache<K, V>
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

bulkMappingFunctionback to summary
pack-priv final @Nullable BiFunction<? super Set<? extends K>, ? super Executor, ? extends CompletableFuture<? extends Map<? extends K, ? extends V>>> bulkMappingFunction
cacheLoaderback to summary
pack-priv final AsyncCacheLoader<K, V> cacheLoader
cacheViewback to summary
pack-priv @Nullable LocalAsyncLoadingCache.LoadingCacheView<K, V> cacheView
loggerback to summary
pack-priv static final System.Logger logger

Hides com.github.benmanes.caffeine.cache.LocalAsyncCache.logger.

mappingFunctionback to summary
pack-priv final BiFunction<? super K, ? super Executor, ? extends CompletableFuture<? extends V>> mappingFunction

Constructor Detail

LocalAsyncLoadingCacheback to summary
pack-priv LocalAsyncLoadingCache(AsyncCacheLoader<? super K, V> cacheLoader)
Annotations
@SuppressWarnings:unchecked

Method Detail

canBulkLoadback to summary
pack-priv boolean canBulkLoad(AsyncCacheLoader<?, ?> loader)

Returns whether the supplied cache loader has bulk load functionality.

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

Implements com.github.benmanes.caffeine.cache.AsyncLoadingCache.get.

Doc from com.github.benmanes.caffeine.cache.AsyncLoadingCache.get.

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

Annotations
@Override
getAllback to summary
public CompletableFuture<Map<K, V>> getAll(Iterable<? extends K> keys)

Implements com.github.benmanes.caffeine.cache.AsyncLoadingCache.getAll.

Doc from com.github.benmanes.caffeine.cache.AsyncLoadingCache.getAll.

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

Annotations
@Override
newBulkMappingFunctionback to summary
pack-priv @Nullable BiFunction<Set<? extends K>, Executor, CompletableFuture<Map<K, V>>> newBulkMappingFunction(AsyncCacheLoader<? super K, V> cacheLoader)

Returns a mapping function that adapts to AsyncCacheLoader#asyncLoadAll, if implemented.

newMappingFunctionback to summary
pack-priv BiFunction<? super K, ? super Executor, ? extends CompletableFuture<? extends V>> newMappingFunction(AsyncCacheLoader<? super K, V> cacheLoader)

Returns a mapping function that adapts to AsyncCacheLoader#asyncLoad.

synchronousback to summary
public LoadingCache<K, V> synchronous()

Implements com.github.benmanes.caffeine.cache.AsyncCache.synchronous, com.github.benmanes.caffeine.cache.AsyncLoadingCache.synchronous.

Doc from com.github.benmanes.caffeine.cache.AsyncLoadingCache.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
com.github.benmanes.caffeine.cache back to summary

pack-priv final Class LocalAsyncLoadingCache.LoadingCacheView<K, V>

extends LocalAsyncCache.AbstractCacheView<K, V>
implements LoadingCache<K, V>
Class Inheritance
All Implemented Interfaces
com.github.benmanes.caffeine.cache.LoadingCache, com.github.benmanes.caffeine.cache.Cache

Field Summary

Modifier and TypeField and Description
pack-priv final LocalAsyncLoadingCache<K, V>
private static final long
Inherited from com.github.benmanes.caffeine.cache.LocalAsyncCache.AbstractCacheView:
asMapView

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
pack-priv LocalAsyncLoadingCache<K, V>
public V
get(K
the key with which the specified value is to be associated
key
)

Implements com.github.benmanes.caffeine.cache.LoadingCache.get.

Returns the value associated with the key in this cache, obtaining that value from CacheLoader#load(Object) if necessary.
public Map<K, V>
getAll(Iterable<? extends K>
the keys whose associated values are to be returned
keys
)

Implements com.github.benmanes.caffeine.cache.LoadingCache.getAll.

Returns a map of the values associated with the keys, creating or retrieving those values if necessary.
public CompletableFuture<V>
refresh(K
key with which a value may be associated
key
)

Implements com.github.benmanes.caffeine.cache.LoadingCache.refresh.

Loads a new value for the key, asynchronously.
public CompletableFuture<Map<K, V>>
refreshAll(Iterable<? extends K>
the keys whose associated values are to be returned
keys
)

Implements com.github.benmanes.caffeine.cache.LoadingCache.refreshAll.

Loads a new value for each key, asynchronously.
private @Nullable CompletableFuture<V>
tryComputeRefresh(K key, Object keyReference)

Begins a refresh if the entry has materialized and no reload is in-flight.

private @Nullable CompletableFuture<V>
tryOptimisticRefresh(K key, Object keyReference)

Attempts to avoid a reload if the entry is absent, or a load or reload is in-flight.

Inherited from com.github.benmanes.caffeine.cache.LocalAsyncCache.AbstractCacheView:
asMapcleanUpestimatedSizegetgetAllgetAllPresentgetIfPresentinvalidateinvalidateAllinvalidateAllpolicyputputAllresolvestats

Field Detail

asyncCacheback to summary
pack-priv final LocalAsyncLoadingCache<K, V> asyncCache
Annotations
@SuppressWarnings:serial
serialVersionUIDback to summary
private static final long serialVersionUID

Hides com.github.benmanes.caffeine.cache.LocalAsyncCache.AbstractCacheView.serialVersionUID.

Constructor Detail

LoadingCacheViewback to summary
pack-priv LoadingCacheView(LocalAsyncLoadingCache<K, V> asyncCache)

Method Detail

asyncCacheback to summary
pack-priv LocalAsyncLoadingCache<K, V> asyncCache()

Implements abstract com.github.benmanes.caffeine.cache.LocalAsyncCache.AbstractCacheView.asyncCache.

Annotations
@Override
getback to summary
public V get(K key)

Implements com.github.benmanes.caffeine.cache.LoadingCache.get.

Doc from com.github.benmanes.caffeine.cache.LoadingCache.get.

Returns the value associated with the key in this cache, obtaining that value from CacheLoader#load(Object) if necessary.

If another call to get is currently loading the value for the key, this thread simply waits for that thread to finish and returns its loaded value. Note that multiple threads can concurrently load values for distinct keys.

If the specified key is not already associated with a value, attempts to compute its value 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. Some attempted update operations on this cache by other threads may be blocked while the computation is in progress, so the computation should be short and simple, and 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

Returns:V

the current (existing or computed) value associated with the specified key, or null if the computed value is null

Annotations
@Override
@SuppressWarnings:PMD.PreserveStackTrace
getAllback to summary
public Map<K, V> getAll(Iterable<? extends K> keys)

Implements com.github.benmanes.caffeine.cache.LoadingCache.getAll.

Doc from com.github.benmanes.caffeine.cache.LoadingCache.getAll.

Returns 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 the newly loaded entries; it will never contain null keys or values.

Caches loaded by a CacheLoader will issue a single request to CacheLoader#loadAll for all keys which are not already present in the cache. All entries returned by CacheLoader#loadAll will be stored in the cache, overwriting any previously cached values. If another call to get tries to load the value for a key in keys, implementations may either have that thread load the entry or simply wait for this thread to finish and return the loaded value. In the case of overlapping non-blocking loads, the last load to complete will replace the existing entry. 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:Map<K, V>

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

Annotations
@Override
@SuppressWarnings:PMD.PreserveStackTrace
refreshback to summary
public CompletableFuture<V> refresh(K key)

Implements com.github.benmanes.caffeine.cache.LoadingCache.refresh.

Doc from com.github.benmanes.caffeine.cache.LoadingCache.refresh.

Loads a new value for the key, asynchronously. While the new value is loading the previous value (if any) will continue to be returned by get(key) unless it is evicted. If the new value is loaded successfully, it will replace the previous value in the cache; if an exception is thrown while refreshing, the previous value will remain, and the exception will be logged (using System.Logger) and swallowed.

Caches loaded by a CacheLoader will call CacheLoader#reload if the cache currently contains a value for the key, and CacheLoader#load otherwise. Loading is asynchronous by delegating to the default executor.

Returns an existing future without doing anything if another thread is currently loading the value for key.

Parameters
key:K

key with which a value may be associated

Returns:CompletableFuture<V>

the future that is loading the value

Annotations
@Override
refreshAllback to summary
public CompletableFuture<Map<K, V>> refreshAll(Iterable<? extends K> keys)

Implements com.github.benmanes.caffeine.cache.LoadingCache.refreshAll.

Doc from com.github.benmanes.caffeine.cache.LoadingCache.refreshAll.

Loads a new value for each key, asynchronously. While the new value is loading the previous value (if any) will continue to be returned by get(key) unless it is evicted. If the new value is loaded successfully, it will replace the previous value in the cache; if an exception is thrown while refreshing, the previous value will remain, and the exception will be logged (using System.Logger) and swallowed. If another thread is currently loading the value for key, then this method does not perform an additional load.

Caches loaded by a CacheLoader will call CacheLoader#reload if the cache currently contains a value for the key, and CacheLoader#load otherwise. Loading is asynchronous by delegating to the default executor.

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 that are loading the values

Annotations
@Override
tryComputeRefreshback to summary
private @Nullable CompletableFuture<V> tryComputeRefresh(K key, Object keyReference)

Begins a refresh if the entry has materialized and no reload is in-flight.

Annotations
@SuppressWarnings:FutureReturnValueIgnored
tryOptimisticRefreshback to summary
private @Nullable CompletableFuture<V> tryOptimisticRefresh(K key, Object keyReference)

Attempts to avoid a reload if the entry is absent, or a load or reload is in-flight.

Annotations
@SuppressWarnings:FutureReturnValueIgnored