Class WriteCoalescer<K,V>
java.lang.Object
cloud.opencode.base.cache.write.WriteCoalescer<K,V>
- Type Parameters:
K- key type | 键类型V- value type | 值类型Security | 安全性:
- Thread-safe: Yes - 线程安全: 是
- Null-safe: Partial (null values not allowed) - 空值安全: 部分(不允许 null 值)
- All Implemented Interfaces:
AutoCloseable
Write Coalescer - Batches and coalesces write operations
写合并器 - 批处理和合并写操作
Aggregates multiple write operations into batches for efficient backend persistence. Supports deduplication, batching by time/count, and async write queuing.
将多个写操作聚合为批次以进行高效的后端持久化。 支持去重、按时间/计数批处理和异步写队列。
Features | 主要功能:
- Batch write aggregation - 批量写聚合
- Write deduplication - 写去重
- Time-based flushing - 基于时间的刷新
- Count-based flushing - 基于计数的刷新
- Async write queue - 异步写队列
Usage Examples | 使用示例:
// Create write coalescer
WriteCoalescer<String, User> coalescer = WriteCoalescer.<String, User>builder()
.batchSize(100)
.flushInterval(Duration.ofSeconds(5))
.writer(batch -> userRepository.saveAll(batch))
.build();
// Queue writes
coalescer.write("user:1001", user1);
coalescer.write("user:1002", user2);
coalescer.write("user:1001", user1Updated); // Deduplicates
// Flush manually if needed
coalescer.flush();
// Close when done
coalescer.close();
- Since:
- JDK 25, opencode-base-cache V2.0.5
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceBatch writer interface 批量写入器接口static classBuilder for WriteCoalescer WriteCoalescer 构建器static final recordCoalescer statistics 合并器统计static interfaceWrite error handler 写错误处理器 -
Method Summary
Modifier and TypeMethodDescriptionstatic <K,V> WriteCoalescer.Builder <K, V> builder()Create a builder 创建构建器voidclose()Close the coalescer, flushing pending writes 关闭合并器,刷新待处理的写操作voidQueue a delete operation 队列删除操作intflush()Flush all pending writes 刷新所有待处理的写操作Get all pending writes 获取所有待处理的写操作getPending(K key) Get pending write for a key 获取键的待处理写操作getStats()Get coalescer statistics 获取合并器统计booleanhasPending(K key) Check if key has pending write 检查键是否有待处理的写操作booleanisClosed()Check if coalescer is closed 检查合并器是否已关闭intGet number of pending writes 获取待处理写操作的数量voidReset statistics 重置统计voidQueue a write operation 队列写操作voidQueue multiple write operations 队列多个写操作
-
Method Details
-
builder
Create a builder 创建构建器- Type Parameters:
K- key type | 键类型V- value type | 值类型- Returns:
- builder | 构建器
-
write
-
writeAll
-
delete
Queue a delete operation 队列删除操作- Parameters:
key- the key to delete | 要删除的键
-
flush
public int flush()Flush all pending writes 刷新所有待处理的写操作- Returns:
- number of entries flushed | 刷新的条目数
-
getPending
-
hasPending
Check if key has pending write 检查键是否有待处理的写操作- Parameters:
key- the key | 键- Returns:
- true if pending | 如果待处理返回 true
-
getAllPending
-
pendingCount
public int pendingCount()Get number of pending writes 获取待处理写操作的数量- Returns:
- pending count | 待处理计数
-
getStats
Get coalescer statistics 获取合并器统计- Returns:
- statistics | 统计
-
resetStats
public void resetStats()Reset statistics 重置统计 -
close
public void close()Close the coalescer, flushing pending writes 关闭合并器,刷新待处理的写操作- Specified by:
closein interfaceAutoCloseable
-
isClosed
public boolean isClosed()Check if coalescer is closed 检查合并器是否已关闭- Returns:
- true if closed | 如果已关闭返回 true
-