Interface TtlPolicy<K,V>

Type Parameters:
K - key type | 键类型
V - value type | 值类型

Security | 安全性:

  • Thread-safe: Yes (functional interface, implementations should be stateless) - 线程安全: 是(函数式接口,实现应无状态)
  • Null-safe: Yes - 空值安全: 是
Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface TtlPolicy<K,V>
TTL Policy - Advanced TTL management strategies TTL 策略 - 高级 TTL 管理策略

Provides flexible TTL calculation based on various factors.

根据各种因素提供灵活的 TTL 计算。

Features | 主要功能:

  • Fixed TTL - 固定 TTL
  • Variable TTL by key - 按键可变 TTL
  • Value-based TTL - 基于值的 TTL
  • Pattern-based TTL - 基于模式的 TTL
  • TTL decay - TTL 衰减

Usage Examples | 使用示例:

// Fixed TTL
TtlPolicy<String, User> fixed = TtlPolicy.fixed(Duration.ofMinutes(30));

// Variable by key pattern
TtlPolicy<String, User> pattern = TtlPolicy.<String, User>builder()
    .pattern("session:*", Duration.ofHours(1))
    .pattern("user:*", Duration.ofMinutes(30))
    .defaultTtl(Duration.ofMinutes(10))
    .build();

// Value-based TTL
TtlPolicy<String, User> valueBased = TtlPolicy.byValue(
    (key, user) -> user.isPremium() ? Duration.ofHours(1) : Duration.ofMinutes(10)
);
Since:
JDK 25, opencode-base-cache V2.0.5
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static class 
    Builder for pattern-based TTL policies 基于模式的 TTL 策略构建器
  • Method Summary

    Modifier and Type
    Method
    Description
    static <K,V> TtlPolicy.PatternBuilder<K,V>
    Create a builder for pattern-based TTL 创建基于模式的 TTL 构建器
    static <K,V> TtlPolicy<K,V>
    byKey(Function<K,Duration> calculator)
    Create a key-based TTL policy 创建基于键的 TTL 策略
    static <K,V> TtlPolicy<K,V>
    byValue(BiFunction<K,V,Duration> calculator)
    Create a value-based TTL policy 创建基于值的 TTL 策略
    calculateTtl(K key, V value)
    Calculate TTL for a cache entry 计算缓存条目的 TTL
    static <K,V> TtlPolicy<K,V>
    Create a fixed TTL policy 创建固定 TTL 策略
    static <K,V> TtlPolicy<K,V>
    Create a no-expiration policy 创建不过期策略
    default TtlPolicy<K,V>
    withJitter(double jitterPercent)
    Create a policy that adds jitter to prevent thundering herd 创建添加抖动以防止惊群效应的策略
    default TtlPolicy<K,V>
    Create a policy with maximum TTL 创建具有最大 TTL 的策略
    default TtlPolicy<K,V>
    Create a policy with minimum TTL 创建具有最小 TTL 的策略
  • Method Details

    • calculateTtl

      Duration calculateTtl(K key, V value)
      Calculate TTL for a cache entry 计算缓存条目的 TTL
      Parameters:
      key - the key | 键
      value - the value | 值
      Returns:
      TTL duration, or null for no expiration | TTL 持续时间,null 表示不过期
    • fixed

      static <K,V> TtlPolicy<K,V> fixed(Duration ttl)
      Create a fixed TTL policy 创建固定 TTL 策略
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      ttl - the fixed TTL | 固定 TTL
      Returns:
      TTL policy | TTL 策略
    • noExpiration

      static <K,V> TtlPolicy<K,V> noExpiration()
      Create a no-expiration policy 创建不过期策略
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Returns:
      TTL policy | TTL 策略
    • byKey

      static <K,V> TtlPolicy<K,V> byKey(Function<K,Duration> calculator)
      Create a key-based TTL policy 创建基于键的 TTL 策略
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      calculator - TTL calculator function | TTL 计算函数
      Returns:
      TTL policy | TTL 策略
    • byValue

      static <K,V> TtlPolicy<K,V> byValue(BiFunction<K,V,Duration> calculator)
      Create a value-based TTL policy 创建基于值的 TTL 策略
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      calculator - TTL calculator function | TTL 计算函数
      Returns:
      TTL policy | TTL 策略
    • builder

      static <K,V> TtlPolicy.PatternBuilder<K,V> builder()
      Create a builder for pattern-based TTL 创建基于模式的 TTL 构建器
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Returns:
      builder | 构建器
    • withMinimum

      default TtlPolicy<K,V> withMinimum(Duration minTtl)
      Create a policy with minimum TTL 创建具有最小 TTL 的策略
      Parameters:
      minTtl - minimum TTL | 最小 TTL
      Returns:
      new policy | 新策略
    • withMaximum

      default TtlPolicy<K,V> withMaximum(Duration maxTtl)
      Create a policy with maximum TTL 创建具有最大 TTL 的策略
      Parameters:
      maxTtl - maximum TTL | 最大 TTL
      Returns:
      new policy | 新策略
    • withJitter

      default TtlPolicy<K,V> withJitter(double jitterPercent)
      Create a policy that adds jitter to prevent thundering herd 创建添加抖动以防止惊群效应的策略
      Parameters:
      jitterPercent - jitter percentage (0.0 to 1.0) | 抖动百分比(0.0 到 1.0)
      Returns:
      new policy | 新策略