Interface TtlDecayPolicy
- All Known Implementing Classes:
TtlDecayPolicy.ExponentialDecay, TtlDecayPolicy.LinearDecay, TtlDecayPolicy.NoDecay, TtlDecayPolicy.StepDecay
public sealed interface TtlDecayPolicy
permits TtlDecayPolicy.LinearDecay, TtlDecayPolicy.ExponentialDecay, TtlDecayPolicy.StepDecay, TtlDecayPolicy.NoDecay
TTL Decay Policy - TTL that decreases over time or access count
TTL 衰减策略 - 随时间或访问次数递减的 TTL
Implements TTL that decays based on various factors, useful for gradually expiring cold data while keeping hot data longer.
实现基于各种因素衰减的 TTL,适用于逐渐过期冷数据同时保持热数据更长时间。
Decay Modes | 衰减模式:
- Linear decay - 线性衰减
- Exponential decay - 指数衰减
- Step decay - 阶梯衰减
- Access-count decay - 访问次数衰减
Usage Examples | 使用示例:
// Linear decay: starts at 1 hour, decays to 5 minutes over 10 accesses
TtlDecayPolicy decay = TtlDecayPolicy.linear(
Duration.ofHours(1), // initial TTL
Duration.ofMinutes(5), // minimum TTL
10 // decay after N accesses
);
// Exponential decay: halves TTL each time until minimum
TtlDecayPolicy decay = TtlDecayPolicy.exponential(
Duration.ofHours(1), // initial TTL
Duration.ofMinutes(5), // minimum TTL
0.5 // decay factor
);
Features | 主要功能:
- Linear TTL decay - 线性 TTL 衰减
- Exponential TTL decay - 指数 TTL 衰减
- Step-based TTL decay - 阶梯 TTL 衰减
- Configurable minimum TTL - 可配置最小 TTL
Security | 安全性:
- Thread-safe: Yes (sealed interface, immutable implementations) - 线程安全: 是(密封接口,不可变实现)
- Null-safe: Yes - 空值安全: 是
- Since:
- JDK 25, opencode-base-cache V2.0.5
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic final recordExponential decay implementation 指数衰减实现static final recordLinear decay implementation 线性衰减实现static final recordNo decay - constant TTL 不衰减 - 恒定 TTLstatic final recordStep definition for step decay 阶梯衰减的步骤定义static final recordStep decay implementation 阶梯衰减实现 -
Method Summary
Modifier and TypeMethodDescriptioncalculateDecayedTtl(long accessCount) Calculate decayed TTL based on access count 根据访问次数计算衰减后的 TTLstatic TtlDecayPolicyexponential(Duration initialTtl, Duration minimumTtl, double decayFactor) Create exponential decay policy 创建指数衰减策略Get initial TTL (before any decay) 获取初始 TTL(任何衰减之前)static TtlDecayPolicyCreate linear decay policy 创建线性衰减策略Get minimum TTL (floor) 获取最小 TTL(下限)static TtlDecayPolicyCreate no-decay policy (constant TTL) 创建不衰减策略(恒定 TTL)static TtlDecayPolicystep(TtlDecayPolicy.Step... steps) Create step decay policy 创建阶梯衰减策略
-
Method Details
-
calculateDecayedTtl
Calculate decayed TTL based on access count 根据访问次数计算衰减后的 TTL- Parameters:
accessCount- number of times entry has been accessed | 条目被访问的次数- Returns:
- decayed TTL | 衰减后的 TTL
-
initialTtl
Duration initialTtl()Get initial TTL (before any decay) 获取初始 TTL(任何衰减之前)- Returns:
- initial TTL | 初始 TTL
-
minimumTtl
-
none
Create no-decay policy (constant TTL) 创建不衰减策略(恒定 TTL)- Parameters:
ttl- the constant TTL | 恒定 TTL- Returns:
- decay policy | 衰减策略
-
linear
Create linear decay policy 创建线性衰减策略- Parameters:
initialTtl- initial TTL | 初始 TTLminimumTtl- minimum TTL (floor) | 最小 TTL(下限)decaySteps- number of steps to reach minimum | 达到最小值的步数- Returns:
- decay policy | 衰减策略
-
exponential
Create exponential decay policy 创建指数衰减策略- Parameters:
initialTtl- initial TTL | 初始 TTLminimumTtl- minimum TTL (floor) | 最小 TTL(下限)decayFactor- decay factor (0.0 to 1.0) | 衰减因子(0.0 到 1.0)- Returns:
- decay policy | 衰减策略
-
step
Create step decay policy 创建阶梯衰减策略- Parameters:
steps- array of (accessThreshold, ttl) pairs | (访问阈值, TTL) 对数组- Returns:
- decay policy | 衰减策略
-