Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/resty/lrucache.lua
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ local function ptr2num(ptr)
end


function _M.new(size)
function _M.new(size, evict_cb)
if size < 1 then
return nil, "size too small"
end
Expand All @@ -164,6 +164,7 @@ function _M.new(size)
node2key = {},
num_items = 0,
max_items = size,
evict_cb = evict_cb,
}
return setmetatable(self, mt)
end
Expand Down Expand Up @@ -241,6 +242,9 @@ function _M.set(self, key, value, ttl, flags)
-- print(key, ": evicting oldkey: ", oldkey, ", oldnode: ",
-- tostring(node))
if oldkey then
if self.evict_cb then
self.evict_cb(oldkey, hasht[oldkey])
end
hasht[oldkey] = nil
key2node[oldkey] = nil
end
Expand Down
6 changes: 5 additions & 1 deletion lib/resty/lrucache/pureffi.lua
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ local mt = { __index = _M }
-- load-factor is specified, it will be clamped to the range of [0.1, 1](i.e.
-- if load-factor is greater than 1, it will be saturated to 1, likewise,
-- if load-factor is smaller than 0.1, it will be clamped to 0.1).
function _M.new(size, load_factor)
function _M.new(size, load_factor, evict_cb)
if size < 1 then
return nil, "size too small"
end
Expand Down Expand Up @@ -350,6 +350,7 @@ function _M.new(size, load_factor)
val_v = new_tab(size, 0),
bucket_v = ffi_new(int_array_t, bucket_sz),
num_items = 0,
evict_cb = evict_cb,
}
-- "note_v" is an array of all the nodes used in the LRU queue. Exprpession
-- node_v[i] evaluates to the element of ID "i".
Expand Down Expand Up @@ -526,6 +527,9 @@ function _M.set(self, key, value, ttl, flags)
-- evict the least recently used key
-- assert(not queue_is_empty(self.cache_queue))
node = queue_last(self.cache_queue)
if self.evict_cb then
self.evict_cb(self.key_v[node.id], self.val_v[node.id])
end
remove_key(self, self.key_v[node.id])
else
-- take a free queue node
Expand Down
31 changes: 31 additions & 0 deletions t/009-evict-call-back.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# vim:set ft= ts=4 sw=4 et fdm=marker:
use lib '.';
use t::TestLRUCache;

repeat_each(2);

plan tests => repeat_each() * (blocks() * 3);

no_long_string();
run_tests();

__DATA__

=== TEST 1: evict with call back
--- config
location = /t {
content_by_lua '
local function evict_cb(k,v)
ngx.say(k, v)
end
local lrucache = require "resty.lrucache"
local c = lrucache.new(1, evict_cb)

collectgarbage()

c:set("dog", 12)
c:set("cat", 14)
';
}
--- response_body
dog12
31 changes: 31 additions & 0 deletions t/100-pureffi/009-evict-call-back.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# vim:set ft= ts=4 sw=4 et fdm=marker:
use lib '.';
use t::TestLRUCache;

repeat_each(2);

plan tests => repeat_each() * (blocks() * 3);

no_long_string();
run_tests();

__DATA__

=== TEST 1: evict with call back
--- config
location = /t {
content_by_lua '
local function evict_cb(k,v)
ngx.say(k, v)
end
local lrucache = require "resty.lrucache.pureffi"
local c = lrucache.new(1, nil, evict_cb)

collectgarbage()

c:set("dog", 12)
c:set("cat", 14)
';
}
--- response_body
dog12