Package com.apple.foundationdb.record
Class AsyncLoadingCache<K,V>
java.lang.Object
com.apple.foundationdb.record.AsyncLoadingCache<K,V>
- Type Parameters:
K
- key type for the cacheV
- value type for the cache
A cache for retaining the result of some asynchronous operation up until some expiration time.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final long
The default deadline to impose on loading elements.static final long
A constant to indicate that an operation should not have a limit. -
Constructor Summary
ConstructorsConstructorDescriptionAsyncLoadingCache
(long refreshTimeMillis, long deadlineTimeMillis, long maxSize, ScheduledExecutorService scheduledExecutor) Create a new cache that loads items asynchronously. -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
long
Get the deadline time imposed on cache loading in milliseconds.long
Get the maximum number of elements stored by the cache.long
Get the amount of time between cache refreshes in milliseconds.long
Get the amount of time between cache refreshes in seconds.orElseGet
(K key, Supplier<CompletableFuture<V>> supplier) If the cache does not contain an entry forkey
, retrieve the value using the provided asynchronousSupplier
.toString()
-
Field Details
-
DEFAULT_DEADLINE_TIME_MILLIS
public static final long DEFAULT_DEADLINE_TIME_MILLISThe default deadline to impose on loading elements.- See Also:
-
UNLIMITED
public static final long UNLIMITEDA constant to indicate that an operation should not have a limit. For example, this can be provided to the cache as the deadline time or the max size to indicate that no deadline should be imposed on futures loading entries into the cache or that there should not be a maximum number of cached elements.- See Also:
-
-
Constructor Details
-
AsyncLoadingCache
public AsyncLoadingCache(long refreshTimeMillis, long deadlineTimeMillis, long maxSize, @Nonnull ScheduledExecutorService scheduledExecutor) Create a new cache that loads items asynchronously.- Parameters:
refreshTimeMillis
- the amount of time to let cache entries sit in the cache before reloading themdeadlineTimeMillis
- the maximum amount of time in milliseconds that loading a cache element can takemaxSize
- the maximum number of elements in the cachescheduledExecutor
- a scheduled executor used to manage asynchronous deadlines
-
-
Method Details
-
orElseGet
@Nonnull public CompletableFuture<V> orElseGet(@Nonnull K key, @Nonnull Supplier<CompletableFuture<V>> supplier) If the cache does not contain an entry forkey
, retrieve the value using the provided asynchronousSupplier
. If the value is not currently cached, then theSupplier
is used to load the value asynchronously. If multiple callers ask for the same key at the same time, then they might duplicate each other's work in that both callers will result in a future being created. Whichever future completes first will insert its value into the cache, and all callers that then complete successfully are guaranteed to see that object until such time as the value is expired from the cache.- Parameters:
key
- the key in the cache to lookupsupplier
- an asynchronous operation to retrieve the desired value if the cache is empty- Returns:
- a future containing either the cached value or the result from the supplier
-
getRefreshTimeSeconds
public long getRefreshTimeSeconds()Get the amount of time between cache refreshes in seconds.- Returns:
- the cache refresh time in seconds
-
getRefreshTimeMillis
public long getRefreshTimeMillis()Get the amount of time between cache refreshes in milliseconds.- Returns:
- the cache refresh time in milliseconds
-
getDeadlineTimeMillis
public long getDeadlineTimeMillis()Get the deadline time imposed on cache loading in milliseconds. This is used byorElseGet(Object, Supplier)
to avoid futures hanging during cache loading. If a future takes longer than the returned number of milliseconds to complete, the cache loading operation is failed with aMoreAsyncUtil.DeadlineExceededException
.- Returns:
- the cache loading deadline time in milliseconds
-
getMaxSize
public long getMaxSize()Get the maximum number of elements stored by the cache. This will returnLong.MAX_VALUE
if there is no maximum size enforced by this cache.- Returns:
- the maximum number of elements stored by the cache
-
clear
public void clear() -
toString
-