How to use Cache scope

10
How to use Cache Scope 29-05-2015

Transcript of How to use Cache scope

Page 1: How to use Cache scope

How to use Cache Scope

29-05-2015

Page 2: How to use Cache scope

Abstract

• The main motto of this PPT is How to use Cache Scope in our applications.

Page 3: How to use Cache scope

Introduction• The Cache Scope saves on time and processing load by

storing and reusing frequently called data. You can put any number of message processors into a cache scope and configure the caching strategy to store the responses (which contain the payload of the response message) produced by the processing that occurs within the scope. Mule’s default caching strategy defines how data are stored and reused, but if you want to adjust cache behavior, you can customize a global caching strategy in Mule and make it available for use by all cache scopes in your application.

Page 4: How to use Cache scope

Example

Page 5: How to use Cache scope

.mflow• <?xml version="1.0" encoding="UTF-8"?>

• <mule xmlns:mulexml="http://www.mulesoft.org/schema/mule/xml" xmlns:ws="http://www.mulesoft.org/schema/mule/ws" xmlns:metadata="http://www.mulesoft.org/schema/mule/metadata" xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"

• xmlns:spring="http://www.springframework.org/schema/beans" • xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"• xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd• http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd• http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd• http://www.mulesoft.org/schema/mule/ws http://www.mulesoft.org/schema/mule/ws/current/mule-ws.xsd• http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd• http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd• http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd• http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd">• <ws:consumer-config name="Web_Service_Consumer" wsdlLocation="http://www.webservicex.com/globalweather.asmx?WSDL " service="GlobalWeather"

port="GlobalWeatherSoap" serviceAddress="http://www.webservicex.com/globalweather.asmx" doc:name="Web Service Consumer"/>• <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8088" doc:name="HTTP Listener Configuration"/>• <ee:object-store-caching-strategy name="Caching_Strategy" doc:name="Caching Strategy" keyGenerationExpression="#[flowVars.cntry]">• <in-memory-store/>• </ee:object-store-caching-strategy>• <flow name="CacheFlow">• <http:listener config-ref="HTTP_Listener_Configuration" path="/cache" doc:name="HTTP"/>• <logger message="--flow started-" level="INFO" doc:name="Logger"/>• <set-variable variableName="cntry" value="#[message.inboundProperties.'http.query.params'.country]" doc:name="Variable"/>• <logger message="--country--#[flowVars.cntry]--" level="INFO" doc:name="Logger"/>• <ee:cache cachingStrategy-ref="Caching_Strategy" doc:name="Cache">• <logger message="in cache scope" level="INFO" doc:name="Logger"/>• <flow-ref name="CacheFlow1" doc:name="CacheFlow1"/>• <mulexml:object-to-xml-transformer doc:name="Object to XML"/>• <set-payload value="Final payload:#[payload]" doc:name="Set Payload"/>• </ee:cache>• </flow>•

Page 6: How to use Cache scope

• <flow name="CacheFlow1">• <logger message="--Hai--" level="INFO" doc:name="Logger"/>• <set-payload value="{&quot;country&quot;:&quot;#[flowVars.cntry]&quot;}" doc:name="Set Payload"/>• <json:object-to-json-transformer doc:name="Object to JSON"/>• <logger message="--Hai--#[payload]" level="INFO" doc:name="Logger"/>• <dw:transform-message metadata:id="49568218-767a-44c0-a2dc-4cb3928057ad" doc:name="Transform

Message">• <dw:set-payload><![CDATA[%dw 1.0• %output application/xml• %namespace ns0 http://www.webserviceX.NET• ---• {• ns0#GetCitiesByCountry: {

• ns0#CountryName: payload.country

• }• }]]></dw:set-payload>• </dw:transform-message>• <ws:consumer config-ref="Web_Service_Consumer" operation="GetCitiesByCountry" doc:name="Web Service

Consumer"/>• </flow>• </mule>

Page 7: How to use Cache scope

• Output:• INFO 2016-05-29 09:53:03,598 [Mule.app.deployer.monitor.1.thread.1]

org.mule.module.launcher.MuleDeploymentService: • ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++• + Started app 'Cache' +• ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

• Trigger1:• INFO 2016-05-29 09:53:07,951 [[Cache].HTTP_Listener_Configuration.worker.01]

org.mule.api.processor.LoggerMessageProcessor: --flow started-• INFO 2016-05-29 09:53:08,000 [[Cache].HTTP_Listener_Configuration.worker.01]

org.mule.api.processor.LoggerMessageProcessor: --country--india--• INFO 2016-05-29 09:53:08,001 [[Cache].HTTP_Listener_Configuration.worker.01]

org.mule.api.processor.LoggerMessageProcessor: in cache scope• INFO 2016-05-29 09:53:08,001 [[Cache].HTTP_Listener_Configuration.worker.01]

org.mule.api.processor.LoggerMessageProcessor: --Hai--• INFO 2016-05-29 09:53:08,005 [[Cache].HTTP_Listener_Configuration.worker.01]

org.mule.api.processor.LoggerMessageProcessor: --Hai--{"country":"india"}

• Trigger2:• INFO 2016-05-29 09:53:48,830 [[Cache].HTTP_Listener_Configuration.worker.01]

org.mule.api.processor.LoggerMessageProcessor: --flow started-• INFO 2016-05-29 09:53:48,831 [[Cache].HTTP_Listener_Configuration.worker.01]

org.mule.api.processor.LoggerMessageProcessor: --country--india--

Page 8: How to use Cache scope
Page 9: How to use Cache scope

• Flow of execution:1. URL to trigger the service from browserhttp://localhost:8088/cache?country={name of the country}Example: http://localhost:8088/cache?country=india 2. A. For the first time it flow1 will call flow2 and gets the cities for the country code India.B. For the second trigger it will directly return the cashed response because the country code from the input is same as first trigger