Proxy Cache
You can configure a response cache policy to cache responses returned by the backend service (server) in APIG. When a client sends the same request, APIG does not need to send the request to the backend service but directly returns the cached response. This effectively reduces the backend load and API invoking latency.
- When the response cache policy is used, backend responses are cached to APIG. However, APIG now does not support cache data encryption, which poses security risks to sensitive data in responses. Therefore, exercise caution when configuring the policy.
- Policy parameters will be stored as plaintext. To prevent information leakage, do not contain sensitive information in these parameters.
The following figure shows the principle of the response cache policy.
Usage Guidelines
- The response cache policy supports only APIs that use the GET and HEAD methods.
- Response bodies larger than 1 MB will not be cached.
- The cache size for backend response is 128 MB.
- APIG processes the cache according to the Cache-Control header in the backend response. If the backend does not return the Cache-Control header, the response is cached by default, and the ttl field configured in the policy is used as the cache expiration time.
- By default, APIG does not process the Cache-Control header of the client. You can change this by configuring the client_cache_control in the policy.
- Cache-Control extended cache instructions are not part of the core HTTP cache standard document and are not supported.
Cache-control: immutable Cache-control: stale-while-revalidate=<seconds> Cache-control: stale-if-error=<seconds>
- APIG can cache only the Content-Type, Content-Encoding, and Content-Language headers. If you want to cache more headers, add them to the Cached Backend Headers in the policy. However, the system response headers (such as x-apig-* and x-request-id) added by APIG cannot be added.
- An API can be bound with only one policy of the same type.
- Policies are independent of APIs. A policy takes effect for an API only after they are bound to each other. When binding a policy to an API, you must specify an environment where the API has been published. The policy takes effect for the API only in the specified environment.
- After you bind a policy to an API, unbind the policy from the API, or update the policy, you do not need to publish the API again.
- Taking an API offline does not affect the policies bound to it. The policies are still bound to the API if the API is published again.
- Policies that have been bound to APIs cannot be deleted.
Creating a Proxy Cache Policy
- Go to the APIG console.
- Select a dedicated gateway at the top of the navigation pane.
- In the navigation pane, choose API Management > API Policies.
- On the Policies tab, click Create Policy.
- On the Select Policy Type page, select Proxy Cache in the Plug-ins area.
- Set the policy information.
Table 1 Proxy cache parameters Parameter
Description
Name
Enter a policy name. Using naming rules facilitates future search.
Type
The value is fixed to Proxy Cache.
Description
Description about the plug-in.
Policy Content
Content of the plug-in, which can be configured in a form or using a script.
Proxy Cache Key
The configuration is used to distinguish different caches.
- system_params: default gateway parameters. For details about the parameters, see default gateway parameters
- parameters: request query parameters.
- headers: request headers
HTTP Config
The HTTP Status Code and Cache duration determine whether to cache data and how long the cache will be retained.
If HTTP parameters are not configured, the default Status Code is 200. Range: 200–599. The default cache duration is 300. Range: 1–720,000.
Cache Proxy Modes
The gateway processes the cache through the Cache-Control request header in the client request. By default, the gateway rejects all client requests with the Cache-Control header.
- all: All client requests with the Cache-Control header are allowed.
- off: All client requests with the Cache-Control header are rejected.
- apps: Clients whose app IDs (credential IDs) are in the datas list are allowed.
Cached Backend Headers
By default, only the Content-Type, Content-Encoding, and Content-Language headers can be cached. If you want to cache more headers, add them to the Cached Backend Headers in the policy. However, the system response headers (such as x-apig-* and x-request-id) added by APIG cannot be added.
- Click OK.
- To clone this policy, click Clone in the Operation column.
The name of a cloned policy cannot be the same as that of any existing policy.
- After the policy is created, perform the operations described in Binding the Policy to APIs for the policy to take effect for the API.
- To clone this policy, click Clone in the Operation column.
Example Script
{ "cache_key": { "system_params": [ "$context.sourceIp", "$context.requestId" ], "parameters": [ "demo_parameters" ], "headers": [ "demo_header" ] }, "cache_http_status_and_ttl": [ { "http_status": [ 200 ], "ttl": 300 } ], "client_cache_control": { "mode": "apps", "datas": [ "demo_app_id_1,demo_app_id_2" ] }, "cacheable_headers": [ "demo_cacheable_headers_1,demo_cacheable_headers_2" ] }
Binding the Policy to APIs
- Click a policy name to go to the policy details page.
- Select an environment and click Select APIs.
- Select the API group, environment, and required APIs.
APIs can be filtered by API name or tag. The tag is defined during API creation.
- Click OK.
- If an API no longer needs this policy, click Unbind in the row that contains the API.
- If there are multiple APIs that no longer need this policy, select these APIs, and click Unbind above the API list. You can unbind a policy from a maximum of 1000 APIs at a time.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot