Setting Up WSO2 Cache Mediator

dhanushka madushan
3 min readMar 6, 2019

--

If you have an ESB service that sends a request to backend service and gets back the response, then you might be able to use Cache Mediator to cache the response from the backend and send it when the same request coming into the ESB.

Here, cache mediator uses hashing to find whether the request already in the cache. When request coming into the ESB, it generates the hash value for the request and lookup if request available. If the hash value exists, then ESB sends the relevant response back to the client. Otherwise, the message will be sent to the backend and just before sent the result back to the client, keep it on the cache.

Setup Cache Mediator Proxy on ESB

First, you need to create a backend web service backend to send a response to ESB. For this example, I am using mocky.io as the backend. You need to create backend service with the following payload and replace the mocky backend URL in the proxy service.

{
"key": "value"
}

Here below an example proxy service that uses Cache mediator and you can add this proxy service into the ESB.

<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="simple_cache_proxy"
startOnLoad="true"
statistics="disable"
trace="disable"
transports="http,https">
<target>
<inSequence>
<log>
<property name="testing" value="Request arrives in to InSequence"/>
</log>
<cache collector="false" timeout="20">
<onCacheHit>
<log level="custom">
<property name="testing" value="Cache Hit"/>
</log>
<respond/>
</onCacheHit>
<protocol type="HTTP">
<methods>*</methods>
<headersToExcludeInHash/>
<responseCodes>.*</responseCodes>
<enableCacheControl>false</enableCacheControl>
<includeAgeHeader>false</includeAgeHeader>
<hashGenerator>org.wso2.carbon.mediator.cache.digest.DOMHASHGenerator</hashGenerator>
</protocol>
<implementation maxSize="1000"/>
</cache>
<log>
<property name="testing" value="Hi, I am after Cache"/>
</log>
<send>
<endpoint>
<address uri="http://www.mocky.io/v2/5c7e853f31000068003763bc"/>
</endpoint>
</send>
</inSequence>
<outSequence>
<log>
<property name="testing" value="Hi, I in the top of Out Sequence"/>
</log>
<cache collector="true"/>
<log>
<property name="testing" value="I am in Out Sequence and below the Cache"/>
</log>
<send/>
</outSequence>
</target>
<description/>
</proxy>

you can evoke the service with the following command.

curl -X GET http://localhost:8280/services/simple_cache_proxy.simple_cache_proxyHttpSoap12Endpoint

The first time when you evoke the service, it will print the following log lines in carbon logs and get the same response as we set it on the mocky server.

testing = Request arrives in to InSequence
testing = Hi, I am after Cache
testing = Hi, I in the top of Out Sequence
testing = I am in Out Sequence and below the Cache

The second time you evoke the service it will print the following logs.

testing = Request arrives in to InSequence
testing = Cache Hit

How it works

When a request comes into the ESB, first it goes through the Cache mediator in Insquence. If request already exists in ESB cache, then it executes the “onCacheHit”. Otherwise, it executes the send mediator with mocky endpoint and gets back result into the outSequence. Here, “timeout” property in cache mediator used to define how long request should be cached in seconds. “hashGenerator” property defines the hash generation class name. In out sequence, we should add <cache collector=”true”/> tag in order to collect the final response into the cache.

You can find out more example of using WSO2 ESB service in https://github.com/madushadhanushka/wso2-artifacts-samples repository.

--

--