Sample 420: Simple Response Caching Scenario

<definitions xmlns=""> <sequence name="main"> <in> <cache timeout="20" scope="per-host" collector="false" hashGenerator="org.wso2.caching.digest.DOMHASHGenerator"> <implementation type="memory" maxSize="100"/> </cache> <send> <endpoint> <address uri="http://localhost:9000/services/SimpleStockQuoteService"/> </endpoint> </send> </in> <out> <cache collector="true"/> <send/> </out> </sequence> </definitions>


Showcase the caching capabilities of Synapse by implementing a simple response cache in Synapse for an actual service deployed on Axis2


  • Deploy the SimpleStockQuoteService in the sample Axis2 server and start Axis2
  • Start Synapse using the configuration numbered 420 (repository/conf/sample/synapse_sample_420.xml)
    Unix/Linux: sh -sample 420
    Windows: synapse.bat -sample 420

Executing the Client

In this sample, the message sent to Synapse is checked for an existing cached response by calculating the hash value of the request. If there is a cache hit in Synapse, then this request will not be forwarded to the actual service. Rather, Synapse responds to the client with the cached response. In case of a cache miss that particular message will be forwarded to the actual service and caches that response in the out path for the use of consecutive requests of the same type.

To try out this scenario, send a request from the sample client as follows.

ant stockquote -Dtrpurl=http://localhost:8280/

You will notice that if you send more than one requests within 20 seconds, only the first request is forwarded to the actual service, and the rest of the requests will be served by the cache inside Synapse. You could observe this by looking at the logs printed by the Axis2 server, as well as by observing a constant quote value in the response to the client instead of the random rate, which changes by each and every 20 seconds.

Back to Catalog