Response caching¶
The ESB uses the 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.xmlfile:Property Description collectortrue: specifies that the mediator instance is a response collection instance.false: specifies that the mediator instance is a cache serving instance.
max MessageSizeSpecifies the maximum size of a message to be cached in bytes. An optional attribute, with the default value set to unlimited.maxSizeDefines the maximum number of elements to be cached. hashGeneratorDefines 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). MWARE ESB has a default
HttpRequestHashGeneratorclass written to generate the hash value. See sample here . Please note that prior to API-M 4.0.0, a different hash generator implementationREQUESTHASHGeneratorwas 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
HttpRequestHashGeneratorand overriding itsgetDigest()method. Once done, add the new class as thehashGeneratorattribute of the<cache>element in thevelocity_template.xmlfile andapi_product_template.xmlfile.
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.tomlfile. -
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.
