2

ReactiveRedisTemplate opsForHash put doesn't overwrite the value.

redisTemplate.opsForHash().put("NAMESPACE", id, personInfo.toByteArray()).
                    map(resBoolean -> {                     
                        return resBoolean;
                    }).onErrorResume(e -> {
                        return Mono.just(false);
                    });

Only when I first call the above method with a new key, the value is stored in Redis. When I call the same method second time with the same value or different value, I get false back.

Does redis put doesn't overwrite the value for the same key ? Is it because, each value stored for the key is immutable ?

If I remove the cache value, I was able to set the value again which is obvious. Pleas help answer.

1 Answers1

0

Unfortunately, the documentation of ReactiveRedisTemplate does not describe what the returned Boolean field means.

We can rely on the official Redis documentation:

Return value Integer reply:

The number of fields that were added.

Most probably the ReactiveRedisTemplate translates positive Integer to true and 0 to false. So the returned false value just means that the field is not newly added just overwritten, so your code probably works fine.

Try this example on try.redis.io to see this in action:

> HSET myhash field1 "Hello"
(integer) 1
> HSET myhash field1 "Hello2"
(integer) 0
> HGET myhash field1
"Hello2"
Martin Tarjányi
  • 8,863
  • 2
  • 31
  • 49