Interface AsyncCacheLoader<K,V>

Type Parameters:
K - the type of keys | 键类型
V - the type of values | 值类型
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>
Async Cache Loader SPI - Asynchronous cache value loader interface 异步缓存加载器 SPI - 异步缓存值加载接口

Provides interface for asynchronously loading cache values when not present.

提供缓存值不存在时的异步加载接口。

Features | 主要功能:

  • Async single value loading - 异步单值加载
  • Async batch loading - 异步批量加载
  • Async reload/refresh - 异步重新加载/刷新

Usage Examples | 使用示例:

AsyncCacheLoader<String, User> loader = (key, executor) ->
    CompletableFuture.supplyAsync(() -> userDao.findById(key), executor);

Security | 安全性:

  • Thread-safe: Implementation dependent - 线程安全: 取决于实现
  • Null-safe: Future may complete with null - 空值安全: Future 可能以 null 完成
Since:
JDK 25, opencode-base-cache V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • asyncLoad

      CompletableFuture<V> asyncLoad(K key, Executor executor)
      Async load value for single key 异步加载单个键的值
      Parameters:
      key - the key | 键
      executor - the executor | 执行器
      Returns:
      future containing value | 包含值的 Future
    • asyncLoadAll

      default CompletableFuture<Map<K,V>> asyncLoadAll(Set<? extends K> keys, Executor executor)
      Async batch load values 异步批量加载值
      Parameters:
      keys - the keys | 键集合
      executor - the executor | 执行器
      Returns:
      future containing map of values | 包含值 Map 的 Future
    • asyncReload

      default CompletableFuture<V> asyncReload(K key, V oldValue, Executor executor)
      Async reload value 异步重新加载值
      Parameters:
      key - the key | 键
      oldValue - the old value | 旧值
      executor - the executor | 执行器
      Returns:
      future containing new value | 包含新值的 Future
    • from

      static <K,V> AsyncCacheLoader<K,V> from(CacheLoader<K,V> loader)
      Create from sync loader 从同步加载器创建
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      loader - sync loader | 同步加载器
      Returns:
      async loader | 异步加载器