Search code examples
cachingredisspring-data-redisredis-cluster

How to set maximum time for a key to store in Redis cache


How to set maximum time for a key to store in Redis cache? Is there any configurable parameter for that


Solution

  • There are a few examples in documentation through configuration or object expiration.

    Using configuration:

    RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
        .entryTtl(Duration.ofSeconds(1))
    

    Using annotation on a class attribute or method

    @TimeToLive
    private Long expiration;
    

    Expiration times can be set by using the @TimeToLive annotation on either a numeric property or a method