Class CompiledExpressionCache
java.lang.Object
cloud.opencode.base.expression.compiler.CompiledExpressionCache
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordCache statistics 缓存统计信息 -
Constructor Summary
ConstructorsConstructorDescriptionCreate cache with default size (1000) 使用默认大小(1000)创建缓存CompiledExpressionCache(int maxSize) Create cache with specified max size 使用指定最大大小创建缓存 -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Clear the cache 清除缓存booleanCheck if expression is cached 检查表达式是否已缓存static CompiledExpressionCachecreate(int maxSize) Create a new cache 创建新缓存Get compiled expression 获取编译后的表达式getOrCompile(String expression, Function<String, CompiledExpression> compiler) Get or compile expression 获取或编译表达式getStats()Get cache stats 获取缓存统计信息static CompiledExpressionCacheglobal()Get the global cache instance 获取全局缓存实例intmaxSize()Get max cache size 获取最大缓存大小voidput(String expression, CompiledExpression compiled) Put compiled expression in cache 将编译后的表达式放入缓存voidRemove expression from cache 从缓存中移除表达式intsize()Get current cache size 获取当前缓存大小
-
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
Get the global cache instance 获取全局缓存实例- Returns:
- the global cache | 全局缓存
-
create
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
Get compiled expression 获取编译后的表达式- Parameters:
expression- the expression string | 表达式字符串- Returns:
- the compiled expression, or null if not cached | 编译后的表达式,如果未缓存则为 null
-
put
Put compiled expression in cache 将编译后的表达式放入缓存- Parameters:
expression- the expression string | 表达式字符串compiled- the compiled expression | 编译后的表达式
-
contains
Check if expression is cached 检查表达式是否已缓存- Parameters:
expression- the expression string | 表达式字符串- Returns:
- true if cached | 如果已缓存返回 true
-
remove
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 | 统计信息
-