Interface LoadingCache<K,V>

Type Parameters:
K - the type of keys | 键类型
V - the type of values | 值类型

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
  • Null-safe: Partial (null values not allowed) - 空值安全: 部分(不允许 null 值)
All Superinterfaces:
Cache<K,V>

public interface LoadingCache<K,V> extends Cache<K,V>
Loading Cache Interface - Cache with automatic value loading 加载缓存接口 - 具有自动值加载功能的缓存

Extends Cache with automatic loading capabilities. When a key is not present, the configured loader is automatically called to load the value.

扩展 Cache,具有自动加载功能。当键不存在时,自动调用配置的加载器加载值。

Features | 主要功能:

  • Automatic loading on get - 获取时自动加载
  • Batch loading support - 批量加载支持
  • Refresh support - 刷新支持
  • Async loading - 异步加载

Usage Examples | 使用示例:

// Create loading cache
LoadingCache<String, User> cache = LoadingCache.create(
    "users",
    key -> userService.findById(key),
    CacheConfig.<String, User>builder()
        .maximumSize(10000)
        .expireAfterWrite(Duration.ofMinutes(30))
        .build()
);

// Get value - automatically loads if not present
User user = cache.get("user:1001");

// Refresh value
cache.refresh("user:1001");
Since:
JDK 25, opencode-base-cache V2.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • get

      V get(K key)
      Get value, automatically loading if not present 获取值,不存在时自动加载

      Unlike Cache.get(Object), this method never returns null for non-null keys if the loader can provide a value.

      Cache.get(Object) 不同,如果加载器能提供值,此方法对非 null 键永不返回 null。

      Specified by:
      get in interface Cache<K,V>
      Parameters:
      key - the key | 键
      Returns:
      the value | 值
      Throws:
      RuntimeException - if loading fails | 加载失败时抛出异常
    • getAll

      Map<K,V> getAll(Iterable<? extends K> keys)
      Get all values for given keys, loading missing entries 获取所有键的值,加载缺失的条目
      Specified by:
      getAll in interface Cache<K,V>
      Parameters:
      keys - the keys | 键集合
      Returns:
      map of all entries | 所有条目 Map
    • refresh

      CompletableFuture<V> refresh(K key)
      Refresh value for given key 刷新给定键的值

      Asynchronously reloads the value for the key. If the key is not present, it will be loaded.

      异步重新加载键的值。如果键不存在,将被加载。

      Parameters:
      key - the key to refresh | 要刷新的键
      Returns:
      future that completes when refresh is done | 刷新完成时完成的 Future
    • refreshAll

      default int refreshAll(Predicate<K> predicate)
      Refresh all entries matching the predicate 刷新所有匹配条件的条目
      Parameters:
      predicate - the predicate to match keys | 匹配键的条件
      Returns:
      count of refreshed entries | 刷新的条目数
    • loader

      Function<? super K, ? extends V> loader()
      Get the loader function 获取加载器函数
      Returns:
      the loader | 加载器
    • asyncLoading

      cloud.opencode.base.cache.AsyncLoadingCache<K,V> asyncLoading()
      Get async view of this loading cache 获取加载缓存的异步视图
      Returns:
      async loading cache view | 异步加载缓存视图
    • create

      static <K,V> LoadingCache<K,V> create(String name, Function<? super K, ? extends V> loader, CacheConfig<K,V> config)
      Create a loading cache with the given loader 使用给定的加载器创建加载缓存
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      name - cache name | 缓存名称
      loader - the loader function | 加载函数
      config - cache configuration | 缓存配置
      Returns:
      loading cache | 加载缓存
    • create

      static <K,V> LoadingCache<K,V> create(String name, Function<? super K, ? extends V> loader)
      Create a loading cache with default configuration 使用默认配置创建加载缓存
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      name - cache name | 缓存名称
      loader - the loader function | 加载函数
      Returns:
      loading cache | 加载缓存
    • createWithLoader

      static <K,V> LoadingCache<K,V> createWithLoader(String name, CacheLoader<K,V> loader, CacheConfig<K,V> config)
      Create a loading cache with CacheLoader 使用 CacheLoader 创建加载缓存
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      name - cache name | 缓存名称
      loader - the cache loader | 缓存加载器
      config - cache configuration | 缓存配置
      Returns:
      loading cache | 加载缓存