Interface EvictionPolicy<K,V>
- Type Parameters:
K- the type of keys | 键类型V- the type of values | 值类型
public interface EvictionPolicy<K,V>
Eviction Policy SPI - Cache entry eviction strategy interface
淘汰策略 SPI - 缓存条目淘汰策略接口
Provides interface for selecting which entries to evict when cache is full.
提供当缓存已满时选择要淘汰哪些条目的接口。
Features | 主要功能:
- LRU - Least Recently Used - 最近最少使用
- LFU - Least Frequently Used - 最不经常使用
- FIFO - First In First Out - 先进先出
- W-TinyLFU - Window TinyLFU - 窗口 TinyLFU
Usage Examples | 使用示例:
// Use LRU policy - 使用 LRU 策略
EvictionPolicy<String, User> policy = EvictionPolicy.lru();
// Use W-TinyLFU for better hit rate - 使用 W-TinyLFU 获得更好的命中率
EvictionPolicy<String, User> policy = EvictionPolicy.wTinyLfu();
Security | 安全性:
- Thread-safe: Implementation dependent - 线程安全: 取决于实现
- 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 TypeInterfaceDescriptionstatic final recordWeighted policy wrapper 加权策略包装器 -
Method Summary
Modifier and TypeMethodDescriptiondefault EvictionPolicy<K, V> and(EvictionPolicy<K, V> other) Combine with another policy using AND logic (evict only if both suggest same victim) 使用 AND 逻辑与另一个策略组合(仅当两个策略建议相同候选时淘汰)static <K,V> EvictionPolicy <K, V> fifo()Create FIFO (First In First Out) policy 创建 FIFO(先进先出)策略static <K,V> EvictionPolicy <K, V> lfu()Create LFU (Least Frequently Used) policy 创建 LFU(最不经常使用)策略static <K,V> EvictionPolicy <K, V> lru()Create LRU (Least Recently Used) policy 创建 LRU(最近最少使用)策略voidCalled when entry is removed 当条目被移除时调用default EvictionPolicy<K, V> or(EvictionPolicy<K, V> other) Combine with another policy using OR logic (evict if either suggests eviction) 使用 OR 逻辑与另一个策略组合(任一策略建议淘汰则淘汰)voidrecordAccess(CacheEntry<K, V> entry) Record access to an entry 记录条目访问voidrecordWrite(CacheEntry<K, V> entry) Record write to an entry 记录条目写入default voidreset()Reset policy state 重置策略状态selectVictim(Map<K, CacheEntry<K, V>> entries) Select victim entry to evict 选择要淘汰的条目static <K,V> EvictionPolicy <K, V> weighted(EvictionPolicy.WeightedPolicy<K, V>... policies) Create a weighted composite of multiple policies 创建多个策略的加权组合static <K,V> EvictionPolicy <K, V> wTinyLfu()Create W-TinyLFU (Window TinyLFU) policy with default expected size (10,000) 创建默认期望大小(10,000)的 W-TinyLFU(窗口 TinyLFU)策略static <K,V> EvictionPolicy <K, V> wTinyLfu(int expectedSize) Create W-TinyLFU (Window TinyLFU) policy sized for the expected cache capacity.
-
Method Details
-
recordAccess
Record access to an entry 记录条目访问- Parameters:
entry- the accessed entry | 被访问的条目
-
recordWrite
Record write to an entry 记录条目写入- Parameters:
entry- the written entry | 被写入的条目
-
selectVictim
-
onRemoval
Called when entry is removed 当条目被移除时调用- Parameters:
key- the removed key | 被移除的键
-
reset
default void reset()Reset policy state 重置策略状态 -
lru
Create LRU (Least Recently Used) policy 创建 LRU(最近最少使用)策略- Type Parameters:
K- key type | 键类型V- value type | 值类型- Returns:
- LRU policy | LRU 策略
-
lfu
Create LFU (Least Frequently Used) policy 创建 LFU(最不经常使用)策略- Type Parameters:
K- key type | 键类型V- value type | 值类型- Returns:
- LFU policy | LFU 策略
-
fifo
Create FIFO (First In First Out) policy 创建 FIFO(先进先出)策略- Type Parameters:
K- key type | 键类型V- value type | 值类型- Returns:
- FIFO policy | FIFO 策略
-
wTinyLfu
Create W-TinyLFU (Window TinyLFU) policy with default expected size (10,000) 创建默认期望大小(10,000)的 W-TinyLFU(窗口 TinyLFU)策略- Type Parameters:
K- key type | 键类型V- value type | 值类型- Returns:
- W-TinyLFU policy | W-TinyLFU 策略
-
wTinyLfu
Create W-TinyLFU (Window TinyLFU) policy sized for the expected cache capacity. 创建根据期望缓存容量调整大小的 W-TinyLFU(窗口 TinyLFU)策略。The internal Count-Min Sketch is sized proportionally to
expectedSizeto maintain a low collision rate. Use this overload when the cache maximum size is known in advance.内部 Count-Min Sketch 的大小与
expectedSize成比例, 以保持较低的碰撞率。当缓存最大大小事先已知时,使用此重载。- Type Parameters:
K- key type | 键类型V- value type | 值类型- Parameters:
expectedSize- the expected maximum number of cache entries | 期望的缓存最大条目数- Returns:
- W-TinyLFU policy | W-TinyLFU 策略
-
or
Combine with another policy using OR logic (evict if either suggests eviction) 使用 OR 逻辑与另一个策略组合(任一策略建议淘汰则淘汰)The combined policy selects a victim if either policy returns one. Priority is given to this policy's selection.
组合策略在任一策略返回候选时选择该候选。优先选择当前策略的选择。
- Parameters:
other- the other policy | 另一个策略- Returns:
- combined policy | 组合后的策略
-
and
Combine with another policy using AND logic (evict only if both suggest same victim) 使用 AND 逻辑与另一个策略组合(仅当两个策略建议相同候选时淘汰)The combined policy only evicts if both policies agree on the same victim.
组合策略仅在两个策略同意相同候选时淘汰。
- Parameters:
other- the other policy | 另一个策略- Returns:
- combined policy | 组合后的策略
-
weighted
@SafeVarargs static <K,V> EvictionPolicy<K,V> weighted(EvictionPolicy.WeightedPolicy<K, V>... policies) Create a weighted composite of multiple policies 创建多个策略的加权组合Scores each candidate by weighted vote from each policy.
通过每个策略的加权投票对每个候选评分。
- Type Parameters:
K- key type | 键类型V- value type | 值类型- Parameters:
policies- policies with weights | 带权重的策略- Returns:
- weighted composite policy | 加权组合策略
-