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.
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 ClassesModifier and TypeInterfaceDescriptionstatic classBuilder for pattern-based TTL policies 基于模式的 TTL 策略构建器 -
Method Summary
Modifier and TypeMethodDescriptionstatic <K,V> TtlPolicy.PatternBuilder <K, V> builder()Create a builder for pattern-based TTL 创建基于模式的 TTL 构建器static <K,V> TtlPolicy <K, V> 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 计算缓存条目的 TTLstatic <K,V> TtlPolicy <K, V> Create a fixed TTL policy 创建固定 TTL 策略static <K,V> TtlPolicy <K, V> Create a no-expiration policy 创建不过期策略withJitter(double jitterPercent) Create a policy that adds jitter to prevent thundering herd 创建添加抖动以防止惊群效应的策略withMaximum(Duration maxTtl) Create a policy with maximum TTL 创建具有最大 TTL 的策略withMinimum(Duration minTtl) Create a policy with minimum TTL 创建具有最小 TTL 的策略
-
Method Details
-
calculateTtl
-
fixed
-
noExpiration
Create a no-expiration policy 创建不过期策略- Type Parameters:
K- key type | 键类型V- value type | 值类型- Returns:
- TTL policy | TTL 策略
-
byKey
-
byValue
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
Create a builder for pattern-based TTL 创建基于模式的 TTL 构建器- Type Parameters:
K- key type | 键类型V- value type | 值类型- Returns:
- builder | 构建器
-
withMinimum
-
withMaximum
-
withJitter
-