Interface AsyncCacheLoader<K,V>

Type Parameters:
K - key type (must be non-null)
V - value type (must be non-null)
Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface AsyncCacheLoader<K,V>
Asynchronous functional interface for loading values in cache-aside pattern.

Implementations should load a value asynchronously from an upstream source (database, API, etc.) when the cache experiences a miss. This is the async counterpart of CacheLoader.

Thread Safety

Implementations must be thread-safe as they may be called from multiple threads concurrently.
See Also:
  • Method Details

    • loadAsync

      CompletableFuture<V> loadAsync(K key)
      Asynchronously load a value for the given key.
      Parameters:
      key - the key to load (must not be null)
      Returns:
      a future that completes with the loaded value (must not complete with null)
      Throws:
      NullPointerException - if key is null
    • loadAllAsync

      default CompletableFuture<Map<K,V>> loadAllAsync(Collection<K> keys)
      Asynchronously load values for all given keys.

      The default implementation calls loadAsync(Object) for each key individually and combines the results. Override this method to provide a more efficient bulk-loading implementation (e.g., a single batch query).

      Keys whose futures complete exceptionally are silently skipped in the result map.

      Parameters:
      keys - the keys to load (must not be null, must not contain null elements)
      Returns:
      a future that completes with a map of successfully loaded key-value pairs
      Throws:
      NullPointerException - if keys is null