Search code examples
mulemulesoftmule4

Mule 4 Object store and caching - need to store records from Db in cache and then use them from cache


I am using Mule 4.4 on premise . My use case is to load some static data from a database table and to store it in a cache. I need to periodically also refresh the cache ( say every 24 hours ) The static data is employee data .

So here is my code with scheduler which will populate cache:

<flow name="load_cache" doc:id="8eaefe57-6de8-4d4f-97d5-fa181ea54b8b" >
        <scheduler doc:name="Scheduler" doc:id="b3fb9ce7-7254-481e-a137-d37ed1cc30a3" >
            <scheduling-strategy >
                <fixed-frequency />
            </scheduling-strategy>
        </scheduler>
        <ee:transform doc:name="emp data from db" doc:id="b5d11958-3ea7-4596-9aed-51c616ae67f0" >
            <ee:message >
                <ee:set-payload ><![CDATA[%dw 2.0
output application/java
---
[
    {
        "id" : "001",
        "name": "John"
    },
    {
        "id" : "002",
        "name" : "Jane"
    }
]]]></ee:set-payload>
            </ee:message>
        </ee:transform>
        <foreach doc:name="For Each" doc:id="da8ec47c-7b34-4707-ac78-6ffd433fb40b" >
            <os:store doc:name="Store" doc:id="d139954b-a4b7-4351-9463-8baccb5e2aba" key="#[payload.id]" objectStore="Object_store">
                <os:value ><![CDATA[#[payload.name]]]></os:value>
            </os:store>
        </foreach>
    </flow>

here the transform component 'emp data from db' is simply a transform component simulating results from db. While storing in Object store the key used is the id of each object

So now that the object store is getting periodically populated I would like to test - so have defined a sinple flow with a HTTP Listener

<flow name="header_pass_01Flow" doc:id="dc0c28a7-c277-4192-b641-e73af8afbde3" >
        <http:listener doc:name="Listener" doc:id="57e56cd6-a47e-456b-8c6d-e57627c673b8" path="/caching/*" config-ref="HTTP_Listener_config"/>
        <set-variable value='#["001"]' doc:name="get emp name for empId" doc:id="130a350e-d23b-41f6-86c3-fe8104b63d3f" variableName="id"/>
        <ee:cache doc:name="Cache" doc:id="a11f6da2-d24e-4a69-a0b1-647c0b6dfda8" cachingStrategy-ref="Caching_Strategy">
            <logger level="INFO" doc:name="Logger" doc:id="fdeaaeff-06df-45d7-8c1f-410f343ae4e8" message="Not found in cache"/>
        </ee:cache>
    </flow>

The cache uses an underlying Object store ( the same one which is getting populated by the scheduler.

However - even though I am setting a variable id with the value of 001 before reaching cache the request always gets a 'miss' i.e never finds this id in the cache .... not sure why ?

Here is the complete code of the other components:

<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="f1bec56d-de65-4a93-913d-07a7426d6bf6" >
        <http:listener-connection host="0.0.0.0" port="9091" />
    </http:listener-config>
    
    <os:object-store name="Object_store" doc:name="Object store" doc:id="152be16e-2fd9-4d48-8310-3d6d45be4bff" maxEntries="10" entryTtl="2" entryTtlUnit="HOURS" />
    <ee:object-store-caching-strategy name="Caching_Strategy" doc:name="Caching Strategy" doc:id="79383e4a-b907-450d-92c9-9d15b1efbb81" objectStore="Object_store" />

How do I ensure that the data I am populating via scheduler is available in cache such that on querying I get data from cache ?

am expecting data to be retrieved from cache


Solution

  • It seems that you are confusing the functionality of the Cache Scope and of the Object Store. The Cache Scope uses internally an Object Store to store the information to catch. You should not use that Object Store directly to put the values. Cache Scope automatically catches the information that enters the scope when executing its flow.

    In your example you should put the transformation from the first flow inside the Cache Scope of the second. Keep the logger to verify when it executes or not.