-
-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Experimental] Add new
hmsetnx
lua command
Would be nice to have this in Redis core, but doesn't look like it's likely to happen. Ref. redis/redis#542 Note that there's another Lua implementation floating around (https://redisgreen.net/library/hmsetnx.html), but that doesn't account for a pre-existing hash key without the given fields.
- Loading branch information
1 parent
0723f7d
commit 9a31255
Showing
2 changed files
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
local hkey = KEYS[1]; | ||
|
||
if redis.call('exists', hkey) == 0 then | ||
redis.call('hmset', hkey, unpack(ARGV)); | ||
return 1; | ||
else | ||
local proceed = true; | ||
for i,x in ipairs(ARGV) do | ||
if (i % 2 ~= 0) then -- odd i => `x` is field | ||
if redis.call('hexists', hkey, x) == 1 then | ||
proceed = false; | ||
break; | ||
end | ||
end | ||
end | ||
|
||
if proceed then | ||
redis.call('hmset', hkey, unpack(ARGV)); | ||
return 1; | ||
else | ||
return 0; | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters