caching-in-mule

2016-11-02T16:44:12.000Z
Tags: mule programming caching

Trying caching in mule today. Found that mule has a caching scope component that is easy to use, simply add one to a flow and add flows/other elements you are trying to cache to that cache scope and mule will cache that response message for you, by default it uses an InMemoryObjectStore.

In addition you can setup a filter which will be used to determine if the cache should be used. I was able to setup a simple example using the default cache configuration that comes out of the box and for testing simple cache it worked great.  The moment I wanted to do a little more I ran into some problems.  One of the things I wanted to do was to clear the cache, found a message processor to do that:

<ee:invalidate-cache xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
cachingStrategy-ref="Caching*Strategy"/>

However with the default cache this did not work. Instead I had to define a caching strategy.  I used the following one for my simple project:

<ee:object-store-caching-strategy name="Caching_Strategy" doc:name="Caching Strategy">
  <managed-store storeName="NonPersistentMangedObjectStore" maxEntries="33"
  entryTTL="2000000" expirationInterval="500000"/>
</ee:object-store-caching-strategy>

With this I was able to cache and clear the cache in addition there is a way to clear a specific key in the cache for that I used the following message processor:

<ee:invalidate-key xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
cachingStrategy-ref="Caching*Strategy"/>

This allowed me to create my simple caching project.  My next step was to cache an API at work. When I tried this same approach I ran into some problems.  I got no errors and every time I made a request I would not hit the cache, I would hit the actual API server.  After a day of struggling I wanted to see what was in the actual cache, maybe there was a way to see the keys in the cache?  It took me a little while to find and figure out a MEL expression to do that I found this:

#[''\\nCache has: ''+ app.registry.Caching_Strategy.getStore().allKeys()]

Using this expression I was able to further troubleshoot my issue.

It turns out that my response message was not being cached by mule.  It took me re-reading the documentation on caching scopes a few times.

Finally I found out that my message response payload was something mule calls a consumable payload, and that was the reason my message wasn't being cached, after I fixed that, by adding an object to string transformer which then made my message payload not consumable we were golden.

here is a flow of my project: flow

Last Updated: 5/21/2019, 1:44:44 PM