Class TtlJitter
When many cache entries share the same TTL, they expire simultaneously and cause a thundering-herd surge to the backend. This utility applies random jitter or deterministic stagger offsets to spread expirations over a configurable window, and provides a mutex-based refresh gate to ensure only one thread reloads a stale entry.
当许多缓存条目共享相同的 TTL 时,它们会同时过期并导致对后端的惊群涌入。 此工具通过添加随机抖动或确定性错开偏移来将过期分散到可配置的时间窗口, 并提供基于互斥锁的刷新门控,确保只有一个线程重新加载过期条目。
Features | 主要功能:
- Random TTL jitter to distribute expiration times - 随机 TTL 抖动以分散过期时间
- Staggered TTL for batch-loaded entries - 批量加载条目的错开 TTL
- Mutex-based refresh to prevent thundering herd - 基于互斥锁的刷新以防止惊群效应
Usage Examples | 使用示例:
TtlJitter jitter = TtlJitter.of(Duration.ofSeconds(30)); // up to 30s extra
// Randomize a single entry TTL
Duration ttl = jitter.apply(Duration.ofMinutes(10));
// Stagger TTLs across a batch of 100 entries
for (int i = 0; i < 100; i++) {
Duration ttl = jitter.stagger(Duration.ofMinutes(10), 100, i);
}
// Mutex-protected refresh (only one thread reloads)
String value = jitter.mutexRefresh(() -> loadFromDB(), staleValue);
Performance | 性能特性:
- ThreadLocalRandom for jitter — no shared state contention - 使用 ThreadLocalRandom 无争用
- ReentrantLock.tryLock() for non-blocking mutex refresh - 非阻塞互斥刷新
- O(1) for all operations - 所有操作 O(1)
Security | 安全性:
- Thread-safe: Yes (ThreadLocalRandom + ReentrantLock) - 线程安全: 是
- Null-safe: Partial (null baseTtl is returned unchanged) - 空值安全: 部分(null baseTtl 原样返回)
- Since:
- JDK 25, opencode-base-cache V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionApplies a random jitter offset to the given TTL.static TtlJitterdefaults()Creates a TtlJitter with a default 30-second jitter range.Returns the configured maximum jitter range.<V> VmutexRefresh(Supplier<V> refreshAction, V staleValue) Executes a refresh action with mutex protection.static TtlJitterCreates a TtlJitter with the given maximum jitter range.Calculates a deterministic stagger offset for the entry atindexin a batch.toString()
-
Method Details
-
of
-
defaults
Creates a TtlJitter with a default 30-second jitter range. 创建具有默认 30 秒抖动范围的 TtlJitter。- Returns:
- a TtlJitter with 30s range | 具有 30 秒范围的 TtlJitter
-
apply
Applies a random jitter offset to the given TTL. 对给定的 TTL 添加随机抖动偏移。The added jitter is uniformly distributed in
[0, jitterRange].添加的抖动在
[0, jitterRange]范围内均匀分布。- Parameters:
baseTtl- the base TTL to randomize | 要随机化的基础 TTL- Returns:
- the jittered TTL, or
baseTtlunchanged if it is null or zero | 添加抖动后的 TTL;如果 baseTtl 为 null 或 zero 则原样返回
-
stagger
Calculates a deterministic stagger offset for the entry atindexin a batch. 为批次中index位置的条目计算确定性错开偏移。Distributes a batch of
batchSizeentries evenly across the jitter range so that no two entries in the batch share the same expiration time.将
batchSize个条目均匀分布在抖动范围内,使批次中的任何两个条目都不共享相同的过期时间。- Parameters:
baseTtl- the base TTL | 基础 TTLbatchSize- the total number of entries in the batch | 批次中的条目总数index- the zero-based index of this entry | 此条目的零基索引- Returns:
- the staggered TTL for this entry | 此条目的错开 TTL
-
mutexRefresh
Executes a refresh action with mutex protection. Only the thread that acquires the lock performs the refresh; others return the stale value immediately. 使用互斥锁保护执行刷新操作。只有获取锁的线程执行刷新;其他线程立即返回过期值。- Type Parameters:
V- the value type | 值类型- Parameters:
refreshAction- the action that loads the fresh value | 加载新值的操作staleValue- the value to return if another thread holds the lock | 另一个线程持有锁时返回的值- Returns:
- the refreshed value, or
staleValueif the lock was already held | 刷新后的值;如果锁已被持有则返回staleValue
-
getJitterRange
Returns the configured maximum jitter range. 返回配置的最大抖动范围。- Returns:
- the jitter range | 抖动范围
-
toString
-