-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
yumaojun03
committed
Nov 4, 2024
1 parent
f0a0297
commit fea8488
Showing
10 changed files
with
190 additions
and
12 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
-- refresh.lua: => Arguments: [value, ttl] | ||
-- refresh.lua refreshes 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 | ||
|
||
-- Update keys ttls. | ||
for _, key in ipairs(KEYS) do | ||
redis.call("pexpire", key, ARGV[2]) | ||
end | ||
|
||
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,16 @@ | ||
|
||
-- release.lua: => Arguments: [value] | ||
-- Release.lua deletes provided keys 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 | ||
|
||
-- Delete keys. | ||
redis.call("del", unpack(KEYS)) | ||
|
||
return redis.status_reply("OK") |