Class 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 Detail

      • DEFAULT_DEADLINE_TIME_MILLIS

        public static final long DEFAULT_DEADLINE_TIME_MILLIS
        The default deadline to impose on loading elements.
        See Also:
        Constant Field Values
      • 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:
        Constant Field Values
    • Constructor Detail

      • AsyncLoadingCache

        public AsyncLoadingCache​(long refreshTimeMillis)
      • AsyncLoadingCache

        public AsyncLoadingCache​(long refreshTimeMillis,
                                 long deadlineTimeMillis)
      • AsyncLoadingCache

        public AsyncLoadingCache​(long refreshTimeMillis,
                                 long deadlineTimeMillis,
                                 long maxSize)
    • Method Detail

      • 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()
      • 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()