Class CompiledExpressionCache

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

public class CompiledExpressionCache extends Object
Compiled Expression Cache 编译表达式缓存

Caches compiled expressions for improved performance. Uses LRU-style eviction when the cache reaches its maximum size.

缓存编译后的表达式以提高性能。当缓存达到最大容量时使用 LRU 式淘汰。

Usage | 用法

CompiledExpressionCache cache = new CompiledExpressionCache(1000);

// Get or compile expression
CompiledExpression expr = cache.getOrCompile("a + b", str -> CompiledExpression.compile(str));

Features | 主要功能:

  • LRU-style eviction when capacity is reached - 达到容量时LRU式淘汰
  • Thread-safe with ConcurrentHashMap and ReentrantLock - 使用ConcurrentHashMap和ReentrantLock实现线程安全
  • Global singleton instance and custom instances - 全局单例实例和自定义实例
  • Cache statistics with utilization percentage - 缓存统计信息与使用率百分比

Security | 安全性:

  • Thread-safe: Yes, ConcurrentHashMap with ReentrantLock for writes - 线程安全: 是,ConcurrentHashMap配合ReentrantLock写入
  • Null-safe: No, null keys not supported - 空值安全: 否,不支持null键

Usage Examples | 使用示例:

CompiledExpressionCache cache = new CompiledExpressionCache(500);
CompiledExpression expr = cache.getOrCompile("a + b", CompiledExpression::compile);

// Global singleton
CompiledExpression expr2 = CompiledExpressionCache.global().getOrCompile("x * y", CompiledExpression::compile);
Since:
JDK 25, opencode-base-expression V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • CompiledExpressionCache

      public CompiledExpressionCache()
      Create cache with default size (1000) 使用默认大小(1000)创建缓存
    • CompiledExpressionCache

      public CompiledExpressionCache(int maxSize)
      Create cache with specified max size 使用指定最大大小创建缓存
      Parameters:
      maxSize - the maximum cache size | 最大缓存大小
  • Method Details

    • global

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

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

      public CompiledExpression getOrCompile(String expression, Function<String, CompiledExpression> compiler)
      Get or compile expression 获取或编译表达式
      Parameters:
      expression - the expression string | 表达式字符串
      compiler - the compiler function | 编译器函数
      Returns:
      the compiled expression | 编译后的表达式
    • get

      public CompiledExpression get(String expression)
      Get compiled expression 获取编译后的表达式
      Parameters:
      expression - the expression string | 表达式字符串
      Returns:
      the compiled expression, or null if not cached | 编译后的表达式,如果未缓存则为 null
    • 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 current cache size 获取当前缓存大小
      Returns:
      the size | 大小
    • maxSize

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

      Get cache stats 获取缓存统计信息
      Returns:
      the stats | 统计信息