1

I have a requirement to reset the expire time if the record is accessed before its initial expire time. I am using Spring data redis API to use Redis as Cache. I am using RediscacheManager's setDefaultExpiration(5000) to set default expiration. Unable to find any solutions or documentation about resetting the expiry time. Any guidance is appreciated.

Also, wondering, why couldn't this be a natural feature of Redis Cache, after all, it should get the most used records from cache.

1 Answers1

0

Wrote this method and called from appropriate places. Worked like a charm for me.

public void resetExpire(String keyPattern) {
        LOG.debug("Getting Multiple keys from cache with pattern:  " + keyPattern);
        Set<String> keylist = redisTemplate.keys(keyPattern);
        redisTemplate.executePipelined(new RedisCallback<Object>() {
            public Object doInRedis(RedisConnection connection) throws DataAccessException {
                keylist.forEach(key->
                redisTemplate.expire(key, 5000, TimeUnit.SECONDS));
                return null;
            }
        });
    }