Interface ExpiryPolicy<K,V>

Type Parameters:
K - the type of keys | 键类型
V - the type of values | 值类型

public interface ExpiryPolicy<K,V>
Expiry Policy SPI - Cache entry expiration strategy interface 过期策略 SPI - 缓存条目过期策略接口

Provides interface for determining when cache entries should expire.

提供确定缓存条目何时过期的接口。

Features | 主要功能:

  • TTL - Time To Live (expire after write) - 存活时间(写入后过期)
  • TTI - Time To Idle (expire after access) - 空闲时间(访问后过期)
  • Combined - Both TTL and TTI - 组合策略(同时支持 TTL 和 TTI)
  • Custom - User-defined expiration - 自定义过期策略

Usage Examples | 使用示例:

// TTL - expire 1 hour after write - 写入 1 小时后过期
ExpiryPolicy<String, User> ttl = ExpiryPolicy.ttl(Duration.ofHours(1));

// TTI - expire 30 minutes after last access - 最后访问 30 分钟后过期
ExpiryPolicy<String, User> tti = ExpiryPolicy.tti(Duration.ofMinutes(30));

// Combined - 组合策略
ExpiryPolicy<String, User> combined = ExpiryPolicy.combined(
    Duration.ofHours(1), Duration.ofMinutes(30));

Security | 安全性:

  • Thread-safe: Yes (immutable policies) - 线程安全: 是(不可变策略)
  • Null-safe: Yes - 空值安全: 是
Since:
JDK 25, opencode-base-cache V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Duration
    Duration indicating entry should never expire 表示条目永不过期的时长
  • Method Summary

    Modifier and Type
    Method
    Description
    static <K,V> ExpiryPolicy<K,V>
    Create combined TTL and TTI policy 创建组合 TTL 和 TTI 策略
    static <K,V> ExpiryPolicy<K,V>
    Create policy that never expires 创建永不过期的策略
    expireAfterCreate(K key, V value)
    Calculate expiration duration after create 计算创建后的过期时长
    expireAfterRead(K key, V value, Duration currentDuration)
    Calculate expiration duration after read 计算读取后的过期时长
    expireAfterUpdate(K key, V value, Duration currentDuration)
    Calculate expiration duration after update 计算更新后的过期时长
    static <K,V> ExpiryPolicy<K,V>
    tti(Duration duration)
    Create TTI (Time To Idle) policy - expire after access 创建 TTI(空闲时间)策略 - 访问后过期
    static <K,V> ExpiryPolicy<K,V>
    ttl(Duration duration)
    Create TTL (Time To Live) policy - expire after write 创建 TTL(存活时间)策略 - 写入后过期
  • Field Details

    • INFINITE

      static final Duration INFINITE
      Duration indicating entry should never expire 表示条目永不过期的时长
  • Method Details

    • expireAfterCreate

      Duration expireAfterCreate(K key, V value)
      Calculate expiration duration after create 计算创建后的过期时长
      Parameters:
      key - the key | 键
      value - the value | 值
      Returns:
      expiration duration | 过期时长
    • expireAfterUpdate

      Duration expireAfterUpdate(K key, V value, Duration currentDuration)
      Calculate expiration duration after update 计算更新后的过期时长
      Parameters:
      key - the key | 键
      value - the value | 值
      currentDuration - current remaining duration | 当前剩余时长
      Returns:
      new expiration duration | 新过期时长
    • expireAfterRead

      Duration expireAfterRead(K key, V value, Duration currentDuration)
      Calculate expiration duration after read 计算读取后的过期时长
      Parameters:
      key - the key | 键
      value - the value | 值
      currentDuration - current remaining duration | 当前剩余时长
      Returns:
      new expiration duration | 新过期时长
    • ttl

      static <K,V> ExpiryPolicy<K,V> ttl(Duration duration)
      Create TTL (Time To Live) policy - expire after write 创建 TTL(存活时间)策略 - 写入后过期
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      duration - expiration duration | 过期时长
      Returns:
      TTL policy | TTL 策略
    • tti

      static <K,V> ExpiryPolicy<K,V> tti(Duration duration)
      Create TTI (Time To Idle) policy - expire after access 创建 TTI(空闲时间)策略 - 访问后过期
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      duration - expiration duration | 过期时长
      Returns:
      TTI policy | TTI 策略
    • combined

      static <K,V> ExpiryPolicy<K,V> combined(Duration ttl, Duration tti)
      Create combined TTL and TTI policy 创建组合 TTL 和 TTI 策略
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      ttl - TTL duration | TTL 时长
      tti - TTI duration | TTI 时长
      Returns:
      combined policy | 组合策略
    • eternal

      static <K,V> ExpiryPolicy<K,V> eternal()
      Create policy that never expires 创建永不过期的策略
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Returns:
      eternal policy | 永不过期策略