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 Classes
    Modifier and Type
    Interface
    Description
    static final record 
    Weighted policy wrapper 加权策略包装器
  • Method Summary

    Modifier and Type
    Method
    Description
    default EvictionPolicy<K,V>
    Combine with another policy using AND logic (evict only if both suggest same victim) 使用 AND 逻辑与另一个策略组合(仅当两个策略建议相同候选时淘汰)
    static <K,V> EvictionPolicy<K,V>
    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(最近最少使用)策略
    void
    onRemoval(K key)
    Called 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 逻辑与另一个策略组合(任一策略建议淘汰则淘汰)
    void
    Record access to an entry 记录条目访问
    void
    Record write to an entry 记录条目写入
    default void
    Reset policy state 重置策略状态
    Select victim entry to evict 选择要淘汰的条目
    static <K,V> EvictionPolicy<K,V>
    Create a weighted composite of multiple policies 创建多个策略的加权组合
    static <K,V> EvictionPolicy<K,V>
    Create W-TinyLFU (Window TinyLFU) policy 创建 W-TinyLFU(窗口 TinyLFU)策略
  • Method Details

    • recordAccess

      void recordAccess(CacheEntry<K,V> entry)
      Record access to an entry 记录条目访问
      Parameters:
      entry - the accessed entry | 被访问的条目
    • recordWrite

      void recordWrite(CacheEntry<K,V> entry)
      Record write to an entry 记录条目写入
      Parameters:
      entry - the written entry | 被写入的条目
    • selectVictim

      Optional<K> selectVictim(Map<K, CacheEntry<K,V>> entries)
      Select victim entry to evict 选择要淘汰的条目
      Parameters:
      entries - current cache entries | 当前缓存条目
      Returns:
      key of entry to evict | 要淘汰的条目的键
    • onRemoval

      void onRemoval(K key)
      Called when entry is removed 当条目被移除时调用
      Parameters:
      key - the removed key | 被移除的键
    • reset

      default void reset()
      Reset policy state 重置策略状态
    • lru

      static <K,V> EvictionPolicy<K,V> lru()
      Create LRU (Least Recently Used) policy 创建 LRU(最近最少使用)策略
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Returns:
      LRU policy | LRU 策略
    • lfu

      static <K,V> EvictionPolicy<K,V> lfu()
      Create LFU (Least Frequently Used) policy 创建 LFU(最不经常使用)策略
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Returns:
      LFU policy | LFU 策略
    • fifo

      static <K,V> EvictionPolicy<K,V> fifo()
      Create FIFO (First In First Out) policy 创建 FIFO(先进先出)策略
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Returns:
      FIFO policy | FIFO 策略
    • wTinyLfu

      static <K,V> EvictionPolicy<K,V> wTinyLfu()
      Create W-TinyLFU (Window TinyLFU) policy 创建 W-TinyLFU(窗口 TinyLFU)策略
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Returns:
      W-TinyLFU policy | W-TinyLFU 策略
    • or

      default EvictionPolicy<K,V> or(EvictionPolicy<K,V> other)
      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

      default EvictionPolicy<K,V> and(EvictionPolicy<K,V> other)
      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 | 加权组合策略