Skip to content

Instantly share code, notes, and snippets.

@Petelin
Last active June 22, 2018 09:41
Show Gist options
  • Save Petelin/30dc183220160039c602bac167f16a92 to your computer and use it in GitHub Desktop.
Save Petelin/30dc183220160039c602bac167f16a92 to your computer and use it in GitHub Desktop.
this lua script is used to in redis. it will set to the key if the value is bigger than the `updateat:{timestamp}`, if no udpateat founded update as normal
local a = redis.call('get',KEYS[1]);
if a and a:find("^updateat:") ~= nil then
-- this key already update by anther thread. so we test
if tonumber(string.sub(a, 10, string.len(a))) <= tonumber(KEYS[2]) then
return redis.call('set',KEYS[1], KEYS[3]);
end
return;
end
return redis.call('set',KEYS[1], KEYS[3]);
@Petelin
Copy link
Author

Petelin commented Jun 22, 2018

127.0.0.1:6379> set foo updateat:100

redis-cli --eval /tmp/a.lua foo 120 message --> success

redis-cli --eval /tmp/a.lua foo 80 message --> failed

redis-cli --eval /tmp/a.lua foo2 80 message --> success, since no foo2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment