Using WSO2 Cache Mediator with the Call Mediator

Randini Senanayake
2 min readMar 9, 2023

Usage : The Cache mediator determines if an incoming message is identical to one that was received within a predetermined window of time before allowing it to enter the message flow. By examining the hash value of incoming messages, this is accomplished.

Setup Cache Mediator Proxy on ESB

Step 01: Create a backend to send a response to ESB. If you haven’t one use a mock backend which can be created using (e.g. mocky.io etc). Set the response payload to one as below and mock backend URL in the proxy service.

{
"msg": "Hello World."
}

Step 02: Add the sample proxy provided below to the ESB.

<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="test_cache_proxy"
startOnLoad="true"
statistics="disable"
trace="disable"
transports="http,https">
<target>
<inSequence>
<log>
<property name="test_log" value="Request received"/>
</log>
<cache collector="false" timeout="20">
<onCacheHit>
<log level="custom">
<property name="test_log" value="Inside Cache Hit"/>
</log>
<respond/>
</onCacheHit>
<protocol type="HTTP">
<methods>*</methods>
<headersToExcludeInHash/>
<responseCodes>2[0-9][0-9]</responseCodes>
<enableCacheControl>false</enableCacheControl>
<includeAgeHeader>false</includeAgeHeader>
<hashGenerator>org.wso2.carbon.mediator.cache.digest.DOMHASHGenerator</hashGenerator>
</protocol>
<implementation maxSize="1000"/>
</cache>
<log>
<property name="test_log" value="After Cache Mediator"/>
</log>
<call>
<endpoint>
<address uri="http://run.mocky.io/v3/a1658fe6-49a8-44f4-827e-623aee8f50c5"/>
</endpoint>
</call>
<property name="RESPONSE" scope="default" type="STRING" value="true"/>
<log>
<property name="test_log" value="After Call mediator"/>
</log>
<cache collector="true"/>
<log>
<property name="testing" value="After Cache collector"/>
</log>
<respond/>
</inSequence>
</target>
<description/>
</proxy>

Step 03: Invoke the proxy using the below CURL request

curl -X GET http://localhost:8280/services/test_cache_proxy.test_cache_proxyHttpSoap12Endpoint

Step 04: The following lines will be logged when the service is executed for the first time. To elaborate, the cache mediator is empty at this instance hence the mock backend will be invoked and the request will be cached.

INFO - LogMediator To: /services/test_cache_proxy.test_cache_proxyHttpSoap12Endpoint, MessageID: urn:uuid:f29a0460-ce7e-44e7-8552-c940ff5e1be9, Direction: request, test_log = Request received
INFO - LogMediator To: /services/test_cache_proxy.test_cache_proxyHttpSoap12Endpoint, MessageID: urn:uuid:f29a0460-ce7e-44e7-8552-c940ff5e1be9, Direction: request, test_log = After Cache Mediator
INFO - LogMediator To: http://www.w3.org/2005/08/addressing/anonymous, WSAction: , SOAPAction: , MessageID: urn:uuid:94834df5-0e18-45f5-b3d7-f7c0fea38d99, Direction: response, test_log = After Call mediator
INFO - LogMediator To: http://www.w3.org/2005/08/addressing/anonymous, WSAction: , SOAPAction: , MessageID: urn:uuid:94834df5-0e18-45f5-b3d7-f7c0fea38d99, Direction: response, testing = After Cache collector

Step 05: When the service is re-executed the following lines will be logged. The cache mediator identifies the similar request (within the predetermined window of time. (timeout = 20(s) in the provided example)

[EI-Core]  INFO - LogMediator To: /services/test_cache_proxy.test_cache_proxyHttpSoap12Endpoint, MessageID: urn:uuid:80ccb6c9-0bc5-49b2-abf9-51019f066362, Direction: request, test_log = Request received
[EI-Core] INFO - LogMediator test_log = Inside Cache Hit

Explanation of the above process:

  • When a request comes into the ESB, the Cache mediator in inSequence is initiated.
  • If the request already exists in ESB cache, then it executes the “onCacheHit”. Else, the execution goes through the flow.
  • The “timeout” property in cache mediator used to define the timeline in which the request should be cached. This is set in seconds.
  • The <cache collector=”true”/> property is intended to be defined to collect the final response received from the backend invocation to the cache.
  • The <hashGenerator> parameter is used to define the logic used by the Cache mediator to evaluate the hash values of incoming messages. The default hash generator is
org.wso2.carbon.mediator.cache.digest.HttpRequestHashGenerator

--

--