Interface AsyncLoadingCache<K,​V>

  • Type Parameters:
    K - the type of keys maintained by this cache
    V - the type of mapped values
    All Superinterfaces:

    public interface AsyncLoadingCache<K,​V>
    extends AsyncCache<K,​V>
    A semi-persistent mapping from keys to values. Values are automatically loaded by the cache asynchronously and are stored in the cache until either evicted or manually invalidated.

    Implementations of this interface are expected to be thread-safe and can be safely accessed by multiple concurrent threads.

    • Method Detail

      • get

        CompletableFuture<V> get​(K key)
        Returns the future associated with the key in this cache, obtaining that value from AsyncCacheLoader.asyncLoad(K, java.util.concurrent.Executor) 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.

        key - the key with which the specified value is to be associated
        the current (existing or computed) future value associated with the specified key
        NullPointerException - if the specified key is null or if the future returned by the AsyncCacheLoader is null
        RuntimeException - or Error if the AsyncCacheLoader does when constructing the future, in which case the mapping is left unestablished
      • synchronous

        LoadingCache<K,​V> 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.
        Specified by:
        synchronous in interface AsyncCache<K,​V>
        a thread-safe synchronous view of this cache