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:
@@ -38,6 +38,7 @@ public class RedisCacheEnhanceTemplate {
|
||||
|
||||
public RedisCacheEnhanceTemplate(StringRedisTemplate redis, CacheKeyBuilder keyBuilder,
|
||||
CacheValueCodec codec, RedisCacheProperties props) {
|
||||
validateProps(props);
|
||||
this.redis = redis;
|
||||
this.keyBuilder = keyBuilder;
|
||||
this.codec = codec;
|
||||
@@ -134,6 +135,39 @@ public class RedisCacheEnhanceTemplate {
|
||||
}
|
||||
}
|
||||
|
||||
/** 构造期校验配置项,误配在装配阶段即 fail-fast,而非运行期才暴露。 */
|
||||
private static void validateProps(RedisCacheProperties props) {
|
||||
if (props.getDefaultTtlSeconds() <= 0) {
|
||||
throw new IllegalArgumentException(
|
||||
"redis-cache.default-ttl-seconds must be positive, but was " + props.getDefaultTtlSeconds());
|
||||
}
|
||||
if (props.getNullTtlSeconds() <= 0) {
|
||||
throw new IllegalArgumentException(
|
||||
"redis-cache.null-ttl-seconds must be positive, but was " + props.getNullTtlSeconds());
|
||||
}
|
||||
double ratio = props.getJitterRatio();
|
||||
if (ratio < 0 || ratio >= 1) {
|
||||
throw new IllegalArgumentException(
|
||||
"redis-cache.jitter-ratio must be in [0,1), but was " + ratio);
|
||||
}
|
||||
RedisCacheProperties.Breakdown bd = props.getBreakdown();
|
||||
if (bd == null) {
|
||||
throw new IllegalArgumentException("redis-cache.breakdown must not be null");
|
||||
}
|
||||
if (bd.getLockTtlMs() <= 0) {
|
||||
throw new IllegalArgumentException(
|
||||
"redis-cache.breakdown.lock-ttl-ms must be positive, but was " + bd.getLockTtlMs());
|
||||
}
|
||||
if (bd.getWaitTimeoutMs() < 0) {
|
||||
throw new IllegalArgumentException(
|
||||
"redis-cache.breakdown.wait-timeout-ms must be >= 0, but was " + bd.getWaitTimeoutMs());
|
||||
}
|
||||
if (bd.getWaitIntervalMs() <= 0) {
|
||||
throw new IllegalArgumentException(
|
||||
"redis-cache.breakdown.wait-interval-ms must be positive, but was " + bd.getWaitIntervalMs());
|
||||
}
|
||||
}
|
||||
|
||||
// ---- 读取或回源(三防核心)----
|
||||
|
||||
public <T> T getOrLoad(String key, Class<T> type, Supplier<T> loader) {
|
||||
|
||||
Reference in New Issue
Block a user