How can i upsert (replace) operation in Redis? (as Pipelined)
10,892
What do you mean by "update if exists"? The standard Redis SET commands (SET
, MSET
, HSET
, LSET
, etc.) will update (overwrite) an existing key if the key already exists or insert a new key if the key doesn't already exist.
Sounds like you are asking for the default behavior.
Author by
Serhat Dincel
Updated on June 12, 2022Comments
-
Serhat Dincel almost 2 years
Upsert (Replace)
- Update If Exists
- Insert If Not Exists
(Using Primary Key as Pipelined)
-
user1870400 almost 3 yearswhat if I want to conditional upsert? meaning if they key isn't there insert it and if not compare the timestamp of existing vs new and update only if its new. want to do this without using lua script
-
Carl Zulauf almost 3 yearsRedis keys don't have timestamps, except maybe expiration times. If you are setting expiration timestamps on keys it's pretty easy to use
setnx
or other commands that includenx
(which basically means "not if exists"). Then when your expiring keys get deleted your inserts will take place, but not before then. --- Can also use two keys: one with the value and one with the timestamp and fetch that timestamp before updating the value key. Can be done with Lua or WATCH+EXEC if you need this to be atomic. --- This question could be it's own on stackoverflow.