Cache Usage Samples - 6.3

Talend ESB Mediation Developer Guide

EnrichVersion
6.3
EnrichProdName
Talend Data Fabric
Talend Data Services Platform
Talend ESB
Talend MDM Platform
Talend Open Studio for ESB
Talend Real-Time Big Data Platform
task
Design and Development
EnrichPlatform
Talend ESB

Example: Configuring the cache

from("cache://MyApplicationCache" +
   "?maxElementsInMemory=1000" + 
   "&memoryStoreEvictionPolicy=" +
   "MemoryStoreEvictionPolicy.LFU" +
   "&overflowToDisk=true" +
   "&eternal=true" +
   "&timeToLiveSeconds=300" + 
   "&timeToIdleSeconds=true" +
   "&diskPersistent=true" +
   "&diskExpiryThreadIntervalSeconds=300")

Example: Adding keys to the cache

RouteBuilder builder = new RouteBuilder() {
   public void configure() {
      from("direct:start")
         .setHeader(CacheConstants.CACHE_OPERATION, 
             constant(CacheConstants.CACHE_OPERATION_ADD)) 
         .setHeader(CacheConstants.CACHE_KEY, 
             constant("Ralph_Waldo_Emerson")) 
         .to("cache://TestCache1")
   }
};

Example: Updating existing keys in a cache

RouteBuilder builder = new RouteBuilder() {
   public void configure() {
      from("direct:start")
         .setHeader(CacheConstants.CACHE_OPERATION, 
             constant(CacheConstants.CACHE_OPERATION_UPDATE)) 
         .setHeader(CacheConstants.CACHE_KEY, 
             constant("Ralph_Waldo_Emerson"))
         .to("cache://TestCache1")
   }
};

Example: Deleting existing keys in a cache

RouteBuilder builder = new RouteBuilder() {
   public void configure() {
      from("direct:start")
         .setHeader(CacheConstants.CACHE_OPERATION, 
             constant(CacheConstants.CACHE_DELETE)) 
         .setHeader(CacheConstants.CACHE_KEY, 
             constant("Ralph_Waldo_Emerson"))
         .to("cache://TestCache1")
   }
};

Example: Deleting all existing keys in a cache

RouteBuilder builder = new RouteBuilder() {
   public void configure() {
      from("direct:start")
         .setHeader(CacheConstants.CACHE_OPERATION, 
             constant(CacheConstants.CACHE_DELETEALL)) 
         .to("cache://TestCache1");
   }
};

Example: Notifying any changes registering in a Cache to Processors and other Producers

RouteBuilder builder = new RouteBuilder() {
   public void configure() {
      from("cache://TestCache1").process(new Processor() {
         public void process(Exchange exchange) throws Exception {
            String operation = 
               (String) exchange.getIn().getHeader(
               CacheConstants.CACHE_OPERATION);
            String key = (String) 
               exchange.getIn().getHeader(CacheConstants.CACHE_KEY);
            Object body = exchange.getIn().getBody();
           // Do something
         } 
      })
   } 
};

Example: Using Processors to selectively replace payload with cache values

RouteBuilder builder = new RouteBuilder() {
   public void configure() {
     //Message Body Replacer
      from("cache://TestCache1")
         .filter(header(CacheConstants.CACHE_KEY).isEqualTo("greeting")) 
         .process(new CacheBasedMessageBodyReplacer(
     							"cache://TestCache1","farewell"))
         .to("direct:next");  
      //Message Token replacer
      from("cache://TestCache1")
         .filter(header(CacheConstants.CACHE_KEY).isEqualTo("quote"))
         .process(new CacheBasedTokenReplacer(
    							"cache://TestCache1","novel","#novel#"))
         .process(new CacheBasedTokenReplacer(
    							"cache://TestCache1","author","#author#"))
         .process(new CacheBasedTokenReplacer(
    							"cache://TestCache1","number","#number#"))
         .to("direct:next");

      //Message XPath replacer
      from("cache://TestCache1")
         .filter(header(CacheConstants.CACHE_KEY)
         .isEqualTo("XML_FRAGMENT")) 
         .process(new CacheBasedXPathReplacer(
    							"cache://TestCache1","book1","/books/book1"))
         .process (new CacheBasedXPathReplacer(
    							"cache://TestCache1","book2","/books/book2"))
         .to("direct:next");
   }
};

Example: Getting an entry from the Cache

from("direct:start")
   // Prepare headers
   .setHeader(CacheConstants.CACHE_OPERATION, constant(
      CacheConstants.CACHE_OPERATION_GET))
   .setHeader(CacheConstants.CACHE_KEY, constant("Ralph_Waldo_Emerson"))
   .to("cache://TestCache1").
   // Check if entry was not found
   .choice().when(header(
      CacheConstants.CACHE_ELEMENT_WAS_FOUND).isNull()).
   // If not found, get the payload and put it to cache
   .to("cxf:bean:someHeavyweightOperation")
   .setHeader(CacheConstants.CACHE_OPERATION, constant(
      CacheConstants.CACHE_OPERATION_ADD))
   .setHeader(CacheConstants.CACHE_KEY, 
      constant("Ralph_Waldo_Emerson"))
   .to("cache://TestCache1")
   .end()
   .to("direct:nextPhase");

Example: Checking for an entry in the Cache

Note: The CHECK command tests existence of an entry in the cache but doesn't place a message in the body.

from("direct:start")
   // Prepare headers
   .setHeader(CacheConstants.CACHE_OPERATION, 
      constant(CacheConstants.CACHE_OPERATION_CHECK))
   .setHeader(CacheConstants.CACHE_KEY, constant("Ralph_Waldo_Emerson"))
   .to("cache://TestCache1").
   // Check if entry was not found
   .choice().when(header(
      CacheConstants.CACHE_ELEMENT_WAS_FOUND).isNull())
   // If not found, get the payload and put it to cache
   .to("cxf:bean:someHeavyweightOperation").
   .setHeader(CacheConstants.CACHE_OPERATION, 
       constant(CacheConstants.CACHE_OPERATION_ADD))
   .setHeader(CacheConstants.CACHE_KEY, 
       constant("Ralph_Waldo_Emerson"))
   .to("cache://TestCache1")
   .end();