Interface AsyncCacheLoader<K,​V>

  • All Known Subinterfaces:
    CacheLoader<K,​V>
    Functional Interface:
    This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

    @ThreadSafe
    @FunctionalInterface
    public interface AsyncCacheLoader<K,​V>
    Computes or retrieves values asynchronously, based on a key, for use in populating a AsyncLoadingCache.

    Most implementations will only need to implement asyncLoad(K, java.util.concurrent.Executor). Other methods may be overridden as desired.

    Usage example:

    
       AsyncCacheLoader<Key, Graph> loader = (key, executor) ->
           createExpensiveGraphAsync(key, executor);
       AsyncLoadingCache<Key, Graph> cache = Caffeine.newBuilder().buildAsync(loader);
     
    • Method Summary

      All Methods Instance Methods Abstract Methods Default Methods 
      Modifier and Type Method Description
      java.util.concurrent.CompletableFuture<V> asyncLoad​(K key, java.util.concurrent.Executor executor)
      Asynchronously computes or retrieves the value corresponding to key.
      default java.util.concurrent.CompletableFuture<java.util.Map<K,​V>> asyncLoadAll​(java.lang.Iterable<? extends K> keys, java.util.concurrent.Executor executor)
      Asynchronously computes or retrieves the values corresponding to keys.
      default java.util.concurrent.CompletableFuture<V> asyncReload​(K key, V oldValue, java.util.concurrent.Executor executor)
      Asynchronously computes or retrieves a replacement value corresponding to an already-cached key.
    • Method Detail

      • asyncLoad

        @Nonnull
        java.util.concurrent.CompletableFuture<V> asyncLoad​(@Nonnull
                                                            K key,
                                                            @Nonnull
                                                            java.util.concurrent.Executor executor)
        Asynchronously computes or retrieves the value corresponding to key.
        Parameters:
        key - the non-null key whose value should be loaded
        executor - the executor with which the entry is asynchronously loaded
        Returns:
        the future value associated with key
      • asyncLoadAll

        @Nonnull
        default java.util.concurrent.CompletableFuture<java.util.Map<K,​V>> asyncLoadAll​(@Nonnull
                                                                                              java.lang.Iterable<? extends K> keys,
                                                                                              @Nonnull
                                                                                              java.util.concurrent.Executor executor)
        Asynchronously computes or retrieves the values corresponding to keys. This method is called by AsyncLoadingCache.getAll(java.lang.Iterable<? extends K>).

        If the returned map doesn't contain all requested keys then the entries it does contain will be cached and getAll will return the partial results. If the returned map contains extra keys not present in keys then all returned entries will be cached, but only the entries for keys will be returned from getAll.

        This method should be overridden when bulk retrieval is significantly more efficient than many individual lookups. Note that AsyncLoadingCache.getAll(java.lang.Iterable<? extends K>) will defer to individual calls to AsyncLoadingCache.get(K, java.util.function.Function<? super K, ? extends V>) if this method is not overridden.

        Parameters:
        keys - the unique, non-null keys whose values should be loaded
        executor - the executor with which the entries are asynchronously loaded
        Returns:
        a future containing the map from each key in keys to the value associated with that key; may not contain null values
      • asyncReload

        @Nonnull
        default java.util.concurrent.CompletableFuture<V> asyncReload​(@Nonnull
                                                                      K key,
                                                                      @Nonnull
                                                                      V oldValue,
                                                                      @Nonnull
                                                                      java.util.concurrent.Executor executor)
        Asynchronously computes or retrieves a replacement value corresponding to an already-cached key. If the replacement value is not found then the mapping will be removed if null is computed. This method is called when an existing cache entry is refreshed by Caffeine.refreshAfterWrite(long, java.util.concurrent.TimeUnit), or through a call to LoadingCache.refresh(K).

        Note: all exceptions thrown by this method will be logged and then swallowed.

        Parameters:
        key - the non-null key whose value should be loaded
        oldValue - the non-null old value corresponding to key
        executor - the executor with which the entry is asynchronously loaded
        Returns:
        a future containing the new value associated with key, or containing null if the mapping is to be removed