spring-data-redis icon indicating copy to clipboard operation
spring-data-redis copied to clipboard

Override cache TTL configuration [DATAREDIS-858]

Open spring-projects-issues opened this issue 6 years ago • 2 comments

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

spring-projects-issues avatar Aug 03 '18 00:08 spring-projects-issues

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

spring-projects-issues avatar Feb 13 '20 09:02 spring-projects-issues

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 :)

nicdard avatar Sep 22 '22 11:09 nicdard