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
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder for CacheWarmer CacheWarmer 构建器static final recordKey with priority for priority-based warming 用于基于优先级预热的带优先级的键static interfaceProgress callback interface 进度回调接口static final recordProgress snapshot for callbacks 用于回调的进度快照static interfaceWarming event handler 预热事件处理器static final recordWarming options 预热选项static final recordWarming result 预热结果static final recordWarming statistics 预热统计 -
Method Summary
Modifier and TypeMethodDescriptionstatic <K,V> CacheWarmer.Builder <K, V> builder()Create a builder 创建构建器voidclose()getStats()Get warming statistics 获取预热统计voidReset statistics 重置统计scheduleWarming(Duration initialDelay, Duration interval, Supplier<Iterable<? extends K>> keySupplier) Schedule warming with cron-like expression 使用类似 cron 的表达式安排预热scheduleWarming(Duration interval, Supplier<Iterable<? extends K>> keySupplier) Schedule periodic warming 安排定期预热Warm cache with provided keys (synchronous) 用提供的键预热缓存(同步)warm(Iterable<? extends K> keys, CacheWarmer.WarmingOptions options) Warm cache with options 使用选项预热缓存warmAsync(Iterable<? extends K> keys, CacheWarmer.ProgressCallback<K> progressCallback) Warm cache asynchronously 异步预热缓存warmWithPriority(List<CacheWarmer.PriorityKey<K>> priorityKeys) Warm with priority queue (high priority keys first) 使用优先级队列预热(先预热高优先级键)
-
Method Details
-
builder
Create a builder 创建构建器- Type Parameters:
K- key type | 键类型V- value type | 值类型- Returns:
- builder | 构建器
-
warm
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
Warm with priority queue (high priority keys first) 使用优先级队列预热(先预热高优先级键)- Parameters:
priorityKeys- keys with priorities | 带优先级的键- Returns:
- warming result | 预热结果
-
getStats
-
resetStats
public void resetStats()Reset statistics 重置统计 -
close
public void close()- Specified by:
closein interfaceAutoCloseable
-