All Products
Search
Document Center

Cache API

Last Updated: Mar 10, 2021

You can configure API cache information to cache the API response and relieve the stress on the business system.

About this task

API cache covers the response of all backend requests, including response header and response body. So, the response headers of the cached APIs must have the status information eliminated, such as user-mode data in cookie. This cache function is only applicable for caching stateless data.

The backend business system can inform the gateway of not caching response by adding Pragma: no-cache in the response header.

Procedure

  1. Log in to the mPaaS console, and from the navigation bar on the left, click Mobile Gateway Service.
  2. In the API list, select the API to be configured, and click Configure on the operation column to go to the API detail page.
  3. Click Edit in the Cache configuration section, and then configure the following rule:
    • Cache result: Whether to cache or not
    • Cache time: Cache’s time to live, in seconds
    • Cache key: Key expression used for caching. Click Modify to define the cache key. In the pop-up modal dialog box, enter the required primary key. You can drag the keys to sort them. For information about the key syntax, see Key syntax.

Key syntax

Supported syntax

When the gateway receives an API request, it obtains the corresponding data from the request according to the key configuration and uses the data as the cached key.

The syntax is as follows:

Syntax Description
$ Root object, for example: $.bar
[num] Array access, num refers to number, for example: $[0].bar.foos[1].name
. Attribute access, for example: $.bar
[‘key’] Attribute access, for example: $.bar
$.header API request header object, used to obtain the field in the request header, for example: $.header.remote\_addr
$.cookie API request cookie object, used to obtain the value in cookie, for example: $.cookie.session\_id
$.http_body HTTP request body object, used to obtain the field in the request body, for example: $.http\_body.name
$.http_qs HTTP request parameter object, used to obtain the request parameter, for example: $.http\_qs.name

Code sample

For example, to obtain objects from the following request message:

  1. URL:/json.htm?tenantId=boo
  2. Header:
  3. Content-Type:application/json
  4. opt:com.mobile.info.get
  5. workspaceId:default
  6. appId:B2D553102
  7. cookie:JSESSIONID=abcd;traceId=trace1000
  8. Body:
  9. [
  10. {
  11. "key": "1234",
  12. "locations": [
  13. "beijing",
  14. "shagnhai"
  15. ],
  16. "language": "zh-Hans",
  17. "unit": "c"
  18. },
  19. {
  20. "demo": {
  21. "name": "nick"
  22. }
  23. }
  24. ]

Here are examples of the expression:

  1. $.header.appId = B2D553102
  2. $.cookie.traceId = trace1000
  3. $.http_qs.tenantId = boo
  4. $[0].key = 1234
  5. $[0].locations[1] = shagnhai
  6. $[1].demo.name = nick