This site contains the documentation that is relevant to older WSO2 product versions and offerings.
For the latest WSO2 documentation, visit https://wso2.com/documentation/.

Cache Mediator

When a message comes to the Cache Mediator, it 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. Once the Cache Mediator finds that the message is a cached message, it will fetch the cached response and prepare ESB for sending the response. If a sequence is specified for a 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 in-line or referenced.

Cache Mediator Attributes:

  • id - Specify an ID for the Cache Mediator to have two instances with the same id that correlates the response message into the cache for the request message hash, when you have multiple cache mediators within your Synapse configuration.
  • timeout - Optional attribute, which 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.
  • collector
    • true - The collector attribute true specifies that the mediator instance is a response collection instance.
    • false - The collector attribute false specifies that its a cache serving instance.
  • maxMessageSize - The optional attribute, which specifies the maximum size of a message to be cached in bytes and defaults to unlimited.
  • implementation - The attribute, which defines if the cache is disk or memory based.
  • maxSize - The attribute, which defines the maximum number of elements to be cached.

UI Configuration

The following Cache Mediator options are available:

Cache Mediator General Options
  • Cache Id - Specify an ID for the Cache Mediator to have two instances with the same id that correlates the response message into the cache for the request message hash, when you have multiple cache mediators within your Synapse configuration.

Note

You should have the same id for a cache mediator instance in incoming path and the corresponding mediator instance in outgoing message path.

  • Cache Scope - Scope of the cache.
    • Per-Host - The cache is kept only for the current host in a cluster.
    • Per-Mediator - The cache is kept once for the whole cluster.
  • Cache Type - Whether the mediator is in the incoming path (check request) or the outgoing path (cache the response).
    • Finder - Set if the message is in incoming path. This indicate the mediator find for the request hash of each incoming message.
    • Collector - Set if the message is in outgoing path. This indicate the mediator collect the response message in the cache.
  • Hash Generator - The logic for finding the hash which check against each incoming message.

    Tip

    Default to org.wso2.caching.digest.DOMHASHGenerator.

  • Cache Timeout - The cache timeout (the time to keep the cache before expiring) in seconds.
  • Maximum Message Size - The limit of the message to cache in bytes.
Cash Implementation Details
  • Implementation Type - Currently only "In-Memory" is available.
  • Maximum size

    Tip

    Default 1000 bytes

On Cache Hit

Specify the sequence to follow when the cache mediator is hit. You can either specify it as anonymous, where you can define child mediators for the Cache Mediator, or you can refer a named sequence of mediators from Configuration Registry or Governance Registry.

Note

You can configure the Mediator using XML. Click on "switch to source view" in the "Mediator" window.


Examples

In this example, first message will be sent to the endpoint specified as cache is not hit. The response will come to the Cache Mediator inside the out mediator 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.

<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>