|
| 1 | +--- |
| 2 | +title: Caching |
| 3 | +nextjs: |
| 4 | + metadata: |
| 5 | + title: Caching |
| 6 | + description: Learn how caching works and how to control it. |
| 7 | +--- |
| 8 | + |
| 9 | +By default, all calls to Expression built-in functions are cached for performance. |
| 10 | +This means that if you call the same function with the same arguments multiple times, |
| 11 | +the result will be retrieved from the cache instead of being recalculated. |
| 12 | + |
| 13 | +For example, if you have an Expression that for a given reason executes the same `Query` |
| 14 | +twice with the same parameters, the second execution will return the cached result of the first execution, |
| 15 | +preventing the Salesforce governor limits from being hit. |
| 16 | + |
| 17 | +This aggressive caching is generally safe, because all Expression functions are pure (give the same |
| 18 | +output for the same input, without side effects). Caching is also contextual, meaning that if you |
| 19 | +call the `expression.Evaluator.run` method multiple times with different contexts, the caches will not |
| 20 | +interfere with each other, even if you have the same function calls in different expressions. |
| 21 | + |
| 22 | +But there are situations where you might want to disable caching, for example, if you have a custom |
| 23 | +function that performs non-deterministic operations (like generating random numbers) or side effects, like |
| 24 | +DML operations. For these cases, you can control the caching behavior in the following ways: |
| 25 | + |
| 26 | +## Global Caching |
| 27 | + |
| 28 | +Global caching is enabled by default for all Expression built-in (standard) functions. You can |
| 29 | +disable it by calling the `disableStandardFunctionResultCaching` method of the `Configuration` object: |
| 30 | + |
| 31 | +```apex |
| 32 | +expression.Configuration config = new expression.Configuration() |
| 33 | + .disableStandardFunctionResultCaching(); |
| 34 | +expression.Evaluator.run('your expression here', config); |
| 35 | +``` |
| 36 | + |
| 37 | +## User-defined Function Caching |
| 38 | + |
| 39 | +You can control cache independently for any function you declare yourself, by using the `nocache` keyword. |
| 40 | +See [Declaring Functions](./declaring-functions) for more information. |
| 41 | + |
| 42 | +But be aware that if global caching is enabled, the `nocache` keyword will have no effect on built-in functions |
| 43 | +called from within your custom function. These will still be cached. |
| 44 | + |
| 45 | +## Custom Metadata Type Functions |
| 46 | + |
| 47 | +If you are using [Custom Metadata Type Functions](./custom-functions), you can control caching by setting |
| 48 | +the `Cache Result` field on the Custom Metadata record to `false`. This flag is independent of the global caching |
| 49 | +setting, |
| 50 | +so regardless of whether global caching is enabled or disabled, setting this flag will enable/disable caching for that |
| 51 | +specific function. |
0 commit comments