-
Notifications
You must be signed in to change notification settings - Fork 181
/
redis.lua
134 lines (106 loc) · 2.95 KB
/
redis.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
local redis = require "resty.redis"
local _M = {}
local function prefixed_key(self, key)
if self.options["prefix"] then
return self.options["prefix"] .. ":" .. key
else
return key
end
end
function _M.new(auto_ssl_instance)
local options = auto_ssl_instance:get("redis") or {}
if not options["host"] then
options["host"] = "127.0.0.1"
end
if not options["port"] then
options["port"] = 6379
end
return setmetatable({ options = options }, { __index = _M })
end
function _M.get_connection(self)
local connection = ngx.ctx.auto_ssl_redis_connection
if connection then
return connection
end
connection = redis:new()
local ok, err
local connect_options = self.options["connect_options"] or {}
if self.options["socket"] then
ok, err = connection:connect(self.options["socket"], connect_options)
else
ok, err = connection:connect(self.options["host"], self.options["port"], connect_options)
end
if not ok then
return false, err
end
if self.options["auth"] then
ok, err = connection:auth(self.options["auth"])
if not ok then
return false, err
end
end
if self.options["db"] then
ok, err = connection:select(self.options["db"])
if not ok then
return false, err
end
end
ngx.ctx.auto_ssl_redis_connection = connection
return connection
end
function _M.setup()
end
function _M.get(self, key)
local connection, connection_err = self:get_connection()
if connection_err then
return nil, connection_err
end
local res, err = connection:get(prefixed_key(self, key))
if res == ngx.null then
res = nil
end
return res, err
end
function _M.set(self, key, value, options)
local connection, connection_err = self:get_connection()
if connection_err then
return false, connection_err
end
key = prefixed_key(self, key)
local ok, err = connection:set(key, value)
if ok then
if options and options["exptime"] then
local _, expire_err = connection:expire(key, options["exptime"])
if expire_err then
ngx.log(ngx.ERR, "auto-ssl: failed to set expire: ", expire_err)
end
end
end
return ok, err
end
function _M.delete(self, key)
local connection, connection_err = self:get_connection()
if connection_err then
return false, connection_err
end
return connection:del(prefixed_key(self, key))
end
function _M.keys_with_suffix(self, suffix)
local connection, connection_err = self:get_connection()
if connection_err then
return false, connection_err
end
local keys, err = connection:keys(prefixed_key(self, "*" .. suffix))
if keys and self.options["prefix"] then
local unprefixed_keys = {}
-- First character past the prefix and a colon
local offset = string.len(self.options["prefix"]) + 2
for _, key in ipairs(keys) do
local unprefixed = string.sub(key, offset)
table.insert(unprefixed_keys, unprefixed)
end
keys = unprefixed_keys
end
return keys, err
end
return _M