Class CacheConfig.Builder<K,V>

java.lang.Object
cloud.opencode.base.cache.config.CacheConfig.Builder<K,V>
Type Parameters:
K - key type | 键类型
V - value type | 值类型
Enclosing class:
CacheConfig<K,V>

public static final class CacheConfig.Builder<K,V> extends Object
Cache Configuration Builder 缓存配置构建器
Since:
JDK 25, opencode-base-cache V1.0.0
Author:
Leon Soo www.LeonSoo.com
  • Constructor Details

    • Builder

      public Builder()
      Creates a new Builder instance | 创建新的构建器实例
  • Method Details

    • maximumSize

      public CacheConfig.Builder<K,V> maximumSize(long size)
      Set maximum entry count 设置最大条目数

      Note: Setting maximumSize automatically clears maximumWeight to avoid conflicts. Use one or the other.

      Parameters:
      size - maximum size | 最大容量
      Returns:
      this builder | 此构建器
    • maximumWeight

      public CacheConfig.Builder<K,V> maximumWeight(long weight)
      Set maximum weight 设置最大权重

      Note: Setting maximumWeight automatically clears maximumSize to avoid conflicts. Use one or the other.

      Parameters:
      weight - maximum weight | 最大权重
      Returns:
      this builder | 此构建器
    • expireAfterWrite

      public CacheConfig.Builder<K,V> expireAfterWrite(Duration duration)
      Set expiration after write (TTL) 设置写入后过期时间(TTL)
      Parameters:
      duration - expiration duration | 过期时长
      Returns:
      this builder | 此构建器
    • expireAfterAccess

      public CacheConfig.Builder<K,V> expireAfterAccess(Duration duration)
      Set expiration after access (TTI) 设置访问后过期时间(TTI)
      Parameters:
      duration - expiration duration | 过期时长
      Returns:
      this builder | 此构建器
    • refreshAfterWrite

      public CacheConfig.Builder<K,V> refreshAfterWrite(Duration duration)
      Set refresh after write duration 设置写入后刷新时间
      Parameters:
      duration - refresh duration | 刷新时长
      Returns:
      this builder | 此构建器
    • evictionPolicy

      public CacheConfig.Builder<K,V> evictionPolicy(EvictionPolicy<K,V> policy)
      Set eviction policy 设置淘汰策略
      Parameters:
      policy - eviction policy | 淘汰策略
      Returns:
      this builder | 此构建器
    • expiryPolicy

      public CacheConfig.Builder<K,V> expiryPolicy(ExpiryPolicy<K,V> policy)
      Set expiry policy 设置过期策略
      Parameters:
      policy - expiry policy | 过期策略
      Returns:
      this builder | 此构建器
    • loader

      public CacheConfig.Builder<K,V> loader(CacheLoader<K,V> loader)
      Set cache loader 设置缓存加载器
      Parameters:
      loader - cache loader | 缓存加载器
      Returns:
      this builder | 此构建器
    • loader

      public CacheConfig.Builder<K,V> loader(Function<K,V> loadFunc)
      Set cache loader from function 从函数设置缓存加载器
      Parameters:
      loadFunc - load function | 加载函数
      Returns:
      this builder | 此构建器
    • removalListener

      public CacheConfig.Builder<K,V> removalListener(RemovalListener<K,V> listener)
      Set removal listener 设置移除监听器
      Parameters:
      listener - removal listener | 移除监听器
      Returns:
      this builder | 此构建器
    • recordStats

      public CacheConfig.Builder<K,V> recordStats()
      Enable statistics recording 启用统计记录
      Returns:
      this builder | 此构建器
    • useVirtualThreads

      public CacheConfig.Builder<K,V> useVirtualThreads()
      Enable virtual threads for async operations 启用虚拟线程用于异步操作
      Returns:
      this builder | 此构建器
    • executor

      public CacheConfig.Builder<K,V> executor(Executor executor)
      Set custom executor for async operations 设置自定义执行器用于异步操作
      Parameters:
      executor - the executor | 执行器
      Returns:
      this builder | 此构建器
    • concurrencyLevel

      public CacheConfig.Builder<K,V> concurrencyLevel(int level)
      Set concurrency level 设置并发级别
      Parameters:
      level - concurrency level | 并发级别
      Returns:
      this builder | 此构建器
    • initialCapacity

      public CacheConfig.Builder<K,V> initialCapacity(int capacity)
      Set initial capacity 设置初始容量
      Parameters:
      capacity - initial capacity | 初始容量
      Returns:
      this builder | 此构建器
    • refreshAheadPolicy

      public CacheConfig.Builder<K,V> refreshAheadPolicy(RefreshAheadPolicy<K,V> policy)
      Set refresh ahead policy for proactive cache refresh 设置提前刷新策略用于主动缓存刷新
      Parameters:
      policy - refresh ahead policy | 提前刷新策略
      Returns:
      this builder | 此构建器
    • weigher

      public CacheConfig.Builder<K,V> weigher(ValueWeigher<V> weigher)
      Set value weigher for memory-based eviction 设置值权重计算器用于基于内存的淘汰
      Parameters:
      weigher - value weigher | 值权重计算器
      Returns:
      this builder | 此构建器
    • build

      public CacheConfig<K,V> build()
      Build the configuration 构建配置
      Returns:
      cache config | 缓存配置
      Throws:
      IllegalArgumentException - if configuration is invalid | 配置无效时抛出异常
    • validate

      public void validate()
      Validate the configuration 验证配置
      Throws:
      IllegalArgumentException - if configuration is invalid | 配置无效时抛出异常
      Since:
      V2.0.2
    • diagnose

      public List<String> diagnose()
      Get validation diagnostics without throwing exceptions 获取验证诊断信息,不抛出异常
      Returns:
      list of validation issues | 验证问题列表
      Since:
      V2.0.4