API cache
Configure API caching to cache API responses. This reduces the load on your backend services.
About this task
The API cache stores the entire response from the backend service, including the response header and the response body. Therefore, the response headers of a cached API must not contain stateful information, such as user mode data in a cookie. This feature is designed only for caching stateless data.
Your backend service can prevent a response from being cached. Add the Pragma: no-cache directive to the response header to instruct the gateway not to cache that specific response.
Procedure
Log on to the mPaaS console. In the navigation pane on the left, click Mobile Gateway.
In the API list, find the API that you want to configure. In the Operation column, click Configure to open the API details page.
In the Cache Configuration section, click Modify and configure the following rules:
Result Cache: Specifies whether to enable caching.
Cache Time: The time to live (TTL) for the cache, in seconds.
Cache Key: The expression for the cache key. Click Modify to define the cache key. In the dialog box that appears, enter the primary keys for the cache. Drag the keys to sort them. For more information about the key syntax, see the Key syntax section below.
Key-value syntax
Supported syntax
When an API request reaches the gateway, the gateway uses the key configuration to retrieve data for the cache key. The syntax is as follows:
Syntax | Description |
$ | The root object. For example: $.bar |
[num] | Accesses an array, where num is a number. For example: $[0].bar.foos[1].name |
. | Accesses a property. For example: $.bar |
['key'] | Accesses a property. For example: $['bar'] |
$.header | The API request header object. Use this to get fields from the request header. For example: $.header.remote_addr |
$.cookie | The API request cookie object. Use this to get values from the cookie. For example: $.cookie.session_id |
$.http_body | The request body object for HTTP backends. Use this to get fields from the request body. For example: $.http_body.name |
$.http_qs | The request parameter object for HTTP backends. Use this to get request parameters. For example: $.http_qs.name |
Code example
The following example shows how to retrieve objects from a request message:
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"
}
}
]The following are example expressions:
$.header.appId = B2D553102
$.cookie.traceId = trace1000
$.http_qs.tenantId = boo
$[0].key = 1234
$[0].locations[1] = shanghai
$[1].demo.name = nick