|
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 –
- 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
- Store Name
- Maximum number of entries
- TTL (Time to live)
- Expiration Interval
- 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
- Store Name
- Maximum number of entries
- TTL (Time to live)
- Expiration Interval
- Persistence (true/false)
- 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
- Store Name
- Maximum number of entries
- TTL (Time to live)
- Expiration Interval
- Persistence (true/false)
- 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>

|
2. Creating Mule Flow

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.

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


|
How whole flow works:

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.
|
|
Very useful, help full
It is very help full for mule beginner .
Could you please also add some sample tutorial
Thanks For Giving the Great Information about the Mule Soft