Cache Scope

 

As the name suggests Cache scope is used to store or cache the result for frequently used request. Imagine a Product API GET method which can accept product # and provide product detail in response. API can fetch the product information from external Data Source which can be DB, File or any third part service. This can be and time consuming and expensive calls in case most of the time product information doesn’t change frequently. To increase the performance or throughput of an API Caching is introduced.

Scope will save the data against the key defined in scope and reuse the cache result instead of processing the actual logic.

As a POC we will create two flows within mule application

  • nocache – flow will accept productid as header input and provide the result
  • cache – flow will accept the productid and used it as key for Cache scope and provide the result either from cache or form actual implementation.

Create http listener with path as nocache

Cache Scope

 

Add logger and payload transformation with 5 second wait

Cache Scope

%dw 2.0
import * from dw::Runtime
output application/json
---
{ 
	"product id":  attributes.headers."productid",
	"product desc": "product description"	
} wait 5000

 

Now if we run this application and test it through SOAP UI, we can see API took good 5 seconds to provide proper response

Cache Scope

 

Add a new flow with http listener with path as cache

Add same activities but in Cache Scope

Cache Scope

 

Configure Cache Scope

Select “Reference to a Strategy” and click add

 

Select key expression and provide key against which we want to save the result

 

We can also see Object Store reference – this is need to define for how long you want to cache the result. Max no of results you want to cache and all.

Configure Object Store – more detail on Object Store

Add Object Store module if not available in Pallet – more detail on adding module to pallet

 

Go to Global settings and select Object store

 

Define the Object Store settings

 

This will create Object store configuration

 

Will go back to Cache scope configuration and select the Object Store configuration

Click Ok

Run the application and test the same

For first time for productid – 1234 API took > 5seconds to return the response

 

Now if we test again then we can see the response is coming in 30 mille seconds

So Cache activity will keep on adding the result/payload against each key and use it accordingly.

Sample application – cache-sample

Sample SOAP UI project – Cache-Scope-Sample-soapui-project

 

  
Thank you for taking out time to read the above post. Hope you found it useful. In case of any questions, feel free to comment below. Also, if you are keen on knowing about a specific topic, happy to explore your recommendations as well.
 
For any latest updates or posts on our website, you can follow us on LinkedIn. Look forward to connecting with you there.


Share this:
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments