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: provide error instead of nil panic when cache_zone is missing #10138

Merged
merged 10 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
10 changes: 10 additions & 0 deletions apisix/plugins/proxy-cache/memory.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ end


function _M:set(key, obj, ttl)
if self.dict == nil then
return nil, "no cache_zone provided"
end

local obj_json = core.json.encode(obj)
if not obj_json then
return nil, "could not encode object"
Expand All @@ -43,6 +47,9 @@ end


function _M:get(key)
if self.dict == nil then
return nil, "no cache_zone provided"
end
Revolyssup marked this conversation as resolved.
Show resolved Hide resolved
-- If the key does not exist or has expired, then res_json will be nil.
local res_json, err = self.dict:get(key)
if not res_json then
Expand All @@ -63,6 +70,9 @@ end


function _M:purge(key)
if self.dict == nil then
return nil, "no cache_zone provided"
end
self.dict:delete(key)
end

Expand Down
54 changes: 54 additions & 0 deletions t/plugin/proxy-cache/memory.t
Original file line number Diff line number Diff line change
Expand Up @@ -661,3 +661,57 @@ GET /hello
--- more_headers
Cache-Control: only-if-cached
--- error_code: 504



=== TEST 36: configure plugin without memory_cache zone
Revolyssup marked this conversation as resolved.
Show resolved Hide resolved
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"plugins": {
"proxy-cache": {
"cache_strategy": "memory",
"cache_key":["$host","$uri"],
"cache_bypass": ["$arg_bypass"],
"cache_control": true,
"cache_method": ["GET"],
"cache_ttl": 10,
"cache_http_status": [200]
}
},
"upstream": {
"nodes": {
"127.0.0.1:1986": 1
},
"type": "roundrobin"
},
"uri": "/hello"
}]]
)

if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- error_code: 200
--- response_body
passed



=== TEST 37: hit route
--- request
GET /hello
--- grep_error_log eval
qr/no cache_zone provided/
--- grep_error_log_out
no cache_zone provided
no cache_zone provided
kayx23 marked this conversation as resolved.
Show resolved Hide resolved
kayx23 marked this conversation as resolved.
Show resolved Hide resolved
Loading