Implementing Cache Scope in your MuleSoft Project

Ismeet Kaur
Another Integration Blog
5 min readMar 20, 2023

The cache is not always junk!

Overview

Cache scope refers to the visibility or accessibility of cached data in a computer system. The scope of a cache determines which parts of the system can access or utilize the cached data.

Cache Scope in MuleSoft

In MuleSoft, cache scope is a component that allows you to cache the result of an operation or transformation for a specified amount of time. This can help improve the performance of your application by reducing the number of times the operation needs to be executed. It is particularly effective for these tasks:

  • Processing repeated requests for the same information.
  • Processing requests for information that involve large repeatable streams.

The cache scope caches repeatable streams only. By default, all streams are repeatable unless configured to be non-repeatable.

Repeatable streams by definition are typically used when you need to process the same data multiple times, such as when performing data transformations or writing data to multiple endpoints. you can read the stream multiple times without losing data or causing side effects.

Non-repeatable streams by definition are typically used when you are working with large or infinite data sets that cannot be stored entirely in memory.

Caching Strategy

In MuleSoft caching strategy defines the actions a Cache Scope takes when a message is cached. By default, the Cache scope uses an in-memory object store to store data. MuleSoft also allows you to create a custom caching strategy that references an existing object store. You can also create a new custom object store to use in the caching strategy.

Caching strategy allows us to configure the cache size, expiration time, and maximum allowed entries by setting these values in the object store that you define or reference from the caching strategy.

Example Caching Strategy

In this tutorial, we will see a simple example of caching a Salesforce token to authenticate MuleSoft with salesforce. To do so we will create a session id to request an access token. Use that access token to connect to Salesforce Apex API

Prerequisites

  • Anypoint Platform credentials.
  • Anypoint Studio downloaded on your machine to develop implementation
  • Credentials to your Salesforce account.

Cache scope in action

Let us understand how is cache scope benefitting us. Let us assume that we have a throughput of 3 requests per minute. Our token expires in 1 minute. We have set the entry ttl to 1 min and the expiration time to 45 seconds. As per this, when the first request comes in, the mule event goes through the cache scope to the HTTP request connector to retrieve the token. Then this token is saved in the object store.
The next two requests coming in will be in the time interval of 1 minute. hence, it will retrieve the token from the object store, instead of requesting the token each time. This way we can save a significant number of callouts and save the number of requests sent to Salesforce and keep the operations executed in check.

Steps

  • Create a basic flow as shown in the image below.
    Here we have used a scheduler as an event source. It is set to run at a frequency of 1000ms.
Example Flow
Scheduler Frequency details
  • Token Request (vars.requestSessionID): Configure the token request to be sent to receive the OAuth token. Refer to the command below.
    “grant_type=” ++ grantType ++ “&client_id=” ++ p(‘sfdc.consumerKey’) ++ “&client_secret=” ++ p(‘sfdc.consumerSecret’) ++ “&username=” ++ p(‘sfdc.username’) ++ “&password=” ++ p(‘secure::sfdcPassword’) ++ p(‘sfdc.securityToken’)
    Definition to create the request token:
    grant_type = Always set to “Password”
    client_id = Salesforce consumer key
    client_secret = Salesforce consumer key
    username = Your username to log on to Salesforce org
    password = This is created by adding your Salesforce org password with the security Token
Creating token request to retrieve Oauth token
  • Configure the cache scope as below. In the caching strategy, choose reference to strategy. This will ensure that your data is cached in your defined object store instead of in memory.
Caching scope configuration
Configuring caching strategy for “reference to a strategy”

In the image above, we see the caching strategy global configuration allows us to set the reference to an object store, existing or new. The “Event Key” allows us to select how to generate the object’s key before storing it in the object store. This is usually done to secure the keys stored in the object store.
This can be done in 3 ways:
Default
Key Expression:
A DataWeave expression. Note that for two requests that are the same (“equal”), you need to generate the same key. Otherwise, you can get the wrong results.
Key Generator:
Requires you to implement the com.mulesoft.mule.runtime.cache.api.key.MuleEventKeyGenerator interface.

  • Implement Object store: To implement the object store, it is important to define time-to-live(entry ttl) and expiration intervals. This should be set up in such a way that the entry ttl is more that the expiration interval. Otherwise, the mule processor goes inside the cache scope and does not retrieve any key from the object store. In this example, we have configured entry ttl to 15 mins and expiration interval to 10 minutes
Configuring Object store
  • Configuring the HTTP Request connector. Use the following configurations in the image to configure the connector. Pass the created token request as the body of the request.
HTTP Request parameter configuration
  • Create a bearer token using the response from the HTTP request. by adding “Bearer” to the response.
Create bearer token

You can now use these credentials to connect to your Salesforce APIs.

To know more about the cache scope or watch video tutorials on Youtube, refer here.

--

--

Ismeet Kaur
Another Integration Blog

I want to inspire women to not be afraid and take on tech. I encourage all Women who Mule to stand stronger together.