Cache Mediator
When a message enters a message flow, the Cache mediator checks whether the incoming message is similar to a previous message that was received within a specified period of time. This is done by evaluating the hash value of incoming messages. If a similar message was identified before, the Cache mediator executes the onCacheHit
sequence (if specified), fetches the cached response, and prepares the ESB profile to send the response. The onCacheHit
sequence can send back the response message using the Respond Mediator . If the onCacheHit
sequence is not specified, the cached response is sent back to the requester and the message is not passed on. If a similar message has not been seen before, then the message is passed on.
The Cache mediator is a content-aware mediator.
The Cache mediator supports only local caching. It does not support distributed caching.
Syntax
<cache [timeout="seconds"] [collector=(true | false)] [maxMessageSize="in-bytes"] > <onCacheHit [sequence="key"]> (mediator)+ </onCacheHit>? <protocol type="http" >? <methods>comma separated list</methods> <headersToExcludeInHash>comma separated list</headersToExcludeInHash> <responseCodes>regular expression</responseCodes> <enableCacheControl>(true | false)</enableCacheControl> <includeAgeHeader>(true | false)</includeAgeHeader> <hashGenerator>class</hashGenerator> </protocol> <implementation [maxSize="int"]/> </cache>
Note
In a message flow you can use the cache mediator as a finder (in the incoming path to check the request) or as a collector (in the outgoing path to cache the response).
It is not possible to have more than one cache mediator in the same message flow because mediation is terminated after the finder on a cache hit, and the response is not passed on to the next finder after a cache hit.
Following is an example where the expected response from the last cache hit is not received because the response is sent once the request comes to the first finder:
<?xml version="1.0" encoding="UTF-8"?> <proxy xmlns="http://ws.apache.org/ns/synapse" name="cache115" transports="http https" startOnLoad="true"> <description /> <target> <inSequence> <cache collector="false" timeout="60"> <protocol type="HTTP"> <methods>POST</methods> <headersToExcludeInHash /> <responseCodes>.*</responseCodes> <enableCacheControl>false</enableCacheControl> <includeAgeHeader>false</includeAgeHeader> <hashGenerator>org.wso2.carbon.mediator.cache.digest.HttpRequestHashGenerator</hashGenerator> </protocol> </cache> <call> <endpoint> <address uri="http://demo0585968.mockable.io/some" /> </endpoint> </call> <property name="RESPONSE" value="true" scope="default" type="STRING" /> <log level="full" /> <cache collector="true" /> <property name="RESPONSE" value="false" scope="default" type="STRING" /> <cache collector="false" timeout="60"> <protocol type="HTTP"> <methods>POST</methods> <headersToExcludeInHash /> <responseCodes>.*</responseCodes> <hashGenerator>org.wso2.carbon.mediator.cache.digest.HttpRequestHashGenerator</hashGenerator> </protocol> </cache> <call> <endpoint> <address uri="http://demo0585968.mockable.io/hello" /> </endpoint> </call> <property name="RESPONSE" value="true" scope="default" type="STRING" /> <log level="full" /> <cache collector="true" /> <respond /> </inSequence> </target> </proxy>
Configuration
Click on the relevant tab to view the UI configuration depending on whether the cache type of the cache mediator is Finder or Collector.
Examples
Following are examples of how you can use the Cache mediator.
Example one
According to this example configuration, when the first message is sent to the endpoint, the cache is not hit. The Cache mediator configured in the Out
sequence caches the response to this message. When a similar message is sent to the endpoint for the second time, the previous response is directly fetched from the cache and sent to the requester. This happens because the onCacheHit
sequence is not defined in this configuration.
<?xml version="1.0" encoding="UTF-8"?> <sequence name="main"> <in> <cache collector="false" maxMessageSize="10000" timeout="20"> <protocol type="HTTP"> <methods>POST</methods> <headersToExcludeInHash/> <responseCodes>2[0-9][0-9]</responseCodes> <enableCacheControl>false</enableCacheControl> <includeAgeHeader>false</includeAgeHeader> <hashGenerator>org.wso2.carbon.mediator.cache.digest.HttpRequestHashGenerator</hashGenerator> </protocol> <implementation maxSize="100"/> </cache> <send> <endpoint name="inlined"> <address uri="http://localhost:9000/services/SimpleStockQuoteService"/> </endpoint> </send> </in> <out> <cache collector="true"/> <send/> </out> </sequence>
Example two
According to this example configuration, if you define a cache collector using the cache mediator in the in sequence, you need to add the RESPONSE
property to consider the message as a response message.
<?xml version="1.0" encoding="UTF-8"?> <api xmlns="http://ws.apache.org/ns/synapse" name="cacheAPI" context="/cache"> <resource methods="POST GET" uri-template="/headerapi/*"> <inSequence> <cache collector="false" timeout="5000"> <protocol type="HTTP"> <methods>GET, POST</methods> <headersToExcludeInHash>*</headersToExcludeInHash> <responseCodes>.*</responseCodes> <enableCacheControl>false</enableCacheControl> <includeAgeHeader>false</includeAgeHeader> <hashGenerator>org.wso2.carbon.mediator.cache.digest.HttpRequestHashGenerator</hashGenerator> </protocol> </cache> <call> <endpoint> <address uri="http://localhost:9000/services/SimpleStockQuoteService"/> </endpoint> </call> <property name="RESPONSE" value="true" scope="default" type="STRING"/> <enrich> <source type="inline" clone="true"> <ax21:newvalue xmlns:ax21="http://services.samples/xsd">testsamplevalue </ax21:newvalue> </source> <target xmlns:ax21="http://services.samples/xsd" xmlns:ns="http://services.samples" action="sibling" xpath="//ns:getQuoteResponse/ns:return/ax21:volume"/> </enrich> <cache collector="true"/> <respond/> </inSequence> </resource> </api>
Samples
/wiki/spaces/EI6xx/pages/49611291
Invalidating cached responses remotely
You can invalidate all cached response remotely by using any JMX monitoring tool such as Jconsole via the exposed MBeans. You can use the invalidateTheWholeCache()
operation of the org.wso2.carbon.mediatio
n
MBean for this as shown below.