redisspring-cachespring-data-redisredis-cache

Redis cache not being cleared despite setting TTL


I want the data stored in redis cache to be cleared from the cache automatically after a given period, without calling the delete method on it. In this POC, I am setting the TTL as 60 seconds. I have tried setting it in Cache manager using the API setDefaultExpiration, setExpires and in the RedisTemplate using the API expire. None of the solutions have worked for me so far.

@Configuration
public class RedisServerConfig extends CachingConfigurerSupport {
    @Bean
    public RedisTemplate<String, String> redisTemplate() {
        RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(connectionFactory());
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        return redisTemplate;
    }
    @Bean
    public CacheManager cacheManager(RedisTemplate<String, String> redisTemplate) {
        RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate);
        redisCacheManager.setUsePrefix(true);
        redisCacheManager.setCacheNames(cacheNames);
        redisCacheManager.setTransactionAware(true);
        redisCacheManager.setLoadRemoteCachesOnStartup(true);

        // Using setDefaultExpiration
        //redisCacheManager.setDefaultExpiration(60);

        // Using setExpires
        //Map<String, Long> expires = new HashMap<>();
        //cacheNames.stream().forEach(name->expires.put(name, 60L));
        //redisCacheManager.setExpires(expires);
        return redisCacheManager;
    }
    @Bean
    public RedisConnectionFactory connectionFactory() {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxTotal(maxTotal);
        poolConfig.setMaxIdle(maxIdle);
        poolConfig.setMinIdle(minIdle);
        poolConfig.setTestOnBorrow(true);
        poolConfig.setTestOnReturn(true);
        poolConfig.setTestWhileIdle(true);
        poolConfig.setMaxWaitMillis(secondsToWait);
        JedisConnectionFactory factory = new JedisConnectionFactory(sentinelConfig(), poolConfig);
        factory.setUsePool(true);
        factory.setPassword(redisPassword);
        factory.setPort(redisPort);

        return factory;
    }
}

In the redis repository used,

@Repository
public class RedisRepository {
    private static final Logger logger = LoggerFactory.getLogger(RedisRepositoryImpl.class);
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    public void saveSharedSecret(String customerNumber, String sharedSecret) {
        redisTemplate.opsForHash().put(MfaConstants.REDIS_SHAREDSECRET_REGION, myKey, myValue);
        redisTemplate.expire(myKey, 1, TimeUnit.MINUTES);

        // The timeOut obtained here is zero
        logger.info("myKey: [{}], timeOut: [{}]", myKey, redisTemplate.getExpire(myKey, TimeUnit.SECONDS));
    }
}

Kindly point me in the right direction. Any help is appreciated.


Solution

  • In your code:

    redisTemplate.opsForHash().put(MfaConstants.REDIS_SHAREDSECRET_REGION, myKey, myValue);
    redisTemplate.expire(myKey, 1, TimeUnit.MINUTES);
    

    So basically you want to expire a key inside a hash. But unfortunately, it's cannot be done using standard redis command.

    Reference