Skip to content

Commit 36c1631

Browse files
committed
Split local store by usage to limit fragmentation
Split by usage into 3 sections One for locks: cache_locks One for metadata: cache_metadata One for entities: cache_entities
1 parent 226af35 commit 36c1631

3 files changed

Lines changed: 27 additions & 12 deletions

File tree

lib/luchador/cache.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,9 @@ function Cache:serve()
151151
self.storage:keepalive()
152152

153153
if debug_headers and self.req_headers['clear-ngx-cache'] then
154-
ngx.shared.cache:flush_all()
154+
ngx.shared.cache_locks:flush_all()
155+
ngx.shared.cache_metadata:flush_all()
156+
ngx.shared.cache_entities:flush_all()
155157
end
156158

157159
self.storage:flush_expired()

lib/luchador/storage.lua

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,36 +121,37 @@ function Storage:compress(content, content_type, use_best)
121121
end
122122

123123
function Storage:get_skip()
124-
return ngx.shared.cache:get(self:page_key() .. 'skip')
124+
return ngx.shared.cache_locks:get(self:page_key() .. 'skip')
125125
end
126126

127127
function Storage:set_skip()
128-
local r, err = ngx.shared.cache:set(self:page_key() .. 'skip', true, 30)
128+
local r, err = ngx.shared.cache_locks:set(self:page_key() .. 'skip', true, 30)
129129
return r
130130
end
131131

132132
function Storage:get_lock(timeout)
133-
local r, err = ngx.shared.cache:add(self:page_key() .. 'lock', true, timeout)
133+
local r, err = ngx.shared.cache_locks:add(self:page_key() .. 'lock', true, timeout)
134134
return r
135135
end
136136

137137
function Storage:release_lock()
138-
return ngx.shared.cache:delete(self:page_key() .. 'lock')
138+
return ngx.shared.cache_locks:delete(self:page_key() .. 'lock')
139139
end
140140

141-
function Storage:set(key, val, ttl)
141+
function Storage:set(key, val, ttl, is_metadata)
142142
key = namespace .. key
143143

144144
val = {val = val, ttl = ttl, created = ngx.time()}
145145
val = serializer.serialize(val)
146146

147-
ngx.shared.cache:set(key, self.datastore:set(key, val, ttl), ttl)
147+
self:get_local_store(is_metadata):set(key, self.datastore:set(key, val, ttl), ttl)
148148
end
149149

150-
function Storage:get(key)
150+
function Storage:get(key, is_metadata)
151151
key = namespace .. key
152152
local locally_cached = false
153-
local entry = ngx.shared.cache:get(key)
153+
local local_storage = self:get_local_store(is_metadata)
154+
local entry = local_storage:get(key)
154155

155156
if entry then
156157
locally_cached = true
@@ -168,16 +169,26 @@ function Storage:get(key)
168169
local age = (ngx.time() - thawed.created)
169170
local remaining_ttl = thawed.ttl - age
170171
if remaining_ttl > 0 then
171-
ngx.shared.cache:set(key, entry, remaining_ttl)
172+
local_storage:set(key, entry, remaining_ttl)
172173
end
173174
end
174175

175176
return thawed.val, locally_cached
176177
end
177178
end
178179

180+
function Storage:get_local_store(is_metadata)
181+
if is_metadata then
182+
return ngx.shared.cache_metadata
183+
else
184+
return ngx.shared.cache_entities
185+
end
186+
end
187+
179188
function Storage:flush_expired()
180-
ngx.shared.cache:flush_expired(10)
189+
ngx.shared.cache_locks:flush_expired(5)
190+
ngx.shared.cache_metadata:flush_expired(5)
191+
ngx.shared.cache_entities:flush_expired(5)
181192
end
182193

183194
function Storage:keepalive()

test/nginx/nginx.conf

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ events {
66

77
http {
88
lua_package_path "/usr/local/lib/lua/?.lua;$prefix/../../lib/?.lua;;";
9-
lua_shared_dict cache 10m;
9+
lua_shared_dict cache_locks 1m;
10+
lua_shared_dict cache_metadata 10m;
11+
lua_shared_dict cache_entities 10m;
1012
keepalive_timeout 65;
1113

1214
server {

0 commit comments

Comments
 (0)