This documentation is for WSO2 Enterprise Service Bus version 4.7.0 . View documentation for the latest release.
Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 6 Next »

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.



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 evaluates the hash value of an incoming message as described in the optional hash generator implementation (a class implementing the org.wso2.caching.digest.DigestGenerator class interface). The default hash generator is org.wso2.caching.digest.DOMHashGenerator. If the generated hash value is 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 - The Cache Mediator must be specified with an id and two instances with the same id that correlates the response message into the cache for the request message hash.
  • timeout - Optional attribute, which specifies the valid duration for cached elements. The scope defines if the 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's value as true specifies that the mediator instance is a response collection instance.
    • false - The collector attribute's value as false specifies that it's a cache serving instance.
  • maxMessageSize - An 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 - Id for cache 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 - 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.

    Tip

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

  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.

    <sequence name="SEQ_CACHE">
      <in>
        <cache scope="per-host"
          collector="false"
          hashGenerator="org.wso2.caching.digest.DOMHASHGenerator"
          timeout="20">
          <implementation type="memory" maxSize="100"/>
        </cache>
    
        <send>
          <endpoint>
            <address uri="http://localhost:9000/services/SimpleStockQuoteService"/>
          </endpoint>
        </send>
      </in>
    
      <out>
        <cache scope="per-host" collector="true"/>
        <send/>
      </out>
    </sequence>
  2. According to this example configuration, 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.

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

See also Sample 420: Simple Cache Implemented on ESB for the Actual Service.

  • No labels