Last active
January 3, 2016 08:39
-
-
Save kekekeks/8437490 to your computer and use it in GitHub Desktop.
Interlocked compare exchange for Redis
EVAL "<script>" 1 <key> <sequenceId> <keyCount> <key1> <value1> <key2> <value2>
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
local oldSeq; local oldData; | |
oldSeq = redis.call('hget',KEYS[1], 'sequence') | |
oldData = redis.call('hgetall',KEYS[1]) | |
if oldSeq and tonumber(oldSeq) >= tonumber(ARGV[1]) then | |
return -1 | |
else | |
redis.call('hset', KEYS[1], 'sequence', ARGV[1]); | |
local maxi = tonumber(ARGV[2])*2 + 3; | |
for idx = 3, maxi, 2 do | |
redis.call('hset', KEYS[1], ARGV[idx], ARGV[idx+1]); | |
end | |
return oldData; | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment