简体   繁体   English

X-Cache-Status 总是使用 Kong 代理缓存插件绕过

[英]X-Cache-Status is always Bypass using Kong proxy-cache plugin

I have service in Kong and I have set proxy-cache plugin for that service.我在 Kong 有服务,并且已经为该服务设置了代理缓存插件。

curl -X POST http://localhost:8001/plugins --data "name=proxy-cache" --data "config.strategy=redis" --data 'service_id=2f0a285d-7b25-48d6-adc3-bbf28ffe5f47' --data "config.redis.host=127.0.0.1" --data "config.redis.port=6379" --data "config.redis.password=my_redis_password"

When I call an API from that service:当我从该服务调用 API 时:

curl -i -X GET --url http://localhost:3002/v1/currency --header 'apikey: MY_API_KEY'

everything works correctly but X-Cache-Status is always Bypass一切正常,但X-Cache-Status总是绕过

HTTP/1.1 200 OK                                                                                                                                       
Content-Type: application/json; charset=utf-8                                                                                                         
Content-Length: 3654                                                                                                                                  
Connection: keep-alive                                                                                                                                
X-RateLimit-Limit-second: 100                                                                                                                         
X-RateLimit-Remaining-second: 99                                                                                                                      
X-Cache-Key: 3e18cdfc6e02359fb0f874efdf5788d8                                                                                                         
X-Cache-Status: Bypass                                                                                                                                
X-Powered-By: Express
...

How can I debug Bypass reason?如何调试绕过原因?

为避免在 X-Cache-Status 中绕过,您必须在创建代理缓存插件时添加此配置

--data "config.content_type=application/json; charset=utf-8" 

The plugin proxy-cache that comes bundled with Kong community edition only allows in-memory caching. Kong 社区版附带的插件proxy-cache仅允许内存缓存。 If you want to use Redis for caching, you will have to use Kong Enterprise version.如果要使用Redis进行缓存,则必须使用Kong Enterprise版本。 More information here更多信息在这里

As an alternative, there is a open source plugin called kong-plugin-proxy-cache available on Github .作为替代方案, Github上有一个名为kong-plugin-proxy-cache开源插件。 You will have to first install the plugin from Luarocks and then enable the plugin in Kong config您必须首先从 Luarocks 安装插件,然后在 Kong 配置中启用该插件

    # Install plugin dependency
    sudo luarocks install lua-resty-redis-connector

    # install plugin
    sudo luarocks install kong-plugin-proxy-cache

    # Enable plugin in kong.conf
    plugins = bundled,proxy-cache

    # After enabling, you can use plugin with any service, route or consumer.
    # To enable it for a service
    curl -X POST http://localhost:8001/services/<service-name>/plugins \
    --data "name=proxy-cache"  \
    --data "config.cache_ttl=300" \
    --data "config.cache_control=false" \
    --data "config.redis.host=<redis-host>" \
    --data "config.redis.port=<redis-port>"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM