Mule compiles each script that the Dynamic Evaluate component runs, and then stores the compiled script in the DataWeave compiled scripts cache. When the component runs the same script again, Mule reuses the cached script instead of compiling it again. Mule identifies each cached script by its full text, so any change to the text, even a single character, creates a new script that Mule must compile and cache.
Because the Dynamic Evaluate component selects or builds a script for each event, different events can produce different scripts. Limit the number of script variations to what your use case requires, for example, one script per locale or message type.
Don’t build the script text from values that change on each event, such as payload fields, query parameters, or order IDs. If each event produces a different script:
-
Mule compiles a new script for every event, which adds processing time.
-
The cache fills with scripts that Mule never reuses, and memory usage grows until the cache reaches its size limit.
-
Mule evicts other cached scripts, which then must be compiled again.
Instead, pass the values that change on each event in the Parameters field, or reference them in the script through payload, vars, or attributes.
This example embeds the order ID in the script text, so Mule compiles and caches a new script for each order:
<!-- Avoid: the script text changes for each order -->
<set-variable variableName="orderScript"
value='#["output application/json --- { message: \"Order " ++ payload.orderId ++ " received\" }"]'/>
<ee:dynamic-evaluate expression="#[vars.orderScript]"/>
This example passes the order ID as a parameter instead, so the script text is the same for every order and Mule compiles it only once:
<set-variable variableName="orderScript"
value='output application/json --- { message: "Order " ++ orderId ++ " received" }'/>
<ee:dynamic-evaluate expression="#[vars.orderScript]">
<ee:parameters>#[{orderId: payload.orderId}]</ee:parameters>
</ee:dynamic-evaluate>
To set the maximum number of compiled scripts in the cache, use the mule.weave.script.parser.cache.size system property. For more information, see DataWeave Memory Management.