Skip to content

Instantly share code, notes, and snippets.

@kekekeks
Last active January 3, 2016 08:39
Show Gist options
  • Save kekekeks/8437490 to your computer and use it in GitHub Desktop.
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>
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