fix(cache): 公共 set(key,null) 改用短 null-ttl
set(key,null) 原按数据 TTL(可达 default-ttl-seconds)写入空值标记,使空标记长期 占位;改为与回源空值一致用 null-ttl-seconds(不抖动),空标记宜短。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -53,8 +53,9 @@ public class RedisCacheEnhanceTemplate {
|
|||||||
|
|
||||||
public void set(String key, Object value, long ttlSeconds) {
|
public void set(String key, Object value, long ttlSeconds) {
|
||||||
requirePositiveTtl(ttlSeconds);
|
requirePositiveTtl(ttlSeconds);
|
||||||
redis.opsForValue().set(keyBuilder.build(key), codec.encode(value),
|
// 空值(写入空标记)按短 null-ttl 处理,与回源 null 一致,避免空标记长期占位
|
||||||
applyJitter(ttlSeconds), TimeUnit.SECONDS);
|
long ttl = (value == null) ? props.getNullTtlSeconds() : applyJitter(ttlSeconds);
|
||||||
|
redis.opsForValue().set(keyBuilder.build(key), codec.encode(value), ttl, TimeUnit.SECONDS);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---- 读 ----
|
// ---- 读 ----
|
||||||
|
|||||||
@@ -339,4 +339,14 @@ class RedisCacheEnhanceTemplateTest {
|
|||||||
when(valueOps.get("cache:user:1")).thenReturn(null);
|
when(valueOps.get("cache:user:1")).thenReturn(null);
|
||||||
assertFalse(template.exists("user:1"));
|
assertFalse(template.exists("user:1"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ---- #9: 公共 set(key,null) 用短 null-ttl(与回源空值一致) ----
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void setNullUsesNullTtlNotDataTtl() {
|
||||||
|
props.setJitterRatio(0);
|
||||||
|
template.set("user:1", null, 3600); // 传入数据 TTL,但写空标记应改用短 null-ttl
|
||||||
|
verify(valueOps).set("cache:user:1", CacheConstant.NULL_SENTINEL,
|
||||||
|
props.getNullTtlSeconds(), TimeUnit.SECONDS);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user