Skip to content

ValueOperation setIFAbsent with ttl not working. #2081

Closed
@codeayra

Description

@codeayra

I am using RedisTemplate with LettuceConnection Factory(in spring boot) to set key in Redis with ttl ,
Key is getting Set in redis but its ttl not getting set.

when I try to set ttl using redis-cli , its getting set.

here is the code.
public Boolean setIfAbsent(String key, Object value, long timeout, TimeUnit unit) { Boolean isSuccess = Boolean.FALSE; String absoluteKey = generateAbsoluteKey(key); log.info("Setting key {} in redis..", absoluteKey); try { String sValue = objectMapper.writeValueAsString(value); redisTemplate.opsForValue() .setIfAbsent(absoluteKey, sValue, timeout, unit); } catch (Exception e) { log.error("Error in saving value to redis cache for key: " + absoluteKey, e); } return isSuccess; }

RedisConfiguration is like following

implementation group: 'org.springframework.data', name: 'spring-data-redis'

`@Bean
public LettuceConnectionFactory lettuceConnectionFactory() {
return new LettuceConnectionFactory(hostName, port);
}

@Bean
public RedisTemplate<String, String> redisTemplate() {
    RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>();
    redisTemplate.setConnectionFactory(lettuceConnectionFactory());
    redisTemplate.setExposeConnection(true);
    redisTemplate.setKeySerializer(new StringRedisSerializer());
    redisTemplate.setValueSerializer(new StringRedisSerializer());
    return redisTemplate;
}`

There is no error on console.
I am using spring-data-redis-2.5.0-M5
redis version I have tried with 6.2 also 4.0 .

Please suggest.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions