spring-data-redis
spring-data-redis copied to clipboard
Remove Duplicate Code and Hardcoded Strings in RedisTemplate
Summary
Extract duplicated precision command fallback logic from RedisTemplate into a reusable utility class with type-safe enum constants.
Current Problem
The same try-catch pattern is duplicated across 3 methods in RedisTemplate:
// This appears in expire(), expireAt(), and getExpire()
try {
return connection.pExpire(rawKey, rawTimeout);
} catch (Exception ignore) {
return connection.expire(rawKey, TimeoutUtils.toSeconds(timeout, unit));
}
------- after -------
Create a utility class PrecisionApiHelper with an enum for type-safe command names:
// New approach - single reusable method
return doWithKeys(connection -> PrecisionApiHelper.withPrecisionFallback(
PrecisionCommand.PEXPIRE,
() -> connection.pExpire(rawKey, rawTimeout),
() -> connection.expire(rawKey, TimeoutUtils.toSeconds(timeout, unit))
));