X-Cache-Status始终使用Kong proxy-cache插件绕过

时间:2019-02-26 11:25:08

标签: caching redis kong kong-plugin

我在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"

当我从该服务调用API时:

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

一切正常,但 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
...

如何调试绕过原因?

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

Kong社区版随附的插件proxy-cache仅允许内存中缓存。如果要使用Redis进行缓存,则必须使用Kong Enterprise版本。更多信息here

作为替代方案,Github上有一个名为kong-plugin-proxy-cache的开源插件。您必须先从Luarocks安装插件,然后在Kong config中启用插件

    # 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>"