Store and Retrieve Responses in the System Cache in WSO2.

Store and Retrieve Responses in the System Cache in WSO2.

Storing and retrieving responses in the system cache using WSO2 can significantly enhance the performance and efficiency of your applications. By leveraging the caching capabilities of WSO2, you can reduce response times, improve scalability, and optimize resource utilization. This not only saves time and energy but also ensures a smooth and seamless user experience.

The cache mediator uses hashing to determine if the request is already in the cache. When a request enters the ESB, it generates the hash value for the request.

If the hash value exists, the ESB sends the relevant response back to the client. Otherwise, the message will be sent to the backend. Just before sending the result back to the client, it is kept 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 refers to the duration for which the cache is stored, and the response is sent back to the client without proceeding to the 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 using the 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 on Cache-MediatorCompositeExporter and select Export Project Artifacts and Run.

You will then see a dialog box. Choose 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 out sequence

Drag and Drop the Cache Mediator

Configure the Cache Properties:

Cache Mediator Implementation: Default

Cache Type: COLLECTOR

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.

The article talks about storing and retrieve responses in the system cache in WSO2. For more information, please write to [email protected].