Configure API caching information to cache API responses to reduce the pressure on the business system.
About this task
The API cache contains the response of the entire backend request, including the response header and response body. Therefore, the status class information, such as user data in the cookie
, must be excluded from the response header of the cached API. This caching feature is only suitable for caching stateless data.
The backend service system can add a Pragma: no-cache
to the response header to notify the gateway not to cache the response.
Procedure
Log on to the mPaaS console. In the left-side navigation pane, click Mobile Gateway Service.
In the API list, find the API that you want to configure and click Configure in the Actions column.
Click Modify in the Cache Configuration section and configure the following rules:
Cache result: Specifies whether to enable caching.
Cache time: The lifetime of the cache. Unit: seconds.
Cache key: The key-value expression used for caching. Click Edit to modify the cache key. In the displayed modal box, enter the primary key required for the cache. You can drag and drop them to sort. For information about key-value syntax, see Key-value syntax below.
Key-Value Syntax
Supported Syntax
When an API request arrives at the gateway, the gateway obtains the corresponding data as the cached key value according to the key-value configuration. The syntax is as follows:
Statement | Description |
$ | The root object, for example:$.bar |
[num] | Array access, where num is a number. Example:$[0].bar.foos[1].name |
. | Property access, for example:$.bar |
['key'] | Property access, for example:$['bar'] |
$.header | The API request header object, which is used to obtain the fields in the request header, for example:$.header.remote_addr |
$.cookie | API request cookie object to get the value in the cookie, for example:$.cookie.session_id |
$.http_body | The backend is an HTTP request body object, which is used to obtain the fields in the request body, for example, $.http_body.name |
$.http_qs | The backend is an HTTP request parameters object that is used to obtain request parameters, for example, $.http_qs.name |
Sample code
Take the following request data as an example and obtain the object from the request packet:
URL:/json.htm?tenantId=boo
Header:
Content-Type:application/json
opt:com.mobile.info.get
workspaceId:default
appId:B2D553102
cookie:JSESSIONID=abcd;traceId=trace1000
Body:
[
{
"key": "1234",
"locations": [
"beijing",
"shanghai"
],
"language": "zh-Hans",
"unit": "c"
},
{
"demo": {
"name": "nick"
}
}
]
An example of the expression is shown below:
$.header.appId = B2D553102
$.cookie.traceId = trace1000
$.http_qs.tenantId = boo
$[0].key = 1234
$[0].locations[1] = shanghai
$[1].demo.name = nick