Interface CacheLoader<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 CacheLoader<K,V>
Cache Loader SPI - Synchronous cache value loader interface 缓存加载器 SPI - 同步缓存值加载接口

Provides interface for loading cache values when they are not present.

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

Features | 主要功能:

  • Single value loading - 单值加载
  • Batch loading - 批量加载
  • Reload/refresh support - 重新加载/刷新支持

Usage Examples | 使用示例:

CacheLoader<String, User> loader = key -> userDao.findById(key);

// Or with batch loading - 或者批量加载
CacheLoader<String, User> batchLoader = new CacheLoader<>() {
    public User load(String key) { return userDao.findById(key); }
    public Map<String, User> loadAll(Set<String> keys) {
        return userDao.findByIds(keys);
    }
};

Security | 安全性:

  • Thread-safe: Implementation dependent - 线程安全: 取决于实现
  • Null-safe: May return null for missing values - 空值安全: 可为缺失值返回 null
Since:
JDK 25, opencode-base-cache V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    load(K key)
    Load value for single key 加载单个键的值
    default Map<K,V>
    loadAll(Set<? extends K> keys)
    Batch load values for multiple keys (default: load one by one) 批量加载多个键的值(默认:逐个加载)
    default V
    reload(K key, V oldValue)
    Reload value (default: just load again) 重新加载值(默认:直接重新加载)
  • Method Details

    • load

      V load(K key) throws Exception
      Load value for single key 加载单个键的值
      Parameters:
      key - the key | 键
      Returns:
      the value, or null if not found | 值,未找到返回 null
      Throws:
      Exception - if loading fails | 加载失败时抛出异常
    • loadAll

      default Map<K,V> loadAll(Set<? extends K> keys) throws Exception
      Batch load values for multiple keys (default: load one by one) 批量加载多个键的值(默认:逐个加载)
      Parameters:
      keys - the keys | 键集合
      Returns:
      map of key-value pairs | 键值对 Map
      Throws:
      Exception - if loading fails | 加载失败时抛出异常
    • reload

      default V reload(K key, V oldValue) throws Exception
      Reload value (default: just load again) 重新加载值(默认:直接重新加载)
      Parameters:
      key - the key | 键
      oldValue - the old value | 旧值
      Returns:
      the new value | 新值
      Throws:
      Exception - if reloading fails | 重新加载失败时抛出异常