Interface EvictionPolicy<T>

Type Parameters:
T - the pooled object type - 池化对象类型
All Known Implementing Classes:
EvictionPolicy.Composite, EvictionPolicy.IdleTime, EvictionPolicy.LFU, EvictionPolicy.LRU, EvictionPolicy.MaxAge

public sealed interface EvictionPolicy<T> permits EvictionPolicy.IdleTime<T>, EvictionPolicy.LRU<T>, EvictionPolicy.LFU<T>, EvictionPolicy.MaxAge<T>, EvictionPolicy.Composite<T>
EvictionPolicy - Sealed Eviction Policy Interface (JDK 25 Sealed) EvictionPolicy - 密封驱逐策略接口 (JDK 25 Sealed)

Sealed interface for type-safe eviction policies. Uses JDK 25 sealed types and pattern matching for exhaustive handling.

用于类型安全驱逐策略的密封接口。使用JDK 25密封类型和模式匹配进行穷尽处理。

Implementations | 实现:

  • IdleTime - Evict based on idle duration - 基于空闲时长驱逐
  • LRU - Least Recently Used - 最近最少使用
  • LFU - Least Frequently Used - 最不经常使用
  • MaxAge - Evict based on maximum object lifetime - 基于最大对象生命周期驱逐
  • Composite - Combine multiple policies - 组合多个策略

Features | 主要功能:

  • Sealed interface with exhaustive pattern matching support - 密封接口,支持穷尽模式匹配
  • Idle-time-based eviction with configurable duration - 基于空闲时间的驱逐,可配置时长
  • LRU and LFU eviction strategies - LRU和LFU驱逐策略
  • Composite policy combining multiple strategies with AND/OR logic - 组合策略,使用AND/OR逻辑组合多个策略
  • Immutable record implementations for thread safety - 不可变记录实现,确保线程安全

Usage Examples | 使用示例:

// Pattern matching (exhaustive)
String desc = switch (policy) {
    case EvictionPolicy.IdleTime<T>(var maxIdle) ->
        "Evict if idle > " + maxIdle;
    case EvictionPolicy.LRU<T>(var max) ->
        "Keep max " + max + " objects";
    case EvictionPolicy.LFU<T>(var minCount) ->
        "Evict if borrowed < " + minCount;
    case EvictionPolicy.MaxAge<T>(var maxLife) ->
        "Evict if age > " + maxLife;
    case EvictionPolicy.Composite<T>(var policies, var all) ->
        "Composite: " + (all ? "ALL" : "ANY");
};

Security | 安全性:

  • Thread-safe: Yes (immutable records) - 线程安全: 是(不可变记录)
Since:
JDK 25, opencode-base-pool V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • evict

      boolean evict(PooledObject<T> obj, EvictionContext context)
      Determines if the object should be evicted. 确定对象是否应被驱逐。
      Parameters:
      obj - the pooled object - 池化对象
      context - the eviction context - 驱逐上下文
      Returns:
      true if should evict - 如果应驱逐返回true