Interface CacheWarmer<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 CacheWarmer<K,V>
Cache Warmer SPI - Cache pre-warming interface 缓存预热器 SPI - 缓存预热接口

Provides interface for pre-loading cache data at application startup.

提供应用启动时预加载缓存数据的接口。

Features | 主要功能:

  • Sync/Async warming - 同步/异步预热
  • Priority-based loading - 基于优先级的加载
  • Completion callbacks - 完成回调
  • Paged loading support - 分页加载支持

Usage Examples | 使用示例:

CacheWarmer<Long, Product> warmer = () -> {
    return productDao.findHotProducts(1000).stream()
        .collect(Collectors.toMap(Product::getId, p -> p));
};

// With paged loading - 分页加载
CacheWarmer<Long, Product> pagedWarmer = CacheWarmer.paged(
    offset -> productDao.findProducts(offset, 100),
    100, 10);

Security | 安全性:

  • Thread-safe: Implementation dependent - 线程安全: 取决于实现
  • Null-safe: Yes - 空值安全: 是
Since:
JDK 25, opencode-base-cache V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    static <K,V> CacheWarmer<K,V>
    Create empty warmer 创建空预热器
    static <K,V> CacheWarmer<K,V>
    from(Supplier<Map<K,V>> supplier)
    Create warmer from supplier 从 Supplier 创建预热器
    default boolean
    Check if warming is enabled 检查是否启用预热
    default void
    onComplete(int loadedCount, Duration duration)
    Called when warm-up completes 预热完成时调用
    default void
    Called when warm-up fails 预热失败时调用
    static <K,V> CacheWarmer<K,V>
    paged(Function<Integer,Map<K,V>> pageLoader, int pageSize, int maxPages)
    Create paged loading warmer 创建分页加载预热器
    default int
    Get warm-up priority (lower = higher priority) 获取预热优先级(数值越小优先级越高)
    Perform warm-up and return data to load 执行预热并返回要加载的数据
    Async warm-up 异步预热
  • Method Details

    • warmUp

      Map<K,V> warmUp()
      Perform warm-up and return data to load 执行预热并返回要加载的数据
      Returns:
      map of key-value pairs to load | 要加载的键值对 Map
    • warmUpAsync

      default CompletableFuture<Map<K,V>> warmUpAsync(Executor executor)
      Async warm-up 异步预热
      Parameters:
      executor - the executor | 执行器
      Returns:
      future containing data | 包含数据的 Future
    • priority

      default int priority()
      Get warm-up priority (lower = higher priority) 获取预热优先级(数值越小优先级越高)
      Returns:
      priority | 优先级
    • isEnabled

      default boolean isEnabled()
      Check if warming is enabled 检查是否启用预热
      Returns:
      true if enabled | 启用返回 true
    • onComplete

      default void onComplete(int loadedCount, Duration duration)
      Called when warm-up completes 预热完成时调用
      Parameters:
      loadedCount - number of entries loaded | 加载的条目数
      duration - warm-up duration | 预热耗时
    • onError

      default void onError(Throwable cause)
      Called when warm-up fails 预热失败时调用
      Parameters:
      cause - failure cause | 失败原因
    • from

      static <K,V> CacheWarmer<K,V> from(Supplier<Map<K,V>> supplier)
      Create warmer from supplier 从 Supplier 创建预热器
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      supplier - data supplier | 数据供应器
      Returns:
      warmer | 预热器
    • paged

      static <K,V> CacheWarmer<K,V> paged(Function<Integer,Map<K,V>> pageLoader, int pageSize, int maxPages)
      Create paged loading warmer 创建分页加载预热器
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      pageLoader - page loader function | 分页加载函数
      pageSize - page size | 页大小
      maxPages - max pages to load | 最大加载页数
      Returns:
      warmer | 预热器
    • empty

      static <K,V> CacheWarmer<K,V> empty()
      Create empty warmer 创建空预热器
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Returns:
      empty warmer | 空预热器