Repository navigation
Expand file tree
/
Copy pathcache.lua
More file actions
179 lines (128 loc) · 3.8 KB
/
Copy pathcache.lua
File metadata and controls
179 lines (128 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
10000
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
local kong_mlcache = require "kong.mlcache"
local type = type
local max = math.max
local ngx_log = ngx.log
local ngx_now = ngx.now
local ERR = ngx.ERR
local NOTICE = ngx.NOTICE
local DEBUG = ngx.DEBUG
local SHM_CACHE = "kong_cache"
--[[
Hypothesis
----------
Item size: 1024 bytes
Max memory limit: 500 MiBs
LRU size must be: (500 * 2^20) / 1024 = 512000
Floored: 500.000 items should be a good default
--]]
local LRU_SIZE = 5e5
local _init
local function log(lvl, ...)
return ngx_log(lvl, "[DB cache] ", ...)
end
local _M = {}
local mt = { __index = _M }
function _M.new(opts)
if _init then
return error("kong.cache was already created")
end
-- opts validation
opts = opts or {}
if not opts.cluster_events then
return error("opts.cluster_events is required")
end
if not opts.worker_events then
return error("opts.worker_events is required")
end
if opts.propagation_delay and type(opts.propagation_delay) ~= "number" then
return error("opts.propagation_delay must be a number")
end
if opts.ttl and type(opts.ttl) ~= "number" then
return error("opts.ttl must be a number")
end
if opts.neg_ttl and type(opts.neg_ttl) ~= "number" then
return error("opts.neg_ttl must be a number")
end
if opts.resty_lock_opts and type(opts.resty_lock_opts) ~= "table" then
return error("opts.resty_lock_opts must be a table")
end
local mlcache, err = kong_mlcache.new(SHM_CACHE, opts.worker_events, {
lru_size = LRU_SIZE,
ttl = max(opts.ttl or 3600, 0),
neg_ttl = max(opts.neg_ttl or 300, 0),
resty_lock_opts = opts.resty_lock_opts,
})
if not mlcache then
return nil, "failed to instantiate mlcache: " .. err
end
local self = {
propagation_delay = max(opts.propagation_delay or 0, 0),
cluster_events = opts.cluster_events,
mlcache = mlcache,
}
local ok, err = self.cluster_events:subscribe("invalidations", function(key)
log(DEBUG, "received invalidate event from cluster for key: '", key, "'")
self:invalidate_local(key)
end)
if not ok then
return nil, "failed to subscribe to invalidations cluster events " ..
"channel: " .. err
end
_init = true
return setmetatable(self, mt)
end
function _M:get(key, opts, cb, ...)
if type(key) ~= "string" then
return error("key must be a string")
end
--log(DEBUG, "get from key: ", key)
local v, err = self.mlcache:get(key, opts, cb, ...)
if err then
return nil, "failed to get from node cache: " .. err
end
return v
end
function _M:probe(key)
if type(key) ~= "string" then
return error("key must be a string")
end
local ttl, err, v = self.mlcache:probe(key)
if err then
return nil, "failed to probe from node cache: " .. err
end
return ttl, nil, v
end
function _M:invalidate_local(key)
if type(key) ~= "string" then
return error("key must be a string")
end
log(DEBUG, "invalidating (local): '", key, "'")
local ok, err = self.mlcache:delete(key)
if not ok then
log(ERR, "failed to delete entity from node cache: ", err)
end
end
function _M:invalidate(key)
if type(key) ~= "string" then
return error("key must be a string")
end
self:invalidate_local(key)
local nbf
if self.propagation_delay > 0 then
nbf = ngx_now() + self.propagation_delay
end
log(DEBUG, "broadcasting (cluster) invalidation for key: '", key, "' ",
"with nbf: '", nbf or "none", "'")
local ok, err = self.cluster_events:broadcast("invalidations", key, nbf)
if not ok then
log(ERR, "failed to broadcast cached entity invalidation: ", err)
end
end
function _M:purge()
log(NOTICE, "purging (local) cache")
local ok, err = self.mlcache:purge()
if not ok then
log(ERR, "failed to purge cache: ", err)
end
end
return _M