Created
July 21, 2017 09:05
-
-
Save fannheyward/707226478542bea0d4dc8d27299fa33d to your computer and use it in GitHub Desktop.
wrapper of lua-resty-ssdb
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
#!/usr/bin/env lua | |
local SSDB = require 'resty.ssdb' | |
local _M = { | |
_VERSION = '0.0.1' | |
} | |
local mt = { | |
__index = _M | |
} | |
local commands = { | |
-- hashmap | |
'hget', 'hset', 'hgetall', | |
} | |
local function do_commands(self, cmd, ...) | |
local cli = SSDB.new() | |
cli:set_timeout(self.opts.timeout or 10000) | |
local ok, err = cli:connect(self.opts.host, self.opts.port) | |
if not ok then | |
return nil, err | |
end | |
local func = cli[cmd] | |
local result, e = func(cli, ...) | |
if e then | |
return nil, e | |
end | |
cli:set_keepalive(10000, 100) | |
return result, nil | |
end | |
function _M.new(self, opts) | |
if not opts.host or not opts.port then | |
return nil, 'no host/port found' | |
end | |
for _,cmd in ipairs(commands) do | |
_M[cmd] = function (self, ...) return do_commands(self, cmd, ...) end | |
end | |
return setmetatable({opts = opts}, mt) | |
end | |
return _M |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment