proxy-cache

Description

The proxy-cache Plugin can be used to cache the response from the Upstream. It can be used with other Plugins and currently supports disk-based caching.

The data to be cached can be filtered with response codes, request modes, or more complex methods using the no_cache and cache_bypass attributes.

Attributes

NameTypeRequiredDefaultValid valuesDescription
cache_strategystringFalsedisk[“disk”,”memory”]Specifies where the cached data should be stored.
cache_zonestringFalsedisk_cache_oneSpecifies which cache area to use. Each cache area can be configured with different paths. Cache areas can be predefined in your configuration file (conf/config.yaml). If the specified cache area is inconsistent with the pre-defined cache area in your configuration file, the cache is invalid.
cache_keyarray[string]False[“$host”, “$request_uri”]Key to use for caching. For example, [“$host”, “$uri”, “-cache-id”].
cache_bypassarray[string]FalseConditions in which response from cache is bypassed. Whether to skip cache retrieval. If at least one value of the string parameters is not empty and is not equal to “0” then the response will not be taken from the cache. For example, [“$arg_bypass”].
cache_methodarray[string]False[“GET”, “HEAD”][“GET”, “POST”, “HEAD”]Request methods for which the response will be cached.
cache_http_statusarray[integer]False[200, 301, 404][200, 599]HTTP status codes of the Upstream response for which the response will be cached.
hide_cache_headersbooleanFalsefalseWhen set to true adds the Expires and Cache-Control headers to the client response.
cache_controlbooleanFalsefalseWhen set to true, complies with Cache-Control behavior in the HTTP specification. Used only for memory strategy.
no_cachearray[string]FalseConditions in which the response will not be cached. If at least one value of the string parameters is not empty and is not equal to “0” then the response will not be saved.
cache_ttlintegerFalse300 secondsTime that a response is cached until it is deleted or refreshed. Comes in to effect when the cache_control attribute is not enabled or the proxied server does not return cache header. Used only for memory strategy.
proxy-cache - 图1note

The cache expiration time cannot be configured dynamically. It can only be set by the Upstream response header Expires or Cache-Control. The default expiration time is 10s if there is no Expires or Cache-Control in the Upstream response header.

If the Upstream service is not available and APISIX returns a 502 or 504 status code, it will be cached for 10s.

Enabling the Plugin

You can add your cache configuration in you APISIX configuration file (conf/config.yaml) as shown below:

conf/config.yaml

  1. proxy_cache:
  2. cache_ttl: 10s # default caching time if the upstream doesn't specify the caching time
  3. zones:
  4. - name: disk_cache_one # name of the cache. Admin can specify which cache to use in the Admin API by name
  5. memory_size: 50m # size of shared memory, used to store the cache index
  6. disk_size: 1G # size of disk, used to store the cache data
  7. disk_path: "/tmp/disk_cache_one" # path to store the cache data
  8. cache_levels: "1:2" # hierarchy levels of the cache

You can enable the Plugin on a specific Route as shown below:

  1. curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "plugins": {
  4. "proxy-cache": {
  5. "cache_key": ["$uri", "-cache-id"],
  6. "cache_bypass": ["$arg_bypass"],
  7. "cache_method": ["GET"],
  8. "cache_http_status": [200],
  9. "hide_cache_headers": true,
  10. "no_cache": ["$arg_test"]
  11. }
  12. },
  13. "upstream": {
  14. "nodes": {
  15. "127.0.0.1:1999": 1
  16. },
  17. "type": "roundrobin"
  18. },
  19. "uri": "/hello"
  20. }'

In the above configuration, the cache_zone attribute defaults to disk_cache_one.

Example usage

Once you have configured the Plugin as shown above, you can make an initial request:

  1. curl http://127.0.0.1:9080/hello -i
  1. HTTP/1.1 200 OK
  2. Content-Type: application/octet-stream
  3. Content-Length: 6
  4. Connection: keep-alive
  5. Server: APISIX web server
  6. Date: Tue, 03 Mar 2020 10:45:36 GMT
  7. Last-Modified: Tue, 03 Mar 2020 10:36:38 GMT
  8. Apisix-Cache-Status: MISS
  9. hello

The Apisix-Cache-Status in the response shows MISS meaning that the response is not cached, as expected. Now, if you make another request, you will see that you get a cached response:

  1. curl http://127.0.0.1:9080/hello -i
  1. HTTP/1.1 200 OK
  2. Content-Type: application/octet-stream
  3. Content-Length: 6
  4. Connection: keep-alive
  5. Server: APISIX web server
  6. Date: Tue, 03 Mar 2020 11:14:46 GMT
  7. Last-Modified: Thu, 20 Feb 2020 14:21:41 GMT
  8. Apisix-Cache-Status: HIT
  9. hello

If you set "cache_zone": "invalid_disk_cache" attribute to an invalid value (cache not configured in the your configuration file), then it will return a 404 response.

proxy-cache - 图2tip

To clear the cached data, you can send a request with PURGE method:

  1. curl -i http://127.0.0.1:9080/hello -X PURGE
  1. HTTP/1.1 200 OK
  2. Date: Tue, 03 Mar 2020 11:17:35 GMT
  3. Content-Type: text/plain
  4. Transfer-Encoding: chunked
  5. Connection: keep-alive
  6. Server: APISIX web server

If the response code is 200, the deletion is successful. If the cached data is not found, a 404 response code will be returned.

Disable Plugin

To disable the proxy-cache Plugin, you can delete the corresponding JSON configuration from the Plugin configuration. APISIX will automatically reload and you do not have to restart for this to take effect.

  1. curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "uri": "/hello",
  4. "plugins": {},
  5. "upstream": {
  6. "type": "roundrobin",
  7. "nodes": {
  8. "127.0.0.1:1999": 1
  9. }
  10. }
  11. }'