Class AsyncLoadingCache<K,V>

java.lang.Object
com.apple.foundationdb.record.AsyncLoadingCache<K,V>
Type Parameters:
K - key type for the cache
V - value type for the cache

@API(UNSTABLE) public class AsyncLoadingCache<K,V> extends Object
A cache for retaining the result of some asynchronous operation up until some expiration time.
  • Field Details

    • DEFAULT_DEADLINE_TIME_MILLIS

      public static final long DEFAULT_DEADLINE_TIME_MILLIS
      The default deadline to impose on loading elements.
      See Also:
    • UNLIMITED

      public static final long UNLIMITED
      A 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 them
      deadlineTimeMillis - the maximum amount of time in milliseconds that loading a cache element can take
      maxSize - the maximum number of elements in the cache
      scheduledExecutor - 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 for key, retrieve the value using the provided asynchronous Supplier. If the value is not currently cached, then the Supplier 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 lookup
      supplier - 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 by orElseGet(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 a MoreAsyncUtil.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 return Long.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

      public String toString()
      Overrides:
      toString in class Object