Caching is the temporary storing a result of some operations, in this case, caching fetched data from Salesforce. In this example, when we ask for the data again, results are returned from cache instead of making a new call to Salesforce.
This document describes the details of the example within the context of Anypoint™ Studio, Mule's graphical user interface (GUI). Where appropriate, the XML configuration is provided.
This document assumes that you are familiar with Mule Runtime and the Anypoint Studio interface. Further, this example assumes that you have a basic understanding of Mule flows and Mule Global Elements.
Example Use Case
In this example, the Salesforce querying operation is encapsulated within the cache scope component:
- The query to Salesforce is performed to fetch Contacts.
- Additional calls to Salesforce are avoided and data is returned from cache.
- The result is printed.
Check the Anypoint Studio console while running the example to see when the cache is used.
Set Up and Run the Example
Complete the following procedure to create, then run this example in your own instance of Anypoint Studio. Skip to the next section if you prefer to simply review this example.
- Open the Example project in Anypoint Studio from Anypoint Exchange. Don't run the application.
- Log in to your Salesforce account. From your account menu (your account is labeled with your name), select Setup.
- In the left navigation bar, under the Personal Setup heading, click to expand the My Personal Information folder.
- Click Reset My Security Token. Salesforce resets the token and emails you the new one.
- Access the email that Salesforce sent and copy the new token onto your local clipboard.
- In your application in Studio, click the Global Elements tab.
- Double-click the Salesforce global element to open its Global Element Properties panel. In the Security Token field, paste the new Salesforce token you copied from the email. Alternatively, configure the global element in the XML Editor.
- Change the contents of the Username and Password fields to your account-specific values, then click OK to save your changes.
- In the Package Explorer, right-click the connect-with-salesforce project name, then select Run As > Mule Application. Studio runs the application on the embedded server.
- In your browser, access http://localhost:8081/ and the Contacts are displayed in browser in JSON format.
- Go back to Anypoint Studio and click the console view and search for a log similar to this one:
INFO 2018-02-01 14:44:56,781 [[MuleRuntime].cpuLight.02: [cache-scope-with-salesforce-contacts].cache-scope-with-salesforce-contactsFlow.CPU_LITE @7f8feb5] [event: 0-16100290-0756-11e8-b986-f85971bd4f61] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: Querying Salesforce (not from Cache)
- Switch to your browser and access http://localhost:8081/ again.
- In Anypoint Studio console, search the logs to verify that caching is performed. The log message stating that the call Querying Salesforce (not from Cache) was performed is not in the console logs.
- The Contacts are displayed in JSON format in the browser and returned from the cache.
- Stop the Mule application by clicking the square, red terminate button in the Console.
How it Works
Using a single flow, this application queries Salesforce CRM for contacts and prints them in a browser. From the console log you can check when caching is performed. Feel free to experiment more with this example.
Salesforce Connector performs a query in your Salesforce Account: to get Id, FirstName, LastName, Email, and Phone attributes of Contact objects with a limit set to 200 records. To keep the example simple, the caching is illustrated via logs in console and Contacts are displayed in JSON format in a browser.