Class CacheManager
java.lang.Object
cloud.opencode.base.cache.CacheManager
Cache Manager - Global cache instance management
缓存管理器 - 全局缓存实例管理
Singleton manager for creating, retrieving, and managing cache instances.
用于创建、获取和管理缓存实例的单例管理器。
Features | 主要功能:
- Cache creation and registration - 缓存创建和注册
- Named cache retrieval - 命名缓存获取
- Global statistics - 全局统计
- Bulk operations (cleanUp, shutdown) - 批量操作(清理、关闭)
Usage Examples | 使用示例:
CacheManager manager = CacheManager.getInstance();
// Create or get cache - 创建或获取缓存
Cache<String, User> cache = manager.getOrCreateCache("users", config -> config
.maximumSize(10000)
.expireAfterWrite(Duration.ofMinutes(30)));
// Get existing cache - 获取已存在的缓存
Optional<Cache<String, User>> existing = manager.getCache("users");
// Get all cache names - 获取所有缓存名称
Set<String> names = manager.getCacheNames();
Security | 安全性:
- Thread-safe: Yes - 线程安全: 是
- Null-safe: Yes - 空值安全: 是
- Since:
- JDK 25, opencode-base-cache V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionvoidClean up all caches 清理所有缓存<K,V> Cache <K, V> createCache(String name, CacheConfig<K, V> config) Create cache with configuration (always creates new) 创建带配置的缓存(总是创建新的)Get statistics for all caches 获取所有缓存的统计信息Get existing cache 获取已存在的缓存Get all cache names 获取所有缓存名称getCachesByPattern(String pattern) Get caches matching a name pattern (supports * and ?getCacheStats(String name) Get statistics for a specific cache by name 按名称获取特定缓存的统计信息Get combined statistics 获取合并的统计信息static CacheManagerGet singleton instance 获取单例实例<K,V> Cache <K, V> getOrCreateCache(String name) Get or create cache with default configuration 获取或创建默认配置的缓存<K,V> Cache <K, V> getOrCreateCache(String name, Consumer<CacheConfig.Builder<K, V>> configurer) Get or create cache with configuration 获取或创建带配置的缓存voidInvalidate all entries in all caches 使所有缓存中的所有条目失效intinvalidateByPattern(String pattern) Invalidate all entries in caches matching a name pattern 使匹配名称模式的缓存中的所有条目失效booleanCheck if manager is shut down 检查管理器是否已关闭voidremoveCache(String name) Remove cache by name 按名称移除缓存voidreset()Reset manager (for testing) 重置管理器(用于测试)voidshutdown()Shutdown the cache manager, closing all managed caches and releasing resources.
-
Method Details
-
getInstance
Get singleton instance 获取单例实例- Returns:
- cache manager instance | 缓存管理器实例
-
getOrCreateCache
public <K,V> Cache<K,V> getOrCreateCache(String name, Consumer<CacheConfig.Builder<K, V>> configurer) Get or create cache with configuration 获取或创建带配置的缓存- Type Parameters:
K- key type | 键类型V- value type | 值类型- Parameters:
name- cache name | 缓存名称configurer- configuration consumer | 配置消费者- Returns:
- cache instance | 缓存实例
-
getOrCreateCache
-
createCache
Create cache with configuration (always creates new) 创建带配置的缓存(总是创建新的)- Type Parameters:
K- key type | 键类型V- value type | 值类型- Parameters:
name- cache name | 缓存名称config- cache configuration | 缓存配置- Returns:
- cache instance | 缓存实例
-
getCache
-
getCacheNames
-
removeCache
Remove cache by name 按名称移除缓存- Parameters:
name- cache name | 缓存名称
-
getAllStats
Get statistics for all caches 获取所有缓存的统计信息- Returns:
- map of cache name to stats | 缓存名称到统计的映射
-
getCombinedStats
Get combined statistics 获取合并的统计信息- Returns:
- combined stats | 合并的统计
-
getCacheStats
Get statistics for a specific cache by name 按名称获取特定缓存的统计信息- Parameters:
name- cache name | 缓存名称- Returns:
- cache stats or empty if not found | 缓存统计,不存在返回空统计
- Since:
- V2.0.1
-
getCachesByPattern
Get caches matching a name pattern (supports * and ? wildcards) 获取匹配名称模式的缓存(支持 * 和 ? 通配符)Examples | 示例:
// Get all user-related caches Map<String, Cache> userCaches = manager.getCachesByPattern("user:*"); // Get all caches ending with "-cache" Map<String, Cache> caches = manager.getCachesByPattern("*-cache");- Type Parameters:
K- key type | 键类型V- value type | 值类型- Parameters:
pattern- name pattern | 名称模式- Returns:
- map of matching caches | 匹配的缓存 Map
- Since:
- V2.0.1
-
invalidateByPattern
Invalidate all entries in caches matching a name pattern 使匹配名称模式的缓存中的所有条目失效- Parameters:
pattern- name pattern | 名称模式- Returns:
- count of invalidated caches | 失效的缓存数量
- Since:
- V2.0.1
-
cleanUpAll
public void cleanUpAll()Clean up all caches 清理所有缓存 -
invalidateAll
public void invalidateAll()Invalidate all entries in all caches 使所有缓存中的所有条目失效 -
shutdown
public void shutdown()Shutdown the cache manager, closing all managed caches and releasing resources. 关闭缓存管理器,关闭所有托管缓存并释放资源。This method invalidates all cache entries and attempts to shut down internal executors and schedulers of each managed cache. Caches that implement
AutoCloseablewill have theirclose()method called.DefaultCacheinstances will have theirshutdown()method called to release background cleanup schedulers and internally-created executors. -
isShutdown
public boolean isShutdown()Check if manager is shut down 检查管理器是否已关闭- Returns:
- true if shut down | 已关闭返回 true
-
reset
public void reset()Reset manager (for testing) 重置管理器(用于测试)
-