skip to Main Content

This is a follow up question to an earlier one asked

So using Mule cache , can I refresh the underlying object store ( used by Mule cache ) so that periodically I can retrieve the data from database and pump it into object store .

What I am trying to achieve is whenever a request comes in – the data is attempted to be retrieved from cache . If not found ( miss ) I do NOT want to go immedicately retrieve data from the database ( rather retrieving data from database and pushing int into object store and cache should happen as a scheduled task )

Based on the earlier question my understanding is :

#1 This cannot be done with the inbuilt cache mechanism provided by Mule since this cache manages underlying data itself

#2 I think my use case can be achieved by using an Object store BUT without a cache – so while I can store data periodically and use it while querying I cannot use mule cache

#3 Probably a better solution / rather than an Object store could be something like redis cache but thats a lot of work !

Kindly confirm if this is correct
Thanks

This is a followup to earlier question

Update#1
So in this case I think a picture will be better to explain what I am trying to do and to also elaborate on what @aled mentioned in the first part of his answer ( thanks @aled )

Here is the customer request flow ( customer wants to retrieve employee data based on employee id )
customer flow

And here is the scheduler flow which is pumping data into cache:
enter image description here

So now the scheduler is able to load data into cache successfully
( used keyGenerationExpression='#[(payload.id default "")]'

So lets say the scheduler loads into cache :

    {
    "id" : "001",
    "name" : "John"
    }

Now if customer requests for id 001 it correctly retrieves from cache
However if customer requests for any other id ( say 002 ) then inadverently its a cache miss causing this id to be populated in the cache – this is NOT what I want , my requirement is to simply not populate the cache and return with empty results which tells me this empId is not present.

I am not sure if this is possible using Mule cache ?

Here is the code :
Customer flow :

<flow name="customer_requesting_info_flow">
    <http:listener doc:name="Listener" path="/caching/*" config-ref="HTTP_Listener_config"/>
    
    <set-payload value='#[%dw 2.0
output application/java
---
{
    "id" : payload.empId
}]' doc:name="get emp id from request and then try to extract details from cache"  />
    <ee:cache doc:name="Cache" cachingStrategy-ref="Caching_Strategy">
        <logger level="INFO" doc:name="Logger" message="Cache Miss - Not found in cache"/>
</ee:cache>
        
        <ee:transform doc:name="Transform Message" doc:id="72bf1493-7f55-42de-9d30-84a9b03d257f" >
            <ee:message >
                <ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
payload]]></ee:set-payload>
            </ee:message>
        </ee:transform>
        <logger level="INFO" doc:name="Logger" doc:id="9fdbd52e-4f31-414d-8443-2fc5fcde0298" message="done"/>
    </flow>

And here is the code for scheduler flow to push data into cache :

<flow name="load_cache">
        <scheduler doc:name="Scheduler">
            <scheduling-strategy >
                <fixed-frequency frequency="300000"/>
            </scheduling-strategy>
        </scheduler>
        <logger level="INFO" doc:name="Logger" message="started cron schedule"/>
<ee:transform doc:name="Transform Message">
            <ee:message >
                <ee:set-payload ><![CDATA[%dw 2.0
output application/java
---
    {
        "id" : "001",
        "name" : "John"
    }]]></ee:set-payload>
            </ee:message>
        </ee:transform>
        <flow-ref doc:name="load_cache_sub_flow" name="load_cache_sub_flow"/>
    </flow>
    <sub-flow name="load_cache_sub_flow">
        <ee:invalidate-cache doc:name="Invalidate Cache"  cachingStrategy-ref="Caching_Strategy"/>
        <ee:cache doc:name="Cache" cachingStrategy-ref="Caching_Strategy">
            <logger level="INFO" doc:name="Logger" message='#["Loading data into cache" ++ (payload.id)]'/>
        </ee:cache>
    </sub-flow>

2

Answers


  1. One possible alternative would be to put the cache scope in a subflow. Then you can call that subflow from a flow with the scheduler, setting the right key generation expression, and also from the flow that answers the customer.

    You can implement your own cache module with the Mule SDK for Java. That requires Java development effort, but you can implement your backend in Redis or whatever implementation you want, including how to refresh the cache.

    Also Redis can be used as the implementation of an Object Store for the Cache Scope but that doesn’t change the functionality of the cache, only how its backing Object Store persists data.

    Login or Signup to reply.
  2. You can achieve it without using the cache scope and handling the Object Store directly.

    1. Create an object store and populate that in a flow triggered from a scheduler. Use os:store and not cache
    2. In the flow/subflow that is querying the required data, use a os:retrieve operation without a default value. If the key is present it will be successful, and you will get the cached data. If not, it will fail with a KEY_NOT_FOUND error and you can handle the data as you like
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search