fix(cache): 构造期校验配置项,误配在装配阶段即 fail-fast
default/null-ttl-seconds、jitter-ratio、breakdown 的 lock-ttl-ms/wait-timeout-ms/ wait-interval-ms 原无校验,负值/越界要到运行期(写入非法 TTL、抖动放大数倍)才暴露。 在 RedisCacheEnhanceTemplate 构造器手工校验,非法即抛 IllegalArgumentException; 不引 hibernate-validator,守住零新依赖。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -19,6 +19,7 @@ import org.springframework.data.redis.core.script.RedisScript;
|
||||
import java.time.Duration;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
@@ -294,4 +295,27 @@ class RedisCacheEnhanceTemplateTest {
|
||||
// 中断后立即停止等待:仅首次 tryGet 读一次,而非空转数百次
|
||||
verify(valueOps, atMost(2)).get("cache:user:1");
|
||||
}
|
||||
|
||||
// ---- #5: 构造期配置 fail-fast 校验 ----
|
||||
|
||||
private RedisCacheEnhanceTemplate newTemplateWith(Consumer<RedisCacheProperties> mut) {
|
||||
RedisCacheProperties p = new RedisCacheProperties();
|
||||
mut.accept(p);
|
||||
return new RedisCacheEnhanceTemplate(redis, keyBuilder, codec, p);
|
||||
}
|
||||
|
||||
@Test
|
||||
void constructorRejectsInvalidProps() {
|
||||
assertThrows(IllegalArgumentException.class, () -> newTemplateWith(p -> p.setDefaultTtlSeconds(0)));
|
||||
assertThrows(IllegalArgumentException.class, () -> newTemplateWith(p -> p.setNullTtlSeconds(-1)));
|
||||
assertThrows(IllegalArgumentException.class, () -> newTemplateWith(p -> p.setJitterRatio(1.0)));
|
||||
assertThrows(IllegalArgumentException.class, () -> newTemplateWith(p -> p.setJitterRatio(-0.1)));
|
||||
assertThrows(IllegalArgumentException.class, () -> newTemplateWith(p -> p.getBreakdown().setLockTtlMs(0)));
|
||||
assertThrows(IllegalArgumentException.class, () -> newTemplateWith(p -> p.getBreakdown().setWaitIntervalMs(0)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void constructorAcceptsDefaults() {
|
||||
new RedisCacheEnhanceTemplate(redis, keyBuilder, codec, new RedisCacheProperties());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user