Skip to content

Add TtlFunction to RedisCacheConfiguration for dynamic Time to Live #1433

Closed
@spring-projects-issues

Description

@spring-projects-issues

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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions