Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(purge) prevent purging when using a custom LRU cache #79

Merged
merged 1 commit into from
Mar 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions lib/resty/mlcache.lua
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,10 @@ function _M:purge(flush_expired)
error("no ipc to propagate purge, specify opts.ipc_shm or opts.ipc", 2)
end

if LRU_INSTANCES[self.name] ~= self.lru then
error("cannot purge when using custom LRU cache", 2)
end

-- clear shm first
self.dict:flush_all()

Expand Down
45 changes: 38 additions & 7 deletions t/08-purge.t
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ no ipc to propagate purge, specify opts.ipc_shm or opts.ipc



=== TEST 2: purge() deletes all items (sanity 1/2)
=== TEST 2: purge() deletes all items from L1 + L2 (sanity 1/2)
--- http_config eval: $::HttpConfig
--- config
location = /t {
Expand Down Expand Up @@ -87,7 +87,7 @@ ok



=== TEST 3: purge() deletes all items (sanity 2/2)
=== TEST 3: purge() deletes all items from L1 (sanity 2/2)
--- http_config eval: $::HttpConfig
--- config
location = /t {
Expand Down Expand Up @@ -128,7 +128,38 @@ ok



=== TEST 4: purge() deletes all items from shm_miss is specified
=== TEST 4: purge() deletes all items from L1 with a custom LRU
--- http_config eval: $::HttpConfig
--- config
location = /t {
content_by_lua_block {
local mlcache = require "resty.mlcache"
local lrucache = require "resty.lrucache"

local cache = assert(mlcache.new("my_mlcache", "cache_shm", {
ipc_shm = "ipc_shm",
lru = lrucache.new(10),
}))

local pok, perr = pcall(cache.purge, cache)
if not pok then
ngx.say(perr)
return
end

ngx.say("ok")
}
}
--- request
GET /t
--- response_body
cannot purge when using custom LRU cache
--- no_error_log
[error]



=== TEST 5: purge() deletes all items from shm_miss is specified
--- http_config eval: $::HttpConfig
--- config
location = /t {
Expand Down Expand Up @@ -176,7 +207,7 @@ ok



=== TEST 5: purge() does not call shm:flush_expired() by default
=== TEST 6: purge() does not call shm:flush_expired() by default
--- http_config eval: $::HttpConfig
--- config
location = /t {
Expand Down Expand Up @@ -211,7 +242,7 @@ flush_expired called with 'max_count'



=== TEST 6: purge() calls shm:flush_expired() if argument specified
=== TEST 7: purge() calls shm:flush_expired() if argument specified
--- http_config eval: $::HttpConfig
--- config
location = /t {
Expand Down Expand Up @@ -248,7 +279,7 @@ flush_expired called with 'max_count': nil



=== TEST 7: purge() calls shm:flush_expired() if shm_miss is specified
=== TEST 8: purge() calls shm:flush_expired() if shm_miss is specified
--- http_config eval: $::HttpConfig
--- config
location = /t {
Expand Down Expand Up @@ -287,7 +318,7 @@ flush_expired called with 'max_count': nil



=== TEST 8: purge() calls broadcast() on purge channel
=== TEST 9: purge() calls broadcast() on purge channel
--- http_config eval: $::HttpConfig
--- config
location = /t {
Expand Down