Skip to content

Commit

Permalink
refactor(dns): implement mlcache:get callback
Browse files Browse the repository at this point in the history
  • Loading branch information
chobits committed Dec 20, 2023
1 parent 1d298cb commit bf83edc
Show file tree
Hide file tree
Showing 7 changed files with 272 additions and 284 deletions.
33 changes: 23 additions & 10 deletions kong/globalpatches.lua
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,10 @@ return function(options)
-- See https://github.com/openresty/resty-cli/pull/12
-- for a definitive solution of using shms in CLI
local SharedDict = {}
local function set(data, key, value, expire_at)
local function set(data, key, value, expire_at, flags)
data[key] = {
value = value,
info = {expire_at = expire_at}
info = {expire_at = expire_at, flags=flags}
}
end
function SharedDict:new()
Expand All @@ -268,20 +268,34 @@ return function(options)
return 0
end
function SharedDict:get(key)
return self.data[key] and self.data[key].value, nil
local v = self.data[key]
if v and not v.info.expired then
return v.value, v.info.flags
end

return nil
end
SharedDict.get_stale = SharedDict.get
function SharedDict:set(key, value, exptime)
function SharedDict:get_stale(key)
local v = self.data[key]
if v then
return v.value, v.info.flags, v.info.expired
end
return nil
end
function SharedDict:set(key, value, exptime, flags)
local expire_at = nil

if exptime then
ngx.timer.at(exptime, function()
self.data[key] = nil
local v = self.data[key]
if v then
v.info.expired = true
end
end)
expire_at = ngx.now() + exptime
end

set(self.data, key, value, expire_at)
set(self.data, key, value, expire_at, flags)
return true, nil, false
end
SharedDict.safe_set = SharedDict.set
Expand Down Expand Up @@ -369,11 +383,10 @@ return function(options)
return 0
else
local remaining = expire_at - ngx.now()
if remaining < 0 then
if remaining == 0 then
return nil, "not found"
else
return remaining
end
return remaining
end
end
end
Expand Down
Loading

0 comments on commit bf83edc

Please sign in to comment.