Class CacheWarmer<K,V>

java.lang.Object
cloud.opencode.base.cache.warming.CacheWarmer<K,V>
Type Parameters:
K - key type | 键类型
V - value type | 值类型

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
  • Null-safe: Yes - 空值安全: 是
All Implemented Interfaces:
AutoCloseable

public class CacheWarmer<K,V> extends Object implements AutoCloseable
Cache Warmer - Advanced cache warming with batch loading and progress tracking 缓存预热器 - 具有批量加载和进度跟踪的高级缓存预热

Provides sophisticated cache warming capabilities including batch loading, priority-based warming, scheduled warming, and detailed progress monitoring.

提供复杂的缓存预热功能,包括批量加载、基于优先级的预热、 定时预热和详细的进度监控。

Features | 主要功能:

  • Batch preloading - 批量预加载
  • Priority-based warming - 基于优先级的预热
  • Scheduled warming - 定时预热
  • Progress tracking - 进度跟踪
  • Incremental warming - 增量预热
  • Parallel loading - 并行加载

Usage Examples | 使用示例:

// Create warmer
CacheWarmer<String, User> warmer = CacheWarmer.<String, User>builder()
    .cache(userCache)
    .loader(userId -> userRepository.findById(userId))
    .batchSize(100)
    .parallelism(4)
    .build();

// Warm with keys
WarmingResult result = warmer.warm(userIds);

// Warm with progress callback
warmer.warmAsync(userIds, progress -> {
    System.out.println("Progress: " + progress.percentComplete() + "%");
});

// Schedule periodic warming
warmer.scheduleWarming(Duration.ofHours(1), () -> getHotKeys());
Since:
JDK 25, opencode-base-cache V2.0.5
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • builder

      public static <K,V> CacheWarmer.Builder<K,V> builder()
      Create a builder 创建构建器
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Returns:
      builder | 构建器
    • warm

      public CacheWarmer.WarmingResult<K> warm(Iterable<? extends K> keys)
      Warm cache with provided keys (synchronous) 用提供的键预热缓存(同步)
      Parameters:
      keys - keys to warm | 要预热的键
      Returns:
      warming result | 预热结果
    • warm

      public CacheWarmer.WarmingResult<K> warm(Iterable<? extends K> keys, CacheWarmer.WarmingOptions options)
      Warm cache with options 使用选项预热缓存
      Parameters:
      keys - keys to warm | 要预热的键
      options - warming options | 预热选项
      Returns:
      warming result | 预热结果
    • warmAsync

      public CompletableFuture<CacheWarmer.WarmingResult<K>> warmAsync(Iterable<? extends K> keys, CacheWarmer.ProgressCallback<K> progressCallback)
      Warm cache asynchronously 异步预热缓存
      Parameters:
      keys - keys to warm | 要预热的键
      progressCallback - callback for progress updates | 进度更新回调
      Returns:
      future with result | 带结果的 Future
    • scheduleWarming

      public ScheduledFuture<?> scheduleWarming(Duration interval, Supplier<Iterable<? extends K>> keySupplier)
      Schedule periodic warming 安排定期预热
      Parameters:
      interval - warming interval | 预热间隔
      keySupplier - supplier for keys to warm | 要预热的键的供应者
      Returns:
      scheduled task handle | 定时任务句柄
    • scheduleWarming

      public ScheduledFuture<?> scheduleWarming(Duration initialDelay, Duration interval, Supplier<Iterable<? extends K>> keySupplier)
      Schedule warming with cron-like expression 使用类似 cron 的表达式安排预热
      Parameters:
      initialDelay - initial delay | 初始延迟
      interval - interval between runs | 运行间隔
      keySupplier - supplier for keys | 键的供应者
      Returns:
      scheduled task handle | 定时任务句柄
    • warmWithPriority

      public CacheWarmer.WarmingResult<K> warmWithPriority(List<CacheWarmer.PriorityKey<K>> priorityKeys)
      Warm with priority queue (high priority keys first) 使用优先级队列预热(先预热高优先级键)
      Parameters:
      priorityKeys - keys with priorities | 带优先级的键
      Returns:
      warming result | 预热结果
    • getStats

      public CacheWarmer.WarmingStats getStats()
      Get warming statistics 获取预热统计
      Returns:
      statistics | 统计
    • resetStats

      public void resetStats()
      Reset statistics 重置统计
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable