WSO2 ESB - Cache Mediator

Cache Mediator

When a message comes Cache mediator checks weather an equivalent message is seen before. If the message is seen before it will execute a specified sequence. It uses message hashes for checking the equivalence of messages. The moment cache meditor finds that the message is a cached message, it will fetch the cached response and prepare ESB for sending the response. In the case of a sequence is specified for cache hit, user can send back the response message within this sequence using a send mediator. If a sequence is not specified cached response is sent back to the requestor.

Syntax

 <cache [id="string"] [hashGenerator="class"] [timeout="seconds"] [scope=(per-host | per-mediator)]
     collector=(true | false) [maxMessageSize="in-bytes"]>
   <onCacheHit [sequence="key"]>
     (mediator)+
   </onCacheHit>?
   <implementation type=(memory | disk) maxSize="int"/>
 </cache>

The cache mediator will evaluate the hash value of an incoming message as described in the optional hash generator implementation (which should be a class implementing the org.wso2.caching.digest.DigestGenerator interface). The default hash generator is 'org.wso2.caching.digest.DOMHashGenerator'. If the generated hash value has been found in the cache then the cache mediator will execute the onCacheHit sequence which can be specified inline or referenced. The cache mediator must be specified with an 'id' and two instances with this same 'id' that correlates the response message into the cache for the request message hash. The optional 'timeout' specifies the valid duration for cached elements, and the scope defines if mediator instances share a common cache per every host instance, or per every cache mediator pair (i.e. 'id') instance. The 'collector' attribute 'true' specifies that the mediator instance is a response collection instance, and 'false' specifies that its a cache serving instance. The maximum size of a message to be cached could be specified with the optional 'maxMessageSize' attributes in bytes and defaults to unlimited. Finally the 'implementation' element may define if the cache is disk or memory based, and the 'maxSize' attribute defines the maximum number of elements to be cached.

UI Configuration

Example

<definitions xmlns="http://ws.apache.org/ns/synapse">
  <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>
</definitions>

In this example, first message will be sent to the endpoint specified as cache is not hit. The responce will come to the cache mediator inside the out meduator and this will cache the response. The second equal message will match the cache and response will be directly fetch from the cache and sent to the requestor. This happens because no onCacheHit sequence is defined.