Search code examples
cachingmulemulesoftmule-esb

Mule 4 Cache Scope. How to stop mule cache for based on certain conditions


I want to use cache in my mule app. The flow is very simple, I am querying DB on the basis of email, and I want to store the result only if it returns some rows. I referred to this question Mule Cache Scope How to invalidate mule cache for no payload of DB output but this solution is working for mule 3 only as Mule 4 uses repeatable streams.

I tried implementing the following workaround :

  1. Wrap the cache scope inside a try scope.
  2. Raise an error when I do not want the cache to be stored.
  3. On error continue error for only that error type

It executes the flow correctly, does not store the result in cache and also gives the successful response but it throws an error (probably in some async process) and I am seeing this in my logs

    ERROR 2020-11-20 17:54:36,923 [[MuleRuntime].uber.03: cachingStrategy_null @302bcf75] [processor: testFlow1/processors/1/processors/0/processors/1; event: 594def80-2b2b-11eb-a1d0-a0d37a4d6fab] org.mule.runtime.core.internal.exception.OnErrorPropagateHandler: 
    ********************************************************************************
    Message               : The mapper returned a null value.
    Element               : testFlow1/processors/1/processors/0 @ test:test.xml:131 (Cache)
    Element DSL           : <ee:cache doc:name="Cache" doc:id="8c09cdc7-4fc9-4de1-a961-f74323f6ea7b" cachingStrategy-ref="Caching_Strategy">
                            <logger level="INFO" doc:name="Copy_of_Logger" doc:id="c539273d-7da2-4a20-bbdc-11f9fd4424cc" message="inside cache scope."></logger>
                            <ee:transform doc:name="Transform Message" doc:id="be16423b-c018-4949-b441-e81c6abc65ef">
                            <ee:message>
                            <ee:set-payload>%dw 2.0
    output application/json
    ---
    if(payload.store) {
        id: payload.id,
        time: now()
    }
    else {}</ee:set-payload>
                            </ee:message>
                            <ee:variables>
                            <ee:set-variable variableName="cacheEntry">%dw 2.0
    output application/java
...
Root Exception stack trace:
java.lang.NullPointerException: The mapper returned a null value.
    at java.util.Objects.requireNonNull(Objects.java:228)
    at reactor.core.* (4 elements filtered from stack; set debug level logging or '-Dmule.verbose.exceptions=true' for everything)(Unknown Source)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at org.mule.service.scheduler.internal.AbstractRunnableFutureDecorator.doRun(AbstractRunnableFutureDecorator.java:111)
    at org.mule.service.scheduler.internal.RunnableFutureDecorator.run(RunnableFutureDecorator.java:54)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)

I am attaching a snippet of my flow too.

enter image description here


Solution

  • You can use the Invalidate Key operation to remove the key after it was added to the cache.

    It should added after the cache scope.