You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by sp...@apache.org on 2022/06/01 11:30:02 UTC
[apisix] branch master updated: fix(proxy-cache): allow nil ctx vars in cache key (#7168)
This is an automated email from the ASF dual-hosted git repository.
spacewander pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git
The following commit(s) were added to refs/heads/master by this push:
new 94e061663 fix(proxy-cache): allow nil ctx vars in cache key (#7168)
94e061663 is described below
commit 94e061663b351f314ee5406fc114dd540c304666
Author: PiteChen <49...@users.noreply.github.com>
AuthorDate: Wed Jun 1 19:29:55 2022 +0800
fix(proxy-cache): allow nil ctx vars in cache key (#7168)
---
apisix/plugins/proxy-cache/util.lua | 2 +-
docs/en/latest/plugins/proxy-cache.md | 4 +++
docs/zh/latest/plugins/proxy-cache.md | 2 +-
t/plugin/proxy-cache/disk.t | 50 +++++++++++++++++++++++++++++++++++
4 files changed, 56 insertions(+), 2 deletions(-)
diff --git a/apisix/plugins/proxy-cache/util.lua b/apisix/plugins/proxy-cache/util.lua
index f20d2fc21..26c6e814b 100644
--- a/apisix/plugins/proxy-cache/util.lua
+++ b/apisix/plugins/proxy-cache/util.lua
@@ -37,7 +37,7 @@ function _M.generate_complex_value(data, ctx)
core.log.info("proxy-cache complex value index-", i, ": ", value)
if string.byte(value, 1, 1) == string.byte('$') then
- tmp[i] = ctx.var[string.sub(value, 2)]
+ tmp[i] = ctx.var[string.sub(value, 2)] or ""
else
tmp[i] = value
end
diff --git a/docs/en/latest/plugins/proxy-cache.md b/docs/en/latest/plugins/proxy-cache.md
index a21d87089..0539c0711 100644
--- a/docs/en/latest/plugins/proxy-cache.md
+++ b/docs/en/latest/plugins/proxy-cache.md
@@ -53,6 +53,10 @@ The cache expiration time cannot be configured dynamically. It can only be set b
If the Upstream service is not available and APISIX returns a 502 or 504 status code, it will be cached for 10s.
+Variables (start with `$`) can be specified in `cache_key`, `cache_bypass` and `no_cache`. It's worth mentioning that the variable value will be an empty string if it doesn't exist.
+
+You can also combine a number of variables and strings (constants), by writing them into an array, eventually, variables will be parsed and stitched together with strings.
+
:::
## Enabling the Plugin
diff --git a/docs/zh/latest/plugins/proxy-cache.md b/docs/zh/latest/plugins/proxy-cache.md
index b2db7c724..b22e4eb1c 100644
--- a/docs/zh/latest/plugins/proxy-cache.md
+++ b/docs/zh/latest/plugins/proxy-cache.md
@@ -45,7 +45,7 @@ title: proxy-cache
| no_cache | array[string] | 可选 | | | 是否缓存数据,可以使用变量,需要注意当此参数的值不为空或非'0'时将不会缓存数据 |
| cache_ttl | integer | 可选 | 300 秒 | | 当选项 cache_control 未开启或开启以后服务端没有返回缓存控制头时,提供的默认缓存时间 |
-注:变量以$开头,也可以使用变量和字符串的结合,但是需要以数组的形式分开写,最终变量被解析后会和字符串拼接在一起。
+注:变量以$开头,不存在时等价于空字符串。也可以使用变量和字符串的结合,但是需要以数组的形式分开写,最终变量被解析后会和字符串拼接在一起。
在 `conf/config.yaml` 文件中的配置示例:
diff --git a/t/plugin/proxy-cache/disk.t b/t/plugin/proxy-cache/disk.t
index 954f967f7..98e6e1d8b 100644
--- a/t/plugin/proxy-cache/disk.t
+++ b/t/plugin/proxy-cache/disk.t
@@ -707,3 +707,53 @@ GET /t
--- error_code: 400
--- response_body eval
qr/failed to check the configuration of plugin proxy-cache err/
+
+
+
+=== TEST 28: nil vars for cache_key
+--- 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_key": ["$arg_foo", "$arg_bar", "$arg_baz"],
+ "cache_zone": "disk_cache_one",
+ "cache_bypass": ["$arg_bypass"],
+ "cache_method": ["GET"],
+ "cache_http_status": [200],
+ "hide_cache_headers": true,
+ "no_cache": ["$arg_no_cache"]
+ }
+ },
+ "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
+--- response_body
+passed
+
+
+
+=== TEST 29: hit route with nil vars in cache_key
+--- request
+GET /hello?bar=a
+--- response_body chop
+hello world!