/
shared_dict.lua
50 lines (40 loc) · 1004 Bytes
/
shared_dict.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
local ngx = ngx
local ngx_log = ngx.log
local ngx_WARN = ngx.WARN
local string_format = string.format
local setmetatable = setmetatable
local _M = {}
local mt = { __index = _M }
function _M.new(options)
if not options.name then
return nil, "shared dictionary name is mandatory"
end
local dict = ngx.shared[options.name]
if not dict then
return nil,
string_format("shared dictionary with name \"%s\" is not configured",
options.name)
end
return setmetatable({
dict = dict,
}, mt), nil
end
function _M.incr(self, key, delta, expiry)
local new_value, err, forcible = self.dict:incr(key, delta, 0, expiry)
if err then
return nil, err
end
if forcible then
ngx_log(ngx_WARN,
"shared dictionary is full, removed valid key(s) to store the new one")
end
return new_value, nil
end
function _M.get(self, key)
local value = self.dict:get(key)
if not value == nil then
return nil, "not found"
end
return value, nil
end
return _M