Response caching¶
The API Manager uses WSO2 ESB's cache mediator to cache response messages for each API. Caching improves performance, because the backend server does not have to process the same data for a request multiple times. You need to set an appropriate timeout period to offset the risk of stale data in the cache.
Enabling Response Caching for an API¶
You need to enable response caching when creating a new API or editing an existing API using the API Publisher.
Please follow below steps to enable response caching for an API.
-
Navigate to the Runtime tab under API Configurations where you find the response caching configuration. Then turn on Response caching and give a timeout value. This enables the default response caching settings.
Note
When creating a new API by using a Swagger or Open API definition, response caching can be set up by defining an API-M supported Open API extension “x-wso2-response-cache”.
Example
x-wso2-response-cache: enabled: true cacheTimeoutInSeconds: 400
-
If you want to change the default response caching settings, edit the following cache mediator properties in the
<API-M_HOME>/repository/resources/api_templates/velocity_template.xml
file:Property Description collector
true
: specifies that the mediator instance is a response collection instance.false
: specifies that the mediator instance is a cache serving instance.
max MessageSize
Specifies the maximum size of a message to be cached in bytes. An optional attribute, with the default value set to unlimited
.maxSize
Defines the maximum number of elements to be cached. hashGenerator
Defines the hash generator class.
When caching response messages, a hash value is generated based on the request's URI, transport headers and the payload (if available). WSO2 has a default
HttpRequestHashGenerator
class written to generate the hash value. See sample here . Please note that prior to API-M 4.0.0, a different hash generator implementationREQUESTHASHGenerator
was used by default and from API-M 4.0.0, this new hash generator implementation is being used.If you want to change this default implementation (for example, to exclude certain headers), you can write a new hash generator implementation by extending the
HttpRequestHashGenerator
and overriding itsgetDigest()
method. Once done, add the new class as thehashGenerator
attribute of the<cache>
element in thevelocity_template.xml
file andapi_product_template.xml
file.
Note
When running a distributed deployment, you need to enable the JSON stream builders on the API Gateway and maintain the standard builders on the API Developer Portal node.
Follow the instructions below to enable the stream builders in the API gateway:
- Open the
<API-M_HOME>/repository/conf/deployment.toml
file. -
Add following configuration.
[message_builder] json = "org.apache.synapse.commons.json.JsonStreamBuilder"
Invalidating Cached Responses Remotely¶
You can invalidate all cached response remotely by using any JMX monitoring tool such as Jconsole using the exposed MBeans. You can use the InvalidateMediatorCache()
operation of the org.wso2.carbon.mediation
MBean for this as shown below.