Skip to content

Commit c7d1587

Browse files
authored
fix(proxy-cache): keep different strategy response header consistency (#11048)
1 parent c0404c2 commit c7d1587

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

apisix/plugins/proxy-cache/memory.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,17 @@ function _M:get(key)
5252
end
5353

5454
-- If the key does not exist or has expired, then res_json will be nil.
55-
local res_json, err = self.dict:get(key)
55+
local res_json, err, stale = self.dict:get_stale(key)
5656
if not res_json then
5757
if not err then
5858
return nil, "not found"
5959
else
6060
return nil, err
6161
end
6262
end
63+
if stale then
64+
return nil, "expired"
65+
end
6366

6467
local res_obj, err = core.json.decode(res_json)
6568
if not res_obj then

apisix/plugins/proxy-cache/memory_handler.lua

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,19 @@ function _M.access(conf, ctx)
213213
end
214214

215215
if err then
216-
core.response.set_header("Apisix-Cache-Status", "MISS")
217-
if err ~= "not found" then
216+
if err == "expired" then
217+
core.response.set_header("Apisix-Cache-Status", "EXPIRED")
218+
219+
elseif err ~= "not found" then
220+
core.response.set_header("Apisix-Cache-Status", "MISS")
218221
core.log.error("failed to get from cache, err: ", err)
222+
219223
elseif conf.cache_control and cc["only-if-cached"] then
224+
core.response.set_header("Apisix-Cache-Status", "MISS")
220225
return 504
226+
227+
else
228+
core.response.set_header("Apisix-Cache-Status", "MISS")
221229
end
222230
return
223231
end

t/plugin/proxy-cache/memory.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ GET /hello
506506
--- response_body chop
507507
hello world!
508508
--- response_headers
509-
Apisix-Cache-Status: MISS
509+
Apisix-Cache-Status: EXPIRED
510510
511511
512512

0 commit comments

Comments
 (0)