proxy-cache
The proxy-cache plugin, which provides the ability to cache upstream response data and can be used with other plugins. The plugin supports disk-based caching and will support the memory-based caching in the future. The data that needs to be cached can be determined by the response code or request method and more complex caching policies can be configured by no_cache and cache_bypass attributes.
Note:
- The cache expiration time cannot be configured dynamically. The expiration time can only be set by the upstream response header
ExpiresorCache-Control, and the default cache expiration time is 10s if there is noExpiresorCache-Controlin the upstream response header - If the upstream service is not available and APISIX will return 502 or 504, then 502 or 504 will be cached for 10s.
Attributes#
| Name | Type | Requirement | Default | Valid | Description |
|---|---|---|---|---|---|
| cache_zone | string | optional | disk_cache_one | Specify which cache area to use, each cache area can be configured with different paths. In addition, cache areas can be predefined in conf/config.yaml file. When the default value is not used, the specified cache area is inconsistent with the pre-defined cache area in the conf/config.yaml file, and the cache is invalid. | |
| cache_key | array[string] | optional | ["$host", "$request_uri"] | key of a cache, can use variables. For example: ["$host", "$uri", "-cache-id"] | |
| cache_bypass | array[string] | optional | Whether to skip cache retrieval. That is, do not look for data in the cache. It can use variables, and note that cache data retrieval will be skipped when the value of this attribute is not empty or not '0'. For example: ["$arg_bypass"] | ||
| cache_method | array[string] | optional | ["GET", "HEAD"] | ["GET", "POST", "PUT", "DELETE", "PATCH", "HEAD","OPTIONS", "CONNECT", "TRACE"] | Decide whether to be cached according to the request method |
| cache_http_status | array[integer] | optional | [200, 301, 404] | [200, 599] | Decide whether to be cached according to the upstream response status |
| hide_cache_headers | boolean | optional | false | Whether to return the Expires and Cache-Control response headers to the client, | |
| no_cache | array[string] | optional | Whether to cache data, it can use variables, and note that the data will not be cached when the value of this attribute is not empty or not '0'. |
Note:
- The variable starts with $.
- The attribute can use a combination of the variable and the string, but it needs to be written separately as an array, and the final values are stitched together after the variable is parsed.
Example configuration in the conf/config.yaml file:
Examples#
Enable the plugin#
Example 1: The cache_zone parameter defaults to disk_cache_one
1: enable the proxy-cache plugin for a specific route :
Test Plugin:
http status is '200' and the response header contains 'Apisix-Cache-Status' to indicate that the plugin is enabled.
2: Verify that the data is cached, request the address above again:
Example 2: Customize the cache_zone parameter to disk_cache_two
- Specify the cache area and other information in the
conf/config.yamlfile:
- Enable the
proxy-cacheplugin for a specific route:
Test Plugin:
http status is '200' and the response header contains 'Apisix-Cache-Status' to indicate that the plug-in is enabled.
- Verify that the data is cached and request the above address again:
The response header
Apisix-Cache-Statusvalue has changed to HIT, indicating that the data has been cached
Example 3: Specifying cache_zone as invalid_disk_cache is inconsistent with the cache area disk_cache_one specified in the conf/config.yaml file.
In response to an error message, the plug-in configuration is invalid.
Clear cached data#
How to clean the cached data only needs to specify the requested method as PURGE.
Test Plugin:
If the response code is 200, it means the deletion is successful. If the cached data is not found, 404 will be returned.
Request again, the cached data is not found and return 404:
Disable Plugin#
Remove the corresponding JSON in the plugin configuration to disable the plugin immediately without restarting the service:
The plugin has been disabled now.