Class OpenCache
java.lang.Object
cloud.opencode.base.cache.OpenCache
OpenCache Facade - Unified entry point for cache operations
OpenCache 门面 - 缓存操作的统一入口
Provides convenient static methods for cache creation and management.
提供便捷的静态方法用于缓存创建和管理。
Features | 主要功能:
- Cache creation with fluent API - 流式 API 创建缓存
- Named cache management - 命名缓存管理
- Policy factory methods - 策略工厂方法
- Global statistics - 全局统计
Usage Examples | 使用示例:
// Create cache with configuration - 使用配置创建缓存
Cache<String, User> cache = OpenCache.getOrCreate("users", config -> config
.maximumSize(10000)
.expireAfterWrite(Duration.ofMinutes(30))
.evictionPolicy(OpenCache.lru())
.recordStats());
// Build cache directly - 直接构建缓存
Cache<String, User> cache = OpenCache.<String, User>builder()
.maximumSize(10000)
.expireAfterWrite(Duration.ofMinutes(30))
.build("myCache");
// Use policy factories - 使用策略工厂
EvictionPolicy<K, V> policy = OpenCache.wTinyLfu();
ExpiryPolicy<K, V> expiry = OpenCache.ttl(Duration.ofHours(1));
Security | 安全性:
- Thread-safe: Yes - 线程安全: 是
- Null-safe: Yes - 空值安全: 是
- Since:
- JDK 25, opencode-base-cache V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classDeprecated, for removal: This API element is subject to removal in a future version. -
Method Summary
Modifier and TypeMethodDescriptionstatic <K,V> OpenCache.CacheBuilder <K, V> builder()Create cache configuration builder 创建缓存配置构建器static voidClean up all caches 清理所有缓存static <K,V> ExpiryPolicy <K, V> Create combined TTL and TTI expiry policy 创建组合 TTL 和 TTI 过期策略static <K,V> CacheDecorators.ChainBuilder <K, V> Start a decorator chain for the given cache 为给定的缓存开始装饰器链static <K,V> EvictionPolicy <K, V> fifo()Create FIFO eviction policy 创建 FIFO 淘汰策略static <K,V> Cache <K, V> fifoCache(long maxSize) Create a simple FIFO cache with given maximum size 创建具有给定最大容量的简单 FIFO 缓存static <K,V> Cache <K, V> Create anonymous cache from specification string 从规范字符串创建匿名缓存static <K,V> Cache <K, V> Create cache from specification string 从规范字符串创建缓存Get existing cache by name 按名称获取已存在的缓存static <K,V> Cache <K, V> getOrCreate(String name) Get or create named cache 获取或创建命名缓存static <K,V> Cache <K, V> getOrCreate(String name, Consumer<CacheConfig.Builder<K, V>> configurer) Get or create named cache with configuration 获取或创建带配置的命名缓存static <K,V> Optional <V> Get value from named cache 从命名缓存中获取值static <K,V> void invalidate(String name, K key) Invalidate a single key in named cache 使命名缓存中的单个键失效static voidinvalidateAll(String name) Invalidate all entries in named cache 使命名缓存中的所有条目失效static booleanCheck if named cache is empty 检查命名缓存是否为空static booleanisValidSpec(String spec) Validate a specification string 验证规范字符串static <K,V> EvictionPolicy <K, V> lfu()Create LFU eviction policy 创建 LFU 淘汰策略static <K,V> Cache <K, V> lfuCache(long maxSize) Create a simple LFU cache with given maximum size 创建具有给定最大容量的简单 LFU 缓存static <K,V> LoadingCache <K, V> loadingCache(String name, Function<? super K, ? extends V> loader, long maxSize) Create a loading cache with automatic value loading 创建具有自动值加载功能的加载缓存static <K,V> LoadingCache <K, V> loadingCache(String name, Function<? super K, ? extends V> loader, long maxSize, Duration duration) Create a loading cache with TTL 创建具有 TTL 的加载缓存static <K,V> EvictionPolicy <K, V> lru()Create LRU eviction policy 创建 LRU 淘汰策略static <K,V> Cache <K, V> lruCache(long maxSize) Create a simple LRU cache with given maximum size 创建具有给定最大容量的简单 LRU 缓存static <K,V> Cache <K, V> lruTtlCache(long maxSize, Duration duration) Create a cache with both LRU and TTL 创建同时具有 LRU 和 TTL 的缓存names()Get all cache names 获取所有缓存名称static <K,V> CacheConfig <K, V> Parse specification string to CacheConfig 解析规范字符串为 CacheConfigstatic <K,V> void Put value in named cache 在命名缓存中放入值static voidRemove cache by name 按名称移除缓存static voidshutdown()Shutdown cache system 关闭缓存系统static longGet size of named cache 获取命名缓存的大小static Map<String, CacheStats> stats()Get global statistics 获取全局统计static <K,V> ExpiryPolicy <K, V> Create TTI expiry policy 创建 TTI 过期策略static <K,V> Cache <K, V> Create a TTI-based cache with given size and idle expiration 创建具有给定容量和空闲过期时间的 TTI 缓存static <K,V> ExpiryPolicy <K, V> Create TTL expiry policy 创建 TTL 过期策略static <K,V> Cache <K, V> Create a TTL-based cache with given size and expiration 创建具有给定容量和过期时间的 TTL 缓存static <K,V> EvictionPolicy <K, V> wTinyLfu()Create W-TinyLFU eviction policy with default expected size (10,000) 创建默认期望大小(10,000)的 W-TinyLFU 淘汰策略static <K,V> EvictionPolicy <K, V> wTinyLfu(int expectedSize) Create W-TinyLFU eviction policy sized for the expected cache capacity 创建根据期望缓存容量调整大小的 W-TinyLFU 淘汰策略static <K,V> Cache <K, V> wTinyLfuCache(long maxSize) Create a high-performance W-TinyLFU cache 创建高性能 W-TinyLFU 缓存
-
Method Details
-
getOrCreate
-
getOrCreate
public static <K,V> Cache<K,V> getOrCreate(String name, Consumer<CacheConfig.Builder<K, V>> configurer) Get or create named cache with configuration 获取或创建带配置的命名缓存- Type Parameters:
K- key type | 键类型V- value type | 值类型- Parameters:
name- cache name | 缓存名称configurer- configuration consumer | 配置消费者- Returns:
- cache instance | 缓存实例
-
fromSpec
Create cache from specification string 从规范字符串创建缓存Parses a comma-separated specification string to configure the cache. This is convenient for loading configuration from properties files.
解析逗号分隔的规范字符串来配置缓存。便于从配置文件加载配置。
Supported options | 支持的选项:
maximumSize=<long>- Maximum entries | 最大条目数expireAfterWrite=<duration>- TTL (e.g., 30m, 1h, 1d) | TTL 过期expireAfterAccess=<duration>- TTI | TTI 过期evictionPolicy=<lru|lfu|fifo|wtinylfu>- Eviction policy | 淘汰策略recordStats- Enable statistics | 启用统计useVirtualThreads- Enable virtual threads | 启用虚拟线程
Example | 示例:
// From code | 代码中使用 Cache<String, User> cache = OpenCache.fromSpec("users", "maximumSize=10000,expireAfterWrite=30m,recordStats"); // From properties | 从配置文件 String spec = props.getProperty("cache.users.spec"); Cache<String, User> cache = OpenCache.fromSpec("users", spec);- Type Parameters:
K- key type | 键类型V- value type | 值类型- Parameters:
name- cache name | 缓存名称spec- specification string | 规范字符串- Returns:
- cache instance | 缓存实例
- Throws:
OpenCacheException- if spec is invalid | 规范无效时抛出异常- Since:
- V2.1.0
-
fromSpec
Create anonymous cache from specification string 从规范字符串创建匿名缓存- Type Parameters:
K- key type | 键类型V- value type | 值类型- Parameters:
spec- specification string | 规范字符串- Returns:
- cache instance | 缓存实例
- Throws:
OpenCacheException- if spec is invalid | 规范无效时抛出异常- Since:
- V2.1.0
-
parseSpec
Parse specification string to CacheConfig 解析规范字符串为 CacheConfig- Type Parameters:
K- key type | 键类型V- value type | 值类型- Parameters:
spec- specification string | 规范字符串- Returns:
- parsed CacheConfig | 解析后的 CacheConfig
- Throws:
OpenCacheException- if spec is invalid | 规范无效时抛出异常- Since:
- V2.1.0
-
isValidSpec
Validate a specification string 验证规范字符串- Parameters:
spec- specification string | 规范字符串- Returns:
- true if valid | 有效返回 true
- Since:
- V2.1.0
-
builder
Create cache configuration builder 创建缓存配置构建器- Type Parameters:
K- key type | 键类型V- value type | 值类型- Returns:
- configuration builder | 配置构建器
-
get
-
names
-
remove
Remove cache by name 按名称移除缓存- Parameters:
name- cache name | 缓存名称
-
cleanUpAll
public static void cleanUpAll()Clean up all caches 清理所有缓存 -
stats
Get global statistics 获取全局统计- Returns:
- map of cache name to stats | 缓存名称到统计的映射
-
shutdown
public static void shutdown()Shutdown cache system 关闭缓存系统 -
invalidate
Invalidate a single key in named cache 使命名缓存中的单个键失效- Type Parameters:
K- key type | 键类型V- value type | 值类型- Parameters:
name- cache name | 缓存名称key- key to invalidate | 要失效的键
-
invalidateAll
Invalidate all entries in named cache 使命名缓存中的所有条目失效- Parameters:
name- cache name | 缓存名称
-
isEmpty
Check if named cache is empty 检查命名缓存是否为空- Parameters:
name- cache name | 缓存名称- Returns:
- true if empty or not exists | 如果为空或不存在返回true
-
size
Get size of named cache 获取命名缓存的大小- Parameters:
name- cache name | 缓存名称- Returns:
- cache size or 0 if not exists | 缓存大小,不存在返回0
-
put
Put value in named cache 在命名缓存中放入值- Type Parameters:
K- key type | 键类型V- value type | 值类型- Parameters:
name- cache name | 缓存名称key- the key | 键value- the value | 值
-
getValue
-
lruCache
Create a simple LRU cache with given maximum size 创建具有给定最大容量的简单 LRU 缓存- Type Parameters:
K- key type | 键类型V- value type | 值类型- Parameters:
maxSize- maximum cache size | 最大缓存容量- Returns:
- LRU cache | LRU 缓存
- Since:
- V2.0.2
-
lfuCache
Create a simple LFU cache with given maximum size 创建具有给定最大容量的简单 LFU 缓存- Type Parameters:
K- key type | 键类型V- value type | 值类型- Parameters:
maxSize- maximum cache size | 最大缓存容量- Returns:
- LFU cache | LFU 缓存
- Since:
- V2.0.2
-
fifoCache
Create a simple FIFO cache with given maximum size 创建具有给定最大容量的简单 FIFO 缓存- Type Parameters:
K- key type | 键类型V- value type | 值类型- Parameters:
maxSize- maximum cache size | 最大缓存容量- Returns:
- FIFO cache | FIFO 缓存
- Since:
- V2.0.2
-
ttlCache
Create a TTL-based cache with given size and expiration 创建具有给定容量和过期时间的 TTL 缓存- Type Parameters:
K- key type | 键类型V- value type | 值类型- Parameters:
maxSize- maximum cache size | 最大缓存容量duration- time-to-live | 存活时间- Returns:
- TTL cache | TTL 缓存
- Since:
- V2.0.2
-
ttiCache
Create a TTI-based cache with given size and idle expiration 创建具有给定容量和空闲过期时间的 TTI 缓存- Type Parameters:
K- key type | 键类型V- value type | 值类型- Parameters:
maxSize- maximum cache size | 最大缓存容量duration- time-to-idle | 空闲时间- Returns:
- TTI cache | TTI 缓存
- Since:
- V2.0.2
-
wTinyLfuCache
Create a high-performance W-TinyLFU cache 创建高性能 W-TinyLFU 缓存- Type Parameters:
K- key type | 键类型V- value type | 值类型- Parameters:
maxSize- maximum cache size | 最大缓存容量- Returns:
- W-TinyLFU cache | W-TinyLFU 缓存
- Since:
- V2.0.2
-
lruTtlCache
Create a cache with both LRU and TTL 创建同时具有 LRU 和 TTL 的缓存- Type Parameters:
K- key type | 键类型V- value type | 值类型- Parameters:
maxSize- maximum cache size | 最大缓存容量duration- time-to-live | 存活时间- Returns:
- LRU+TTL cache | LRU+TTL 缓存
- Since:
- V2.0.2
-
loadingCache
public static <K,V> LoadingCache<K,V> loadingCache(String name, Function<? super K, ? extends V> loader, long maxSize) Create a loading cache with automatic value loading 创建具有自动值加载功能的加载缓存- Type Parameters:
K- key type | 键类型V- value type | 值类型- Parameters:
name- cache name | 缓存名称loader- the loader function | 加载函数maxSize- maximum cache size | 最大缓存容量- Returns:
- loading cache | 加载缓存
- Since:
- V2.0.3
-
loadingCache
public static <K,V> LoadingCache<K,V> loadingCache(String name, Function<? super K, ? extends V> loader, long maxSize, Duration duration) Create a loading cache with TTL 创建具有 TTL 的加载缓存- Type Parameters:
K- key type | 键类型V- value type | 值类型- Parameters:
name- cache name | 缓存名称loader- the loader function | 加载函数maxSize- maximum cache size | 最大缓存容量duration- time-to-live | 存活时间- Returns:
- loading cache with TTL | 具有 TTL 的加载缓存
- Since:
- V2.0.3
-
decorate
Start a decorator chain for the given cache 为给定的缓存开始装饰器链- Type Parameters:
K- key type | 键类型V- value type | 值类型- Parameters:
cache- the cache to decorate | 要装饰的缓存- Returns:
- decorator chain builder | 装饰器链构建器
- Since:
- V2.0.2
-
lru
Create LRU eviction policy 创建 LRU 淘汰策略- Type Parameters:
K- key type | 键类型V- value type | 值类型- Returns:
- LRU policy | LRU 策略
-
lfu
Create LFU eviction policy 创建 LFU 淘汰策略- Type Parameters:
K- key type | 键类型V- value type | 值类型- Returns:
- LFU policy | LFU 策略
-
fifo
Create FIFO eviction policy 创建 FIFO 淘汰策略- Type Parameters:
K- key type | 键类型V- value type | 值类型- Returns:
- FIFO policy | FIFO 策略
-
wTinyLfu
Create W-TinyLFU eviction policy with default expected size (10,000) 创建默认期望大小(10,000)的 W-TinyLFU 淘汰策略- Type Parameters:
K- key type | 键类型V- value type | 值类型- Returns:
- W-TinyLFU policy | W-TinyLFU 策略
-
wTinyLfu
Create W-TinyLFU eviction policy sized for the expected cache capacity 创建根据期望缓存容量调整大小的 W-TinyLFU 淘汰策略- Type Parameters:
K- key type | 键类型V- value type | 值类型- Parameters:
expectedSize- the expected maximum number of cache entries | 期望的缓存最大条目数- Returns:
- W-TinyLFU policy | W-TinyLFU 策略
-
ttl
Create TTL expiry policy 创建 TTL 过期策略- Type Parameters:
K- key type | 键类型V- value type | 值类型- Parameters:
duration- expiration duration | 过期时长- Returns:
- TTL policy | TTL 策略
-
tti
Create TTI expiry policy 创建 TTI 过期策略- Type Parameters:
K- key type | 键类型V- value type | 值类型- Parameters:
duration- expiration duration | 过期时长- Returns:
- TTI policy | TTI 策略
-
combined
Create combined TTL and TTI expiry policy 创建组合 TTL 和 TTI 过期策略- Type Parameters:
K- key type | 键类型V- value type | 值类型- Parameters:
ttl- TTL duration | TTL 时长tti- TTI duration | TTI 时长- Returns:
- combined policy | 组合策略
-
CacheConfig.Builderinstead for full configuration capabilities.