Class CacheSpec

java.lang.Object
cloud.opencode.base.cache.config.CacheSpec

public final class CacheSpec extends Object
CacheSpec - Parse cache configuration from a specification string CacheSpec - 从规范字符串解析缓存配置

Parses a comma-separated list of key=value pairs into a CacheConfig. This is inspired by Caffeine's CacheSpec and provides a convenient way to configure caches from configuration files or system properties.

解析逗号分隔的 key=value 对列表为 CacheConfig。 灵感来自 Caffeine 的 CacheSpec,提供从配置文件或系统属性配置缓存的便捷方式。

Supported Options | 支持的选项:

  • maximumSize=<long> - Maximum number of entries | 最大条目数
  • maximumWeight=<long> - Maximum total weight | 最大总权重
  • initialCapacity=<int> - Initial capacity | 初始容量
  • concurrencyLevel=<int> - Concurrency level | 并发级别
  • expireAfterWrite=<duration> - TTL expiration | 写入后过期
  • expireAfterAccess=<duration> - TTI expiration | 访问后过期
  • refreshAfterWrite=<duration> - Refresh interval | 刷新间隔
  • evictionPolicy=<lru|lfu|fifo|wtinylfu> - Eviction policy | 淘汰策略
  • recordStats - Enable statistics (no value needed) | 启用统计
  • useVirtualThreads - Enable virtual threads | 启用虚拟线程

Duration Format | 时间格式:

  • 100 or 100ms - milliseconds | 毫秒
  • 10s - seconds | 秒
  • 5m - minutes | 分钟
  • 2h - hours | 小时
  • 1d - days | 天

Usage Examples | 使用示例:

// Parse specification string | 解析规范字符串
CacheConfig<String, User> config = CacheSpec.parse(
    "maximumSize=10000,expireAfterWrite=30m,recordStats");

// Use with OpenCache | 与 OpenCache 一起使用
Cache<String, User> cache = OpenCache.fromSpec("users",
    "maximumSize=10000,expireAfterWrite=30m,expireAfterAccess=10m");

// From properties file | 从配置文件
String spec = properties.getProperty("cache.users.spec");
Cache<String, User> cache = OpenCache.fromSpec("users", spec);

// Complex configuration | 复杂配置
String spec = "maximumSize=50000,expireAfterWrite=1h,expireAfterAccess=30m," +
              "initialCapacity=1000,concurrencyLevel=32,evictionPolicy=wtinylfu," +
              "recordStats,useVirtualThreads";
CacheConfig<K, V> config = CacheSpec.parse(spec);

Thread Safety | 线程安全:

This class is immutable and thread-safe.

此类是不可变的且线程安全。

Features | 主要功能:

  • String-based cache configuration - 字符串缓存配置
  • Duration parsing (ms, s, m, h, d) - 时间解析
  • Validation support - 验证支持
  • Properties file compatibility - 属性文件兼容

Security | 安全性:

  • Thread-safe: Yes (stateless utility) - 线程安全: 是(无状态工具类)
  • Null-safe: Yes - 空值安全: 是
Since:
JDK 25, opencode-base-cache V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • parse

      public static <K,V> CacheConfig<K,V> parse(String spec)
      Parse a specification string into a CacheConfig 解析规范字符串为 CacheConfig
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      spec - the specification string | 规范字符串
      Returns:
      parsed CacheConfig | 解析后的 CacheConfig
      Throws:
      OpenCacheException - if the specification is invalid | 规范无效时抛出异常
    • parseToBuilder

      public static <K,V> CacheConfig.Builder<K,V> parseToBuilder(String spec)
      Parse a specification string into a CacheConfig.Builder 解析规范字符串为 CacheConfig.Builder

      This allows further customization after parsing.

      这允许解析后进一步自定义。

      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      spec - the specification string | 规范字符串
      Returns:
      configured builder | 配置好的构建器
      Throws:
      OpenCacheException - if the specification is invalid | 规范无效时抛出异常
    • isValid

      public static boolean isValid(String spec)
      Validate a specification string without parsing 验证规范字符串而不解析
      Parameters:
      spec - the specification string | 规范字符串
      Returns:
      true if valid, false otherwise | 有效返回 true
    • validate

      public static List<String> validate(String spec)
      Get validation errors for a specification string 获取规范字符串的验证错误
      Parameters:
      spec - the specification string | 规范字符串
      Returns:
      list of error messages, empty if valid | 错误消息列表,有效时为空
    • toSpec

      public static <K,V> String toSpec(CacheConfig<K,V> config)
      Build a specification string from a CacheConfig 从 CacheConfig 构建规范字符串
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      config - the cache config | 缓存配置
      Returns:
      specification string | 规范字符串