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

public class WriteCoalescer<K,V> extends Object implements 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:
  • Method Details

    • builder

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

      public void write(K key, V value)
      Queue a write operation 队列写操作
      Parameters:
      key - the key | 键
      value - the value | 值
    • writeAll

      public void writeAll(Map<? extends K, ? extends V> entries)
      Queue multiple write operations 队列多个写操作
      Parameters:
      entries - entries to write | 要写的条目
    • delete

      public void delete(K key)
      Queue a delete operation 队列删除操作
      Parameters:
      key - the key to delete | 要删除的键
    • flush

      public int flush()
      Flush all pending writes 刷新所有待处理的写操作
      Returns:
      number of entries flushed | 刷新的条目数
    • getPending

      public V getPending(K key)
      Get pending write for a key 获取键的待处理写操作
      Parameters:
      key - the key | 键
      Returns:
      pending value or null | 待处理的值或 null
    • hasPending

      public boolean hasPending(K key)
      Check if key has pending write 检查键是否有待处理的写操作
      Parameters:
      key - the key | 键
      Returns:
      true if pending | 如果待处理返回 true
    • getAllPending

      public Map<K,V> getAllPending()
      Get all pending writes 获取所有待处理的写操作
      Returns:
      map of pending writes | 待处理写操作的映射
    • pendingCount

      public int pendingCount()
      Get number of pending writes 获取待处理写操作的数量
      Returns:
      pending count | 待处理计数
    • getStats

      public WriteCoalescer.CoalescerStats getStats()
      Get coalescer statistics 获取合并器统计
      Returns:
      statistics | 统计
    • resetStats

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

      public void close()
      Close the coalescer, flushing pending writes 关闭合并器,刷新待处理的写操作
      Specified by:
      close in interface AutoCloseable
    • isClosed

      public boolean isClosed()
      Check if coalescer is closed 检查合并器是否已关闭
      Returns:
      true if closed | 如果已关闭返回 true