Class ExpressionCache

java.lang.Object
cloud.opencode.base.expression.compiler.ExpressionCache

public final class ExpressionCache extends Object
Expression Cache 表达式缓存

Caches compiled expressions for improved performance.

缓存编译后的表达式以提高性能。

Features | 主要功能:

  • Auto-compile on cache miss - 缓存未命中时自动编译
  • Quarter-size batch eviction strategy - 四分之一大小批量淘汰策略
  • Thread-safe with ConcurrentHashMap - 使用ConcurrentHashMap实现线程安全
  • Global singleton instance - 全局单例实例

Usage Examples | 使用示例:

ExpressionCache cache = ExpressionCache.create(500);
CompiledExpression expr = cache.get("a + b");  // auto-compiles if not cached
boolean cached = cache.contains("a + b");  // true

Security | 安全性:

  • Thread-safe: Yes, ConcurrentHashMap with putIfAbsent - 线程安全: 是,ConcurrentHashMap配合putIfAbsent
  • Null-safe: No, null expression not supported - 空值安全: 否,不支持null表达式
Since:
JDK 25, opencode-base-expression V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • ExpressionCache

      public ExpressionCache(int maxSize)
      Create cache with max size 创建具有最大容量的缓存
      Parameters:
      maxSize - the maximum cache size | 最大缓存大小
  • Method Details

    • global

      public static ExpressionCache global()
      Get the global cache instance 获取全局缓存实例
      Returns:
      the global cache | 全局缓存
    • create

      public static ExpressionCache create(int maxSize)
      Create a new cache 创建新缓存
      Parameters:
      maxSize - the maximum size | 最大大小
      Returns:
      the cache | 缓存
    • get

      public CompiledExpression get(String expression)
      Get or compile expression 获取或编译表达式
      Parameters:
      expression - the expression string | 表达式字符串
      Returns:
      the compiled expression | 编译后的表达式
    • put

      public void put(String expression, CompiledExpression compiled)
      Put compiled expression in cache 将编译后的表达式放入缓存
      Parameters:
      expression - the expression string | 表达式字符串
      compiled - the compiled expression | 编译后的表达式
    • contains

      public boolean contains(String expression)
      Check if expression is cached 检查表达式是否已缓存
      Parameters:
      expression - the expression string | 表达式字符串
      Returns:
      true if cached | 如果已缓存返回true
    • remove

      public void remove(String expression)
      Remove expression from cache 从缓存中移除表达式
      Parameters:
      expression - the expression string | 表达式字符串
    • clear

      public void clear()
      Clear the cache 清除缓存
    • size

      public int size()
      Get cache size 获取缓存大小
      Returns:
      the current size | 当前大小
    • maxSize

      public int maxSize()
      Get max cache size 获取最大缓存大小
      Returns:
      the max size | 最大大小