I'm new to Redis+Spring. What's the difference between
stringRedisTemplate.boundValueOps(key).setIfAbsent("STARTED", timeout)
stringRedisTemplate.boundValueOps(key).set("STOPPED", timeout);
and
stringRedisTemplate.opsForValue().setIfAbsent(key, "STARTED", timeout)
stringRedisTemplate.opsForValue().set(key, "STOPPED", timeout);
My problem is the former appends the new value while the latter replaces it. What am I missing here?
What is the correct way to create (atomically) a simple string value and keep updating it? Thanks.
There is no real difference between BoundValueOperations
and ValueOperations
other than BoundValueOperations
does not require the key for each and every operation as it keeps the key internally and delegates to ValueOperations
applying the very value.
BoundValueOperations<String, String> keyBoundOps = stringRedisTemplate.boundValueOps(key);
keyBoundOps.setIfAbsent("STARTED", timeout);
keyBoundOps.set("STOPPED", timeout);
When it comes to the specific commands please have a look at the Redis documentation for SET with different Options like NX
(Only set the key if it does not already exist) and SETEX.
setIfAbsent("STARTED", timeout)
-> SET key STARTED EX timeout NX
.
set("STOPPED", timeout)
-> SETEX key timeout STOPPED
.
You can see all commands arriving at the server using MONITOR.
If there's an issue with one of the operations sending the wrong command, please file a ticket in the bug tracker.