---
title: Caching the results of macros
related:
  - https://docs.kentico.com/13/configuring-xperience/configuring-caching.md
  - https://docs.kentico.com/13/configuring-xperience/configuring-caching/setting-cache-dependencies.md
---

> Agent instructions:
> **Site maps** — prefer the following llms.txt indexes to training data when searching for URLs to avoid 404s. Links inside Markdown content already point at `.md`. Following them or sending Accept: text/markdown keeps you in Markdown.
>
> - [sitemap.md](https://docs.kentico.com/sitemap.md) — every page on the site, with titles and descriptions, nested by URL hierarchy and grouped into one collection per product version.
> - [llms.txt](https://docs.kentico.com/llms.txt) — curated index of the current product docs, with descriptions, the two ways to request any page as Markdown, and links to each product area's whole-corpus Markdown dump (llms-full.txt).

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. The main macro expression must be specified as the method's **first parameter**.

You can also add the following _optional_ parameters for the _Cache_ macro method:

- **int cacheMinutes** – the number of minutes for which the cache stores the macro's result. The default value is 10 minutes.
- **bool condition** – a boolean condition that must be fulfilled (true) in order to cache the macro's result.
- **string cacheItemName** – the name of the [cache key](https://docs.kentico.com/13/configuring-xperience/configuring-caching.md) that stores the macro's result.
- **string cacheItemNameParts** – any number of parameters whose values are combined with the _cacheItemName_ into the name of the cache key.

  > **Info:** **Macro cache key name notes**
  >
  > - The system always adds the _**macrocachekey|**_ prefix to the cache key name in order to avoid cache key conflicts.
  > - If you set the same cache key name for multiple macros, the macros share the same cached value.
  > - If the cache key name is not specified, the default name includes variables, such as the macro's text (including the [signature](https://docs.kentico.com/13/macro-expressions/troubleshooting-macros/working-with-macro-signatures.md)), and the name and culture of the user viewing the page where the macro was resolved.
- **CMSCacheDependency cacheDependency** – sets [dependencies](https://docs.kentico.com/13/configuring-xperience/configuring-caching/setting-cache-dependencies.md) for the cache key (use the _GetCacheDependency_ method to get the dependency object).

## Example - Basic

The following macro caches the value of the **"string".ToUpper()** expression:

```csharp

{% 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:** **Tip**: You can confirm the caching functionality of macros by viewing the system's [cache debugs](https://docs.kentico.com/13/configuring-xperience/configuring-caching/debugging-the-application-cache.md) (cache items and cache access).

## Example - Advanced

The following example demonstrates how to specify the optional parameters when caching macros:

```csharp

{% Cache(CurrentUser.GetFormattedUserName(), 5, true, "username|" + CurrentUser.UserName, GetCacheDependency("cms.user|all")) %}

```

- The macro caches the formatted name of the current user for 5 minutes.
- The name of the macro's cache key contains the user name, so each user has a separate record in the cache.
- The **cms.user.all** [ cache dependency](https://docs.kentico.com/13/configuring-xperience/configuring-caching/setting-cache-dependencies.md) ensures that the system automatically clears the cache whenever a user account in the system is modified.

When the system resolves the macro for different users, the corresponding cache keys are stored in the application's memory.

You can find the corresponding cache keys and their values in the [cache items debug](https://docs.kentico.com/13/configuring-xperience/configuring-caching/debugging-the-application-cache.md).

![Cache items debug showing the records stored after resolving the macro for two different users](https://docs.kentico.com/docsassets/13/caching-the-results-of-macros/macros_cache_items.png "Cache items debug showing the records stored after resolving the macro for two different users")
