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

request help: proxy-cache plugin, why the cache expiration time cannot be configured dynamically? #5453

Open
jagerzhang opened this issue Nov 9, 2021 · 26 comments
Assignees
Labels
discuss enhancement New feature or request

Comments

@jagerzhang
Copy link
Contributor

jagerzhang commented Nov 9, 2021

Issue description

Nginx 的 proxy-cache 可以动态设置TTL,比如:

proxy_cache_valid 200 1m; # 响应码为200的缓存1分钟
proxy_cache_valid 304 1h; # 响应码为304的缓存1小时
...

但是APISIX 文档提到:The cache expiration time cannot be configured dynamically. The expiration time can only be set by the upstream response header Expires or Cache-Control, and the default cache expiration time is 10s if there is no Expires or Cache-Control in the upstream response header

意思是完全看后端的过期设置来定,否则就按config.yml 默认的cache_ttl,感觉不是那么灵活,是否可以开放下,让我们可以在路由里面自行设置缓存TTL,并可以强制缓存(Nginx的 proxy-cache 还支持proxy_ignore_headers 忽略后端缓存申明...)

补充:
我尝试增加一个 response-rewrite插件来改写 expires 和 Cache-Control:

"response-rewrite": {
      "disable": false,
      "headers": {
        "Cache-Control": "max-age=86400",
        "expires": 86400
      }
    }

然后测试发现虽然响应头部已经被改写了,但是缓存TTL并没有生效,还是10S就过期了,应该是这个response-rewrite的工作机制位于缓存之后,所以并不能影响到缓存的TTL设置。

Environment

  • apisix version (cmd: apisix version): 2.10.0
@tzssangglass
Copy link
Member

can you show an actual scenario? AFAIK, the cache time is determined by the upstream service and APISIX determines the caching behaviour based on the cache time set by the upstream service, which is equivalent to a protocol. If APISIX breaks this protocol, it can become chaotic.

@shuaijinchao
Copy link
Member

The setting of the timeout period belongs to the behavior of the application layer. As an intermediate proxy layer, the gateway should be decoupled from the application layer, so I think there is no problem in letting the application layer set the timeout period.

@jagerzhang
Copy link
Contributor Author

@tzssangglass @shuaijinchao
嗯,从遵循协议上来说你们说的都是有道理的。不过你们应该知道还有一个CDN的场景,是需要支持在网关设置强制缓存的,也就是可以忽略后端应用的缓存申明。而且这一点在Nginx原生的proxy_cache也是支持的,甚至Nginx的还可以支持设置 proxy_cache_ignore_header 来强制忽略后端nocache等不缓存申明。

@spacewander
Copy link
Member

When I worked in a CDN vendor, I made the proxy_cache_valid time/proxy_cache_ignore_header configurable via Lua. It requires modifying the Nginx itself.

@spacewander spacewander added the enhancement New feature or request label Nov 19, 2021
@agile6v
Copy link
Member

agile6v commented Nov 21, 2021

Hi @jagerzhang Currently You can modify the basic configuration to suit your needs. But it works globally.

@jagerzhang
Copy link
Contributor Author

Hi @jagerzhang Currently You can modify the basic configuration to suit your needs. But it works globally.

嗯嗯,这个全局设置我知道了~ 不过我还是挺希望APISIX能支持下动态设置过期时间,把这个特性作为插件的一个可配置参数来设置。

这个缓存时长设置在Nginx里面是可以动态设置的,希望可以参考下:http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache_valid

@jagerzhang
Copy link
Contributor Author

jagerzhang commented Nov 24, 2021

@spacewander @shuaijinchao @tzssangglass 从APISIX的ngx模板来看,这里也是用的Nginx原生的 proxy_cache_valid 指令来缓存的:

proxy_cache_valid any {% if proxy_cache.cache_ttl then %} {* proxy_cache.cache_ttl *} {% else %} 10s {% end %};

在使用Nginx的时候 proxy_cache_valid 是可以支持给指定的响应码定义不同的缓存时间的,也就是说可以配置多条proxy_cache_valid指令,比如:

proxy_cache_valid 200 30m; # 200响应的请求缓存30分钟
proxy_cache_valid 304 1d; # 304响应的请求缓存1天
....

是非常灵活的,不过ngx.tpl应该是启动的时候就静态生成的,可能这几个想法不太现实...

@tzssangglass
Copy link
Member

应该是启动的时候就静态生成的

Yes, if you want to do it, @spacewander way would be better, dynamic.

@Carl5566
Copy link

Carl5566 commented May 5, 2022

這個issue有新的進展嗎?
我也遇到相同的問題,有方法可以解嗎?

@tzssangglass
Copy link
Member

這個issue有新的進展嗎?
我也遇到相同的問題,有方法可以解嗎?

now we can use proxy-cache plugin: https://github.com/apache/apisix/blob/master/docs/zh/latest/plugins/proxy-cache.md

@Carl5566
Copy link

Carl5566 commented May 5, 2022

Yes, 我目前也是用proxy-cache plugin, 但是我的需求跟 @jagerzhang 一樣, 希望每條不同的Rooute能有不同的cache ttl, 現在只能所有route都是同一個cache ttl時間

@tzssangglass
Copy link
Member

希望每條不同的Rooute能有不同的cache ttl, 現在只能所有route都是同一個cache ttl時間

IMO, the plugin configuration is independent on different routes.

@Carl5566
Copy link

Carl5566 commented May 5, 2022

{ "uri": "/*", "name": "www1.trustlog.com", "methods": [ "GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS", "CONNECT", "TRACE" ], "host": "www1.trustlog.com", "plugins": { "proxy-cache": { "cache_ttl": 300, "disable": false },
我使用了 "cache_ttl": 300, cache的時間依舊是10s

@tzssangglass
Copy link
Member

我使用了 "cache_ttl": 300, cache的時間依舊是10s

基于磁盘的缓存需要注意:

不能动态配置缓存的过期时间,只能通过后端服务响应头 Expires 或 Cache-Control 来设置过期时间,如果后端响应头中没有 Expires 或 Cache-Control,那么 APISIX 将默认只缓存 10 秒钟

Maybe that's why.

If you think this is a bug, please submit an issue and give the full steps to reproduce it.

@spacewander
Copy link
Member

So far, cache_ttl is not supported by the disk backend. See https://apisix.apache.org/docs/apisix/plugins/proxy-cache#attributes

@Carl5566
Copy link

Carl5566 commented May 6, 2022

請教有什麼方法可以支持不同route 有不同時間的磁盤緩存

@tzssangglass
Copy link
Member

請教有什麼方法可以支持不同route 有不同時間的磁盤緩存

As explained above, it is not currently supported.

@uran0sH
Copy link
Contributor

uran0sH commented Jan 12, 2023

I will work on it. Thanks

@uran0sH
Copy link
Contributor

uran0sH commented Jan 15, 2023

Should I make the proxy-cache plugin support cache_ttl?

@spacewander
Copy link
Member

Yes

@uran0sH
Copy link
Contributor

uran0sH commented Jan 17, 2023

Yes

When I read the code of disk_handler.lua, I didn't understand how it caches things. It doesn't have an operation like memory:set. Does it use nginx's proxy-cache?

@spacewander
Copy link
Member

It uses nginx's proxy-cache

@uran0sH
Copy link
Contributor

uran0sH commented Jan 18, 2023

So do I need to modify the config of Nginx and reload it in header_filter? Are there some operations to modify the config of Nginx? I am so confused about it. Could you give me some ideas?

@uran0sH
Copy link
Contributor

uran0sH commented Jan 19, 2023

And I have another question: I modify the value of cache_ttl of proxy_cache to 1s in 'conf/default_config.yaml'. It doesn't work.

@spacewander
Copy link
Member

Back from vacation.

So do I need to modify the config of Nginx and reload it in header_filter? Are there some operations to modify the config of Nginx? I am so confused about it. Could you give me some ideas?

No. We need to modify Nginx and control it via Lua so the config can take effect at the plugin level.

@uran0sH
Copy link
Contributor

uran0sH commented Feb 6, 2023

Back from vacation.

So do I need to modify the config of Nginx and reload it in header_filter? Are there some operations to modify the config of Nginx? I am so confused about it. Could you give me some ideas?

No. We need to modify Nginx and control it via Lua so the config can take effect at the plugin level.

Does it mean that we need to modify the source code of Nginx?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discuss enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

8 participants