Mule 3 Mulesoft Basics 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.

24 comments

  1. alpha femme keto Ingredients

    Hey I am so excited I found your weblog, I really found you by error, while I was researching on Yahoo for something else, Nonetheless I am here now and
    would just like to say thank you for a fantastic post and a all round thrilling blog
    (I also love the theme/design), I don’t have time to read it all at the moment but I have saved it and also added in your RSS feeds, so when I have time I will be back
    to read much more, Please do keep up the superb job.

  2. plumbing services look at more info

    I do love the way you have framed this difficulty and it does indeed provide me some fodder for consideration. Nonetheless, from just what I have personally seen, I only wish as the actual remarks pack on that men and women keep on issue and not get started upon a tirade of some other news du jour. Anyway, thank you for this excellent piece and although I do not really agree with this in totality, I respect the perspective.

  3. local plumber near me look at this now

    Its like you learn my mind! You seem to know a lot approximately this, such as you wrote the e book in it or something. I believe that you simply could do with a few to power the message house a bit, however other than that, that is magnificent blog. A great read. I’ll certainly be back.

  4. Ac repair

    Hi would you mind letting me know which webhost you’re working with? I’ve loaded your blog in 3 different internet browsers and I must say this blog loads a lot quicker then most. Can you recommend a good web hosting provider at a honest price? Many thanks, I appreciate it!

  5. Bridgette Snide

    I like the valuable info you supply for your articles. I will bookmark your blog and check once more right here frequently. I am quite certain I will learn a lot of new stuff right here! Best of luck for the following!

  6. Kathlene

    Howdy! Someone in my Facebook group shared this website with us so I came to look it over.

    I’m definitely loving the information. I’m book-marking and will be tweeting this to my followers!

    Exceptional blog and great style and design.

  7. Angel

    We stumbled over here different web address and thought I
    should check things out. I like what I see so now i’m
    following you. Look forward to checking out your web page repeatedly.

  8. Audra

    Hello there I am so excited I found your blog, I really found you by mistake, while I was searching on Bing for something else, Nonetheless I am
    here now and would just like to say cheers for a marvelous post and a all round
    exciting blog (I also love the theme/design), I don’t have time to read through it all at the moment but
    I have bookmarked it and also added in your RSS feeds, so when I have time I will be
    back to read much more, Please do keep up the superb work.

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