Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

When a message comes to the Cache Mediator, it checks whether an equivalent message is seen before. If the message is seen before, it executes 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 the ESB for sending the response. If a sequence is specified for a cache hit, the user can send back the response message within this sequence using a Send Mediator. If a sequence is not specified, the cached response is sent back to the requester.

...

Table of Contents
maxLevel3
minLevel3
locationtop
styleborder:1

...

typeflat
separatorpipe

...

Syntax

Code Block
XML
XML

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

...

  • 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 - Sets if the message is in an incoming path. This indicates the mediator to find for the request hash of each incoming message.
    • Collector - Sets if the message is in outgoing path. This indicates the mediator to collect the response message in the cache.
  • Hash Generator - The logic for finding the hash which checks against each incoming message.

    Info
    titleTip

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

  • Cache Timeout - The cache timeout (the time to keep the cache before it expires) in seconds.
  • Maximum Message Size - The limit of the message to cache in bytes.

...

  • Implementation Type - Currently only "In-Memory" is available.
  • Maximum size

    Info
    titleTip

    Default 1000 bytes

...

Info
titleNote

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

...

Examples

  1. In this example, the first message is sent to the endpoint specified as cache is not hit. The response will come to the Cache Mediator inside the out mediator, which caches the response. The second similar message will match the cache and the response will be directly fetched from the cache and sent to the requester. This happens because no onCacheHit sequence is defined.

    Code Block
    XML
    XML

...

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

...

...

  1. According to this example configuration,

...

  1. if you define a cache collector using the cache mediator in the in sequence, you need to add the RESPONSE property to consider the message as a response message.

    Code Block
    languagexml
    <resource methods="POST GET" uri-template="/headerapi/*">
    	<inSequence>
    		<cache collector="false" timeout="5000">
    			<protocol type="HTTP">
    				<methods>GET, POST</methods>
    				<headersToExcludeInHash>*</headersToExcludeInHash>
    				<responseCodes>.*</responseCodes>
    				<hashGenerator>org.wso2.carbon.mediator.cache.digest.HttpRequestHashGenerator</hashGenerator>
    			</protocol>
    		</cache>
    		<call>
    			<endpoint>
    				<address uri="http://localhost:9000/services/SimpleStockQuoteService"/>
    			</endpoint>
    		</call>
    		<property name="RESPONSE" value="true" scope="default" type="STRING"/>
    		<enrich>
    			<source type="inline" clone="true">
    				<ax21:newvalue
    					xmlns:ax21="http://services.samples/xsd">testsamplevalue
    				</ax21:newvalue>
    			</source>
    			<target
    				xmlns:ax21="http://services.samples/xsd"
    				xmlns:ns="http://services.samples" action="sibling" xpath="//ns:getQuoteResponse/ns:return/ax21:volume"/>
    			</enrich>
    			<cache collector="true"/>
    			<respond/>
    		</inSequence>
Excerpt
hiddentrue

Description of the Cache Mediator in WSO2 ESB.