增加redis类进行redis数据库的操作
This commit is contained in:
parent
5e6777e48b
commit
5660d561be
62
src/share/redis.lua
Normal file
62
src/share/redis.lua
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
local redis = require("resty.redis")
|
||||
local conf = require('config')
|
||||
|
||||
local _M = setmetatable({}, {__index=function(self, key)
|
||||
local red = redis:new()
|
||||
local ok,err = red:connect(conf.REDIS.HOST, conf.REDIS.POST, conf.REDIS.PASSWORD)
|
||||
if not ok then
|
||||
ngx.log(ngx.ERR, err)
|
||||
end
|
||||
if key == 'red' then
|
||||
return red
|
||||
end
|
||||
end})
|
||||
|
||||
function _M:set(key, value, time)
|
||||
local ok, err = self.red:set(key, value)
|
||||
if not ok then
|
||||
return false, "redis failed to set data: " .. err
|
||||
end
|
||||
if time then
|
||||
ok,err = self.red:expire(key, time) -- default expire time is seconds
|
||||
if not ok then
|
||||
return false,err
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
function _M:get(key)
|
||||
local value = self.red:get(key)
|
||||
if value == ngx.null then
|
||||
return nil
|
||||
else
|
||||
return value
|
||||
end
|
||||
end
|
||||
|
||||
function _M:del(key)
|
||||
return self.red:del(key)
|
||||
end
|
||||
|
||||
function _M:expire(key, time)
|
||||
local ok,err = self.red:expire(key, time) -- default time is seconds
|
||||
if not ok then
|
||||
return false,err
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
function _M:incr(key)
|
||||
local ok,err = self.red:incr(key)
|
||||
if not ok then
|
||||
return false, err
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
function _M:ttl(key)
|
||||
return self.red:ttl(key)
|
||||
end
|
||||
|
||||
return _M
|
||||
Loading…
Reference in New Issue
Block a user