You can store the results of macro expressions in the application's cache. Caching allows the system to save resources when processing macros — macros only need to be resolved once, and subsequent occurrences load the result from the application's memory (until the cache expires).
To cache the result of a macro, enclose the expression into the Cache method (as the first parameter). Additionally, you can specify the following optional parameters for the method:
The following macro caches the value of the "string".ToUpper() expression:
{% Cache("string".ToUpper()) %}When the system resolves the macro, the result (STRING) is saved into the cache for 10 minutes. If the macro needs to be resolved again while the cache is still valid, the system does not process the macro at all, and loads the result directly from the cache.
Tip: You can confirm the caching functionality of macros by viewing the system's cache debugs (cache items and cache access).
The following example demonstrates how to specify the optional parameters when caching macros:
{% Cache(CurrentUser.GetFormattedUserName(), 5, true, "MacroCacheKey|UserName|" + CurrentUser.UserName, GetCacheDependency("cms.user|all")) %}When the system resolves the macro for different users, you can find the corresponding cache keys and their values in the cache items debug.