-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add multi-lock support for atomic multi-lock. (#70)
- Loading branch information
1 parent
788a79b
commit cd93b2b
Showing
8 changed files
with
297 additions
and
63 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
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
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,40 @@ | ||
-- obtain.lua: arguments => [value, tokenLen, ttl] | ||
-- Obtain.lua try to set provided keys's with value and ttl if they do not exists. | ||
-- Keys can be overriden if they already exists and the correct value+tokenLen is provided. | ||
|
||
local function pexpire(ttl) | ||
-- Update keys ttls. | ||
for _, key in ipairs(KEYS) do | ||
redis.call("pexpire", key, ttl) | ||
end | ||
end | ||
|
||
-- canOverrideLock check either or not the provided token match | ||
-- previously set lock's tokens. | ||
local function canOverrideKeys() | ||
local offset = tonumber(ARGV[2]) | ||
|
||
for _, key in ipairs(KEYS) do | ||
if redis.call("getrange", key, 0, offset-1) ~= string.sub(ARGV[1], 1, offset) then | ||
return false | ||
end | ||
end | ||
return true | ||
end | ||
|
||
-- Prepare mset arguments. | ||
local setArgs = {} | ||
for _, key in ipairs(KEYS) do | ||
table.insert(setArgs, key) | ||
table.insert(setArgs, ARGV[1]) | ||
end | ||
|
||
if redis.call("msetnx", unpack(setArgs)) ~= 1 then | ||
if canOverrideKeys() == false then | ||
return false | ||
end | ||
redis.call("mset", unpack(setArgs)) | ||
end | ||
|
||
pexpire(ARGV[3]) | ||
return redis.status_reply("OK") |
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,21 @@ | ||
-- pttl.lua: => Arguments: [value] | ||
-- pttl.lua returns provided keys's ttls if all their values match the input. | ||
|
||
-- Check all keys values matches provided input. | ||
local values = redis.call("mget", unpack(KEYS)) | ||
for i, _ in ipairs(KEYS) do | ||
if values[i] ~= ARGV[1] then | ||
return false | ||
end | ||
end | ||
|
||
-- Find and return shortest TTL among keys. | ||
local minTTL = 0 | ||
for _, key in ipairs(KEYS) do | ||
local ttl = redis.call("pttl", key) | ||
-- Note: ttl < 0 probably means the key no longer exists. | ||
if ttl > 0 and (minTTL == 0 or ttl < minTTL) then | ||
minTTL = ttl | ||
end | ||
end | ||
return minTTL |
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
Oops, something went wrong.