Mule provides the Mule 4 Object Store connector that retrieves and stores values for up to 30 days.
The following example retrieves data from an object store and if no data is found for a specific key, a new value is retrieved from the external system and stored in the object store.
<try>
<os:retrieve key="token" target="myVar" objectStore="objectStore"/>
<error-handler>
<on-error-continue type="OS:KEY_NOT_FOUND" logException="false">
<http:request method="GET"
url="http://localhost:8081/retrieve"
target="myVar"/>
<os:store key="token" objectStore="objectStore">
<os:value>
#[vars.myVar]
</os:value>
</os:store>
</on-error-continue>
</error-handler>
</try>
In this example, the <os:retrieve key="token" target="myVar" objectStore="objectStore"/> statement executes the retrieve operation of the Object Store connector using the value token as the key to look for and assign the retrieved value to the myVar variable.
If no value is associated with a key, an error is thrown by the retrieve operation. This is why the operation is enclosed in a <try> block, so that should an error occur, it can be handled by retrieving a fresh value and storing it in the object store.
The HTTP request is implemented inside the <error-handler> block. The value is obtained through an HTTP call and assigned to the myVar variable. Then that value is stored in the object store using the store operation of the Object Store connector.
After this block executes, myVar contains a value that was retrieved from either the cache or the external system, and stored in the cache for later use. You can use that value for any purpose such as adding it as a new header to a request or response, or more.