Class MultiLevelCache<K,V>
java.lang.Object
cloud.opencode.base.cache.multilevel.MultiLevelCache<K,V>
- Type Parameters:
K- key type | 键类型V- value type | 值类型Security | 安全性:
- Thread-safe: Yes - 线程安全: 是
- Null-safe: Partial (null values not allowed) - 空值安全: 部分(不允许 null 值)
- All Implemented Interfaces:
Cache<K,V>
Multi-Level Cache - Supports 3+ levels with flexible configuration
多级缓存 - 支持 3 个以上级别的灵活配置
A flexible multi-level cache implementation that supports arbitrary number of cache levels with individual configurations.
灵活的多级缓存实现,支持任意数量的缓存级别,各级别可独立配置。
Features | 主要功能:
- Unlimited cache levels - 无限缓存级别
- Per-level TTL - 每级 TTL
- Automatic promotion - 自动提升
- Spillover handling - 溢出处理
- Level-specific metrics - 级别特定指标
Usage Examples | 使用示例:
// Create 3-level cache: L1 (hot) -> L2 (warm) -> L3 (cold/remote)
MultiLevelCache<String, User> cache = MultiLevelCache.<String, User>builder()
.level(LevelConfig.<String, User>builder()
.name("L1-hot")
.cache(hotCache)
.ttl(Duration.ofMinutes(5))
.promoteOnHit(true)
.build())
.level(LevelConfig.<String, User>builder()
.name("L2-warm")
.cache(warmCache)
.ttl(Duration.ofMinutes(30))
.promoteOnHit(true)
.build())
.level(LevelConfig.<String, User>builder()
.name("L3-cold")
.cache(coldCache)
.ttl(Duration.ofHours(24))
.build())
.build();
// Use like normal cache
User user = cache.get("user:1001"); // Checks L1, L2, L3 in order
- Since:
- JDK 25, opencode-base-cache V2.0.5
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder for MultiLevelCache MultiLevelCache 构建器static enumInvalidation policy enumeration 失效策略枚举static final recordLevel configuration 级别配置static classLevel configuration builder 级别配置构建器static classLevel metrics 级别指标static final recordMulti-level statistics 多级统计static enumWrite policy enumeration 写策略枚举 -
Method Summary
Modifier and TypeMethodDescriptionasMap()Get a concurrent map view of this cache 获取 ConcurrentMap 视图AsyncCache<K, V> async()Get async view of this cache 获取异步视图static <K,V> MultiLevelCache.Builder <K, V> builder()Create a builder 创建构建器voidcleanUp()Perform cleanup (expired entries, etc.)booleancontainsKey(K key) Check if key exists 检查键是否存在entries()Get snapshot of all entries 获取所有条目的快照longGet estimated entry count (fast) 获取估算条目数(快速)Get value by key, returns null if not present 根据键获取值,不存在返回 nullGet value by key, load using loader if not present 根据键获取值,不存在时通过 loader 加载Get all values for given keys, returns only existing entries 批量获取,仅返回存在的条目Get all values, load missing keys using loader 批量获取,缺失的键通过 loader 加载getLevel(int level) Get cache at specific level 获取特定级别的缓存Get metrics for all levels 获取所有级别的指标Get aggregate statistics 获取聚合统计voidinvalidate(K key) Invalidate a single key 使单个键失效voidInvalidate all entries 清空所有缓存voidinvalidateAll(Iterable<? extends K> keys) Invalidate multiple keys 批量失效keys()Get snapshot of all keys 获取所有键的快照intGet number of cache levels 获取缓存级别数metrics()Get detailed cache metrics with latency percentiles 获取带延迟百分位数的详细缓存指标name()Get cache name 获取缓存名称voidPut a key-value pair into cache 放入键值对voidPut all key-value pairs into cache 批量放入voidputAllWithTtl(Map<? extends K, ? extends V> map, Duration ttl) Put all entries with custom TTL 批量放入并指定自定义 TTLbooleanputIfAbsent(K key, V value) Put if the key is absent, returns true if successful 不存在时放入,成功返回 truebooleanputIfAbsentWithTtl(K key, V value, Duration ttl) Put if absent with custom TTL 不存在时放入并指定自定义 TTLvoidputWithTtl(K key, V value, Duration ttl) Put a key-value pair with custom TTL (overrides default expiration) 放入键值对并指定自定义 TTL(覆盖默认过期时间)longsize()Get exact entry count (may be slow) 获取精确条目数(可能较慢)stats()Get cache statistics 获取缓存统计信息values()Get snapshot of all values 获取所有值的快照Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface Cache
clear, compute, computeIfAbsent, computeIfPresent, containsValue, entryIterator, entryParallelStream, entryStream, forEach, forEachKey, forEachValue, getAndRemove, getByPattern, getIfPresent, getOptional, getOrDefault, getOrNull, invalidateByPattern, invalidateByValue, invalidateIf, isEmpty, keyIterator, keyParallelStream, keyStream, merge, putAllIfAbsent, putIfPresent, removeIfEquals, removeIfPresent, replace, replace, replaceAll, resetStats, updateAll, updateTtl, updateTtl, updateTtlAll, valueParallelStream, valueStream
-
Method Details
-
builder
Create a builder 创建构建器- Type Parameters:
K- key type | 键类型V- value type | 值类型- Returns:
- builder | 构建器
-
get
-
get
Description copied from interface:CacheGet value by key, load using loader if not present 根据键获取值,不存在时通过 loader 加载 -
getAll
-
getAll
-
put
-
putAll
-
putIfAbsent
Description copied from interface:CachePut if the key is absent, returns true if successful 不存在时放入,成功返回 true- Specified by:
putIfAbsentin interfaceCache<K,V> - Parameters:
key- the key | 键value- the value | 值- Returns:
- true if put successfully | 成功放入返回 true
-
putWithTtl
Description copied from interface:CachePut a key-value pair with custom TTL (overrides default expiration) 放入键值对并指定自定义 TTL(覆盖默认过期时间)- Specified by:
putWithTtlin interfaceCache<K,V> - Parameters:
key- the key | 键value- the value | 值ttl- the time-to-live for this entry | 此条目的存活时间
-
putAllWithTtl
Description copied from interface:CachePut all entries with custom TTL 批量放入并指定自定义 TTL- Specified by:
putAllWithTtlin interfaceCache<K,V> - Parameters:
map- the key-value pairs | 键值对 Mapttl- the time-to-live for all entries | 所有条目的存活时间
-
putIfAbsentWithTtl
Description copied from interface:CachePut if absent with custom TTL 不存在时放入并指定自定义 TTL- Specified by:
putIfAbsentWithTtlin interfaceCache<K,V> - Parameters:
key- the key | 键value- the value | 值ttl- the time-to-live | 存活时间- Returns:
- true if put successfully | 成功放入返回 true
-
invalidate
-
invalidateAll
-
invalidateAll
public void invalidateAll()Description copied from interface:CacheInvalidate all entries 清空所有缓存- Specified by:
invalidateAllin interfaceCache<K,V>
-
containsKey
-
size
-
estimatedSize
public long estimatedSize()Description copied from interface:CacheGet estimated entry count (fast) 获取估算条目数(快速)- Specified by:
estimatedSizein interfaceCache<K,V> - Returns:
- estimated count | 估算数量
-
keys
-
values
-
entries
-
asMap
-
stats
-
metrics
-
cleanUp
-
async
-
name
-
levelCount
public int levelCount()Get number of cache levels 获取缓存级别数- Returns:
- level count | 级别数
-
getLevel
-
getLevelMetrics
Get metrics for all levels 获取所有级别的指标- Returns:
- list of level metrics | 级别指标列表
-
getMultiLevelStats
Get aggregate statistics 获取聚合统计- Returns:
- aggregate stats | 聚合统计
-