HSet() object, if hash not exists #390
Replies: 3 comments 3 replies
-
Hi @numberinho, The most performant way is using a Lua script that first checks if |
Beta Was this translation helpful? Give feedback.
-
Hi @rueian That's a nice suggestion, thank you! Not being able to use rediscompat is unfortunate. Rueidiscompat's |
Beta Was this translation helpful? Give feedback.
-
Hi @rueian I think I’m trying to reinvent the wheel… Also, I can add some examples to argToSlice in the next days, if you want. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Goal: Set a Hash with multiple fields, only if the hash does not already exist.
Why: I want to store a "flattened" object (containing only primitives as fields). Each field in the hash maps to a object property.
As far as I can see I cannot use
HSetNX
, because it only checks for the field to not already exist. Also, I cannot chain multipleHSetNX
, because in concurrent access another routine may callHGetAll
and not the whole object is stored in Redis yet. SoHGetAll
will miss some fields and return an incomplete object. Therefore I need some blocking mechanism.Like a
HMSetNX
(this does not exist in Redis!), that would only set the fields if the hash does not exist.This is a bit counter-intuitive, because one may interpret it to only set the fields if the fields do not exist.
For my understanding the function would look like:
Can you give me any suggestions on how to do that using redis/rueidis? Maybe I need to chain the check for
exist
into a transaction with theHSet
commands, to make sure nothing changed betweenexist
andHSet
?Thank you!
Beta Was this translation helpful? Give feedback.
All reactions