Skip to content

Instantly share code, notes, and snippets.

@bilalatli
Last active March 31, 2022 19:24
Show Gist options
  • Select an option

  • Save bilalatli/e4d81b4a4ffb2f10fbe07bef386c3d2f to your computer and use it in GitHub Desktop.

Select an option

Save bilalatli/e4d81b4a4ffb2f10fbe07bef386c3d2f to your computer and use it in GitHub Desktop.
Redis Multi Lock

Installation

Install this lua script into redis server.

Bash Install

redis-cli SCRIPT LOAD "$(cat redis-multilock.min.lua)"

// This command return your script name for execute

For example is your lua hash : e53b6e18671f437fd1eb0012776e1cbb02d734c1

Bash Execution

// redis-cli EVALSHA e53b6e18671f437fd1eb0012776e1cbb02d734c1 {KeyCount} {Key, [Key ...]} {Timeout Millisecond}
redis-cli EVALSHA e53b6e18671f437fd1eb0012776e1cbb02d734c1 3 "key:1" "key:2" "key:3" 10000

If command returns 0; This means all these keys are locked from the moment you run the command ...
If command returns 1; This means you have to wait for all the keys to be unlocked
for _, key in ipairs(KEYS) do
local isExist = redis.call('EXISTS', key)
if isExist == 1 then
return 1
end
end
for _, key in ipairs(KEYS) do
redis.call('PSETEX', key, ARGV[1], 1);
end
return 0
for a,b in ipairs(KEYS)do local c=redis.call('EXISTS',b)if c==1 then return 1 end end;for a,b in ipairs(KEYS)do redis.call('PSETEX',b,ARGV[1],1)end;return 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment