Mule Tutorial Mulesoft Tutorial

Caching or Cache Scope – Mulesoft / Mule ESB Tutorial


Caching In Mule ESB or Cache Scope


In this Mule ESB tutorial we will look into what is caching and why to use it, how can we implement caching in mulesoft project and  configuration properties in Mule Cache Scope/Activity. Also a step by step configuration of mule cache scope/activity and how to cache information retrieved from database. Please refer to Mule Tutorial: Connecting with Database mule tutorial to know how to connect to database in Mule ESB.

What is caching and why to use it?


Caching is a concept with is used to store frequently used data in the memory, file system or database which saves processing time and load if it would have to be access from original source location every time.

For example: We have to create an API to retrieve user information, that has connect to an external database which is on different server and fetch the records. (Assumption: external DB is not changing frequently)

Implementation Without Cache:
For every request my API will connect to the external server DB and search for the record in DB and fetch the record.
if we receive 100 request for the same user, then i need to connect and executed DB query 100 times.

Implementation With Cache:
For every request for different user my API will connect to the external server DB and search for the record in DB and fetch the record and save in an internal cache.
if we receive 100 request for the same user, then i need to connect and executed DB query only once, for all the 99 request we can fetch the record for cache and return the response.

What we can clearly see in the above example is that with cache we can reduce the cost of reconnecting to DB again and again for the same request 99 times. This makes a whole lot of difference when we are dealing with huge number of request in a very short duration.

Caching in Mule ESB: Mule Cache Scope and it configuration details:


Caching in mule ESB can be done by Mule Cache Scope. Mule Cache Scope has 3 storage types –

  1. In-memory:
    This store the data inside system memory. The data stored with In-memory is non-persistent which means in case of API restart or crash, the data been cached will be lost.
    Configuration Propertied

    1. Store Name
    2. Maximum number of entries
    3. TTL (Time to live)
    4. Expiration Interval
  2. Managed-store:
    This stores the data in a place defined by ListableObjectStore. The data stored with Managed-store is persistent which means in case of API restart or crash, the data been cached will no be lost.
    Configuration Propertied

    1. Store Name
    2. Maximum number of entries
    3. TTL (Time to live)
    4. Expiration Interval
    5. Persistence (true/false)
  3. Simple-test-file-store:
    This stores the data in a file. The data stored with Simple-test-file-store configuration is persistent which means in case of API restart or crash, the data been cached will no be lost.
    Configuration Propertied

    1. Store Name
    2. Maximum number of entries
    3. TTL (Time to live)
    4. Expiration Interval
    5. Persistence (true/false)
    6. Name and location of the file

 

Creating the flow:


1. Creating a RAML


In the below RAML we have created a GET method to get details of the users with the endpoint URI as /user/queryuser and query parameter as user_id. So my final endpoint will be http://<hostname>:<port>/user/queryuser?user_id=<user id>

mulecacheraml

2. Creating Mule Flow


mulecachemuleflow

When the request come to the main flow get:/user/queryuser:tutorialsAtoZ_caching-config
1. Logger logs the query parameter user_id.
2. sets a flow variable userID with the value received by query parameter user_id.
3. Flow reference will refer and call Cache flow (tutorialsAtoZ_caching_SubFlow).
4. On completion of Cache flow, Cache flow response is logged and response is send back in json format.

You can copy and paste the below mule code in xml.


















<![CDATA[select * from tutorial.user where id = #[flowVars.userID]]]>




3. Configuring DB connector


You can refer to here for connection mule with mysql DB. Also download the sql file here.
In our DB connector we are using select query and connection to table user to fetch the records base on the userid send in the query parameter.

mulecachedbconfig

3. Configuring Cache Activity


Inside Caching_Strategy configuration we will set key expression to the userid value received in query parameter.
Key Expression: (Optional) Enter an expression that Mule should use to generate a key. Here in this example the key generated will be userid which will be used to refer the data received from the database.

mulecachecacheconfig

mulecachecacheconfig1

How whole flow works:


mulecachemuleflow_req1
When userid is set to 1 and request is fired:

  • API set a flow variable userID with the value received by query parameter user_id.
  • Flow reference will refer and call cache flow (tutorialsAtoZ_caching_SubFlow).
  • the Cache activity checks if the userid already exists in its Key list.
  • when the key is not found then the activities inside the cache is executed.
  • Database activity executes the query and the record fetched is saved in the catch along with the key.
  • The above flow execute 1,2,3,4,5,6,7 activity in serial order.

When again userid is set to 1 and request is fired:

  • API set a flow variable userID with the value received by query parameter user_id.
  • Flow reference will refer and call cache flow (tutorialsAtoZ_caching_SubFlow).
  • the Cache activity checks if the userid already exists in its Key list.
  • when the key is found, none of the activity inside the cache is executed.
  • and the record associated to that key is retrieved form the memory and send as response.
  • The above flow executes 1,2,3,7 activity in serial order. 4, 5, 6 are not executed.

About Varun Goel

Varun Goel is a technology enthusiast with 6+ years exp in IT industry. In fact, he is been developing application after schooling as freelancer. Currently working with one of the Fortune’s 100 Companies having vast experience Mule ESB, Tibco, HTML5, CSS, JSS, Android, Core Java, JSP, PHP, MySQL, AutoCAD, Maya, ZBrush, Photoshop, Flash CS and many more.

2 comments

  1. Radhika

    Very useful, help full
    It is very help full for mule beginner .
    Could you please also add some sample tutorial

Leave a Reply

Your email address will not be published.