spring-data-redis
spring-data-redis copied to clipboard
Override cache TTL configuration [DATAREDIS-858]
Kyle Cronin opened DATAREDIS-858 and commented
RedisCache
should allow sub classes to define the TTL duration based on the cache key or value for use cases where the TTL is not know at configuration time or could change depending on the value being stored. For example access tokens where the token provider specifies how long the token is valid for.
public class RedisCache {
// default impementation delegates to cache config
@NonNull protected Duration getTtl(Object key, Object value) {
return cacheConfig.getTtl();
}
}
public class AccessTokenRedisCache extends RedisCache {
@Override
@NonNull
protected Duration getTtl(Object key, Object value) {
if (value instanceof AccessToken) {
return ((AccessToken) value).getTtl();
} else {
return super.getTtl(key, value);
}
}
}
If there was a way to specify a TTL policy in the RedisCacheConfiguration that would be bonus
No further details from DATAREDIS-858
Mark Paluch commented
TTL can be configured via RedisCacheConfiguration
and RedisCacheManagerBuilder
on a per-cache basis. We currently do not support dynamic policies based on keys and values
Hi, is this issue still active? I have multiple use cases were I would like to randomise the TTL at each put operation, and I would like to rise a PR for this :)