Skip to content
Permalink
Browse files

fix(storage) typo in redis storage, unified interface for file

  • Loading branch information...
fffonion committed Oct 23, 2019
1 parent 21029c9 commit 2dd6cfa2c77ab36d0254e1fedb832f2ecabcec99
Showing with 42 additions and 20 deletions.
  1. +16 −3 lib/resty/acme/storage/file.lua
  2. +26 −17 lib/resty/acme/storage/redis.lua
@@ -33,9 +33,21 @@ function _M:set(k, v)
file:close()
end

function exists(f)
-- TODO: check for existence, not just able to open or not
local f, err = io.open(f, "rb")
if f then
f:close()
end
return err == nil
end

function _M:delete(k)
local f = regulate_filename(self.dir, k)
local err = os.remove(f)
if not exists(f) then
return nil, nil
end
local ok, err = os.remove(f)
if err then
return err
end
@@ -45,8 +57,9 @@ function _M:get(k)
local f = regulate_filename(self.dir, k)
local file, err = io.open(f, "rb")
if err then
ngx.log(ngx.ERR, "can't read file: ", err)
-- TODO: return nil, nil if not found
return nil, err
return nil, nil
end
local output, err = file:read("*a")
if err then
@@ -57,7 +70,7 @@ function _M:get(k)
end

function _M:list(prefix)
error("nyi")
return {}, "nyi"
end

return _M
@@ -4,41 +4,49 @@ local _M = {}
local mt = {__index = _M}

function _M.new(conf)
conf = conf or {}
local self =
setmetatable(
{
host = conf.host or '127.0.0.1',
port = conf.port or 6379,
database = conf.database,
auth = conf.auth,
},
mt
)
return self, nil
end

local function op(self, op, ...)
local client = ngx.ctx.acme_redis_storage
if not client then
client = redis:new()
local ok, err = client:connect(
self.host,
self.port
)
if not ok then
return nil, err
local ok, err
local client = redis:new()
client:set_timeouts(1000, 1000, 1000) -- 1 sec

ok, err = client:connect(
self.host,
self.port
)
if not ok then
return nil, err
end

if self.auth then
ok, err = client:auth(self.auth)
if err then
return nil, "authentication failed " .. err
end
ngx.ctx.acme_redis_storage = client
end

if conf.database then
local ok, err = client:select(database)
if self.database then
ok, err = client:select(self.database)
if not ok then
return nil, "can't select database " .. err
end
end

ok, err = client[op](client, ...)
client:set_keepalive(10000, 100)
client:close()
return ok, err
end

@@ -50,7 +58,7 @@ function _M:set(k, v)
end

function _M:delete(k)
local ok, err = op(self, 'delete', k)
local ok, err = op(self, 'del', k)
if err then
return err
end
@@ -65,9 +73,10 @@ function _M:get(k)
end

local empty_table = {}
function _M:keys(prefix)
local res, err = op(self, 'keys', prefix)
if res == ngx.null then
function _M:list(prefix)
prefix = prefix or ""
local res, err = op(self, 'keys', prefix .. "*")
if not res or res == ngx.null then
return empty_table, err
end
return res, err

0 comments on commit 2dd6cfa

Please sign in to comment.
You can’t perform that action at this time.