Class CacheConfig<K,V>

java.lang.Object
cloud.opencode.base.cache.config.CacheConfig<K,V>
Type Parameters:
K - the type of keys | 键类型
V - the type of values | 值类型

public final class CacheConfig<K,V> extends Object
Cache Configuration - Immutable cache configuration with Builder pattern 缓存配置 - 不可变的缓存配置,使用 Builder 模式

Provides comprehensive cache configuration options including size limits, expiration policies, eviction policies, and async execution settings.

提供全面的缓存配置选项,包括容量限制、过期策略、淘汰策略和异步执行设置。

Features | 主要功能:

  • Size/Weight limits - 容量/权重限制
  • TTL/TTI expiration - TTL/TTI 过期
  • Eviction policies (LRU/LFU/FIFO/W-TinyLFU) - 淘汰策略
  • Removal listener - 移除监听器
  • Statistics recording - 统计记录
  • Virtual thread support - 虚拟线程支持

Usage Examples | 使用示例:

CacheConfig<String, User> config = CacheConfig.<String, User>builder()
    .maximumSize(10000)
    .expireAfterWrite(Duration.ofMinutes(30))
    .expireAfterAccess(Duration.ofMinutes(10))
    .recordStats()
    .build();

Security | 安全性:

  • Thread-safe: Yes (immutable) - 线程安全: 是(不可变)
  • Null-safe: Yes - 空值安全: 是
Since:
JDK 25, opencode-base-cache V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • maximumSize

      public long maximumSize()
      Returns the maximum size | 返回最大大小
      Returns:
      maximum size | 最大大小
    • maximumWeight

      public long maximumWeight()
      Returns the maximum weight | 返回最大权重
      Returns:
      maximum weight | 最大权重
    • expireAfterWrite

      public Duration expireAfterWrite()
      Returns the expire-after-write duration | 返回写入后过期时间
      Returns:
      expire-after-write | 写入后过期时间
    • expireAfterAccess

      public Duration expireAfterAccess()
      Returns the expire-after-access duration | 返回访问后过期时间
      Returns:
      expire-after-access | 访问后过期时间
    • refreshAfterWrite

      public Duration refreshAfterWrite()
      Returns the refresh-after-write duration | 返回写入后刷新时间
      Returns:
      refresh-after-write | 写入后刷新时间
    • evictionPolicy

      public EvictionPolicy<K,V> evictionPolicy()
      Returns the eviction policy | 返回淘汰策略
      Returns:
      eviction policy | 淘汰策略
    • expiryPolicy

      public ExpiryPolicy<K,V> expiryPolicy()
      Returns the expiry policy | 返回过期策略
      Returns:
      expiry policy | 过期策略
    • loader

      public CacheLoader<K,V> loader()
      Returns the cache loader | 返回缓存加载器
      Returns:
      cache loader | 缓存加载器
    • removalListener

      public RemovalListener<K,V> removalListener()
      Returns the removal listener | 返回移除监听器
      Returns:
      removal listener | 移除监听器
    • recordStats

      public boolean recordStats()
      Returns whether stats recording is enabled | 返回是否启用统计记录
      Returns:
      true if stats enabled | 启用统计返回 true
    • useVirtualThreads

      public boolean useVirtualThreads()
      Returns whether virtual threads are used | 返回是否使用虚拟线程
      Returns:
      true if virtual threads | 使用虚拟线程返回 true
    • executor

      public Executor executor()
      Returns the executor | 返回执行器
      Returns:
      executor | 执行器
    • concurrencyLevel

      public int concurrencyLevel()
      Returns the concurrency level | 返回并发级别
      Returns:
      concurrency level | 并发级别
    • initialCapacity

      public int initialCapacity()
      Returns the initial capacity | 返回初始容量
      Returns:
      initial capacity | 初始容量
    • refreshAheadPolicy

      public RefreshAheadPolicy<K,V> refreshAheadPolicy()
      Get refresh ahead policy 获取提前刷新策略
      Returns:
      refresh ahead policy | 提前刷新策略
    • weigher

      public ValueWeigher<V> weigher()
      Get value weigher 获取值权重计算器
      Returns:
      value weigher | 值权重计算器
    • hasExpiration

      public boolean hasExpiration()
      Check if expiration is configured 检查是否配置了过期
      Returns:
      true if expiration configured | 配置了过期返回 true
    • hasSizeLimit

      public boolean hasSizeLimit()
      Check if size limit is configured 检查是否配置了容量限制
      Returns:
      true if size limit configured | 配置了容量限制返回 true
    • builder

      public static <K,V> CacheConfig.Builder<K,V> builder()
      Create new builder 创建新的构建器
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Returns:
      new builder | 新构建器
    • defaultConfig

      public static <K,V> CacheConfig<K,V> defaultConfig()
      Create default configuration 创建默认配置
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Returns:
      default config | 默认配置