Description
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.
Activity
christophstrobl commentedon Jun 11, 2021
I checked
DefaultValueOperationsIntegrationTests#testSetIfAbsentWithExpiration
forLettuce
and everything seems to work as expected. Maybe you can provide a complete minimal sample (something that we can unzip or git clone, build, and deploy) that reproduces the problem.Make sure to tests against jedis and lettuce when using AbstractOpera…
codeayra commentedon Jun 11, 2021
I got the problem,
What I am doing first calling setIfAbsent with ttl and its working after that I get the key update its value and calling setIfPresent without ttl and which might be clearing its ttl.
How can I update a key value without clearing its ttl ?
christophstrobl commentedon Jun 11, 2021
I see - there's an
keepTtl
option onExpiration
that allows to do this. But it is not present onValueOperations
so you'd have to call it something like the following on the template.I created #2084 for that.
codeayra commentedon Jun 11, 2021
Got this this error
org.springframework.data.redis.RedisSystemException: Error in execution; nested exception is io.lettuce.core.RedisCommandExecutionException: ERR syntax error
code is
public void update(String key, Object value) {
String absoluteKey = generateAbsoluteKey(key);
String valueString = objectMapper.writeValueAsString(value);
try {
redisTemplate.execute(connection -> connection
.set(rawKey(absoluteKey), rawValue(valueString), Expiration.keepTtl(), RedisStringCommands.SetOption
.ifPresent()), true);
@SuppressWarnings("unchecked")
private byte[] rawKey(Object key) {
christophstrobl commentedon Jun 11, 2021
KEEPTTL
is available as off Redis 6.Does lettuce tell you more than just
ERR syntax error
?codeayra commentedon Jun 11, 2021
nothing..just this error.
codeayra commentedon Jun 12, 2021
its working finally with redis 6. Thanks.
1 remaining item