The cache mediator plays a crucial role in the Enterprise Service Bus (ESB) by leveraging hashing to efficiently manage cached requests. When a request enters the ESB, it generates a unique hash value that is used to determine if the response is already stored in the cache. If the hash value matches an existing entry, the ESB can immediately retrieve and send the relevant response back to the client. However, if the hash value is not found, the message is forwarded to the backend for processing. Finally, the ESB stores the result in the cache just before sending it back to the client, optimizing performance for future requests.
The cache mediator uses hashing to determine if a request is already in the cache. When a request enters the ESB, it generates a hash value for the request. If the hash value exists, the ESB sends the relevant response back to the client. Otherwise, the message is sent to the backend. Just before sending the result back to the client, the ESB stores it in the cache.
Walkthrough:
Create new project in integration studio >> File >> New >> Integration Project and give the Project name.
After creating project Right Click on API >> New >> RestAPI >> Create new arifact given required filelds as Name and context >> Finish
Drag and Drop the Cache Mediator
Configure the Cache Properties:
Cache Type: FINDER
Cache Timeout(S): 10
the cache Timeout (cache Timeout is the amount of time cache is stored and response is sent back to client without further going in to next mediator)
Max Message Size(bytes): 2000
Drag and Drop the Payload Factory
Configure the Payload Factory Properties:
Drag and Drop the Respond Mediator
After cache mediator, Drag and Drop the Log Mediator
Drag and Drop the Send Mediator
In Send Mediator, Drag and Drop the Default HTTP Endpoint
Build and run the artifacts:
- Right-click Cache-MediatorCompositeExporter and click Export Project Artifacts and Run
- You will see the following dialog box. Select the Cache-MediatorConfigs folder in the artifact list and click Finish.
Get the response as shown in the figure below
{
“args”: {},
“headers”: {
“Accept”: “text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7”,
“Accept-Encoding”: “gzip, deflate, br, zstd”,
“Accept-Language”: “en-US,en;q=0.9”,
“Host”: “httpbin.org”,
“Sec-Ch-Ua”: “\”Chromium\”;v=\”122\”, \”Not(A:Brand\”;v=\”24\”, \”Google Chrome\”;v=\”122\””,
“Sec-Ch-Ua-Mobile”: “?0”,
“Sec-Ch-Ua-Platform”: “\”Windows\””,
“Sec-Fetch-Dest”: “document”,
“Sec-Fetch-Mode”: “navigate”,
“Sec-Fetch-Site”: “none”,
“Sec-Fetch-User”: “?1”,
“Upgrade-Insecure-Requests”: “1”,
“User-Agent”: “Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36”,
“X-Amzn-Trace-Id”: “Root=1-65e00c8f-2d53d6a310d7047c4de47297”
},
“origin”: “14.194.44.162”,
“url”: https://httpbin.org/get }
From above response X-Amzn-Trace-Id will change for every request. By using the cache mediator, we will catch the response for certain period of time (cache Timeout field in cache mediator).
In the outsequence
Drag and Drop the Cache Mediator
Configure the Cache Properties:
Cache Mediator Implementation: Default
Cache Type: COLLLECTOR
Drag and Drop the Payload Factory
Configure the Payload Factory Properties:
Drag and Drop the Send Mediator to return the response to client.
Execute the integration studio with a GET request to generate a URL within the integration studio for accessing the API via Chrome or any web browser.
It is noticeable that the response is same till the cache timeout value is exceeded. Once the cache timeout value (Time in sec) is exceeded new value for X-Amzn-Trace-Id is generated.
For more information, please write to [email protected].