Skip to content

Commit c80b69d

Browse files
authored
Revise Cache Tag Helper documentation attributes (#36142)
1 parent a1440ae commit c80b69d

1 file changed

Lines changed: 108 additions & 92 deletions

File tree

aspnetcore/mvc/views/tag-helpers/built-in/cache-tag-helper.md

Lines changed: 108 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ author: pkellner
44
description: Learn how to use the Cache Tag Helper.
55
ms.author: tdykstra
66
ms.custom: mvc
7-
ms.date: 10/10/2018
7+
ms.date: 09/22/2025
88
uid: mvc/views/tag-helpers/builtin-th/cache-tag-helper
99
---
1010
# Cache Tag Helper in ASP.NET Core MVC
@@ -25,10 +25,26 @@ The first request to the page that contains the Tag Helper displays the current
2525

2626
## Cache Tag Helper Attributes
2727

28-
### enabled
28+
### `expires-after`
2929

30-
| Attribute Type | Examples | Default |
31-
| --------------- | --------------- | ------- |
30+
| Attribute type | Example | Default |
31+
| -------------- | ---------------------------- | :-----: |
32+
| `TimeSpan` | `@TimeSpan.FromSeconds(120)` | — |
33+
34+
`expires-after` sets the length of time from the first request time to cache the contents.
35+
36+
Example:
37+
38+
```cshtml
39+
<cache expires-after="@TimeSpan.FromSeconds(120)">
40+
Current Time Inside Cache Tag Helper: @DateTime.Now
41+
</cache>
42+
```
43+
44+
### `enabled`
45+
46+
| Attribute type | Examples | Default |
47+
| --------------- | --------------- | :-----: |
3248
| Boolean | `true`, `false` | `true` |
3349

3450
`enabled` determines if the content enclosed by the Cache Tag Helper is cached. The default is `true`. If set to `false`, the rendered output is **not** cached.
@@ -41,9 +57,9 @@ Example:
4157
</cache>
4258
```
4359

44-
### expires-on
60+
### `expires-on`
4561

46-
| Attribute Type | Example |
62+
| Attribute type | Example |
4763
| ---------------- | ---------------------------------- |
4864
| `DateTimeOffset` | `@new DateTime(2025,1,29,17,02,0)` |
4965

@@ -57,182 +73,182 @@ The following example caches the contents of the Cache Tag Helper until 5:02 PM
5773
</cache>
5874
```
5975

60-
### expires-after
76+
### `expires-sliding`
6177

62-
| Attribute Type | Example | Default |
63-
| -------------- | ---------------------------- | ---------- |
64-
| `TimeSpan` | `@TimeSpan.FromSeconds(120)` | 20 minutes |
78+
| Attribute type | Example |
79+
| -------------- | --------------------------- |
80+
| `TimeSpan` | `@TimeSpan.FromSeconds(60)` |
6581

66-
`expires-after` sets the length of time from the first request time to cache the contents.
82+
Sets the time that a cache entry should be evicted if its value hasn't been accessed.
6783

6884
Example:
6985

7086
```cshtml
71-
<cache expires-after="@TimeSpan.FromSeconds(120)">
87+
<cache expires-sliding="@TimeSpan.FromSeconds(60)">
7288
Current Time Inside Cache Tag Helper: @DateTime.Now
7389
</cache>
7490
```
7591

76-
The Razor View Engine sets the default `expires-after` value to twenty minutes.
92+
Defaults to 30 seconds if [`expires-after`](#expires-after) and [`expires-on`](#expires-on) aren't defined.
7793

78-
### expires-sliding
94+
### `priority`
7995

80-
| Attribute Type | Example |
81-
| -------------- | --------------------------- |
82-
| `TimeSpan` | `@TimeSpan.FromSeconds(60)` |
96+
| Attribute type | Examples | Default |
97+
| ------------------- | -------------------------------------- | :------: |
98+
| `CacheItemPriority` | `High`, `Low`, `NeverRemove`, `Normal` | `Normal` |
8399

84-
Sets the time that a cache entry should be evicted if its value hasn't been accessed.
100+
`priority` provides cache eviction guidance to the built-in cache provider. The web server evicts `Low` cache entries first when it's under memory pressure.
85101

86102
Example:
87103

88104
```cshtml
89-
<cache expires-sliding="@TimeSpan.FromSeconds(60)">
105+
<cache priority="High">
90106
Current Time Inside Cache Tag Helper: @DateTime.Now
91107
</cache>
92108
```
93109

94-
### vary-by-header
110+
The `priority` attribute doesn't guarantee a specific level of cache retention. `CacheItemPriority` is only a suggestion. Setting this attribute to `NeverRemove` doesn't guarantee that cached items are always retained. See the topics in the [Additional Resources](#additional-resources) section for more information.
95111

96-
| Attribute Type | Examples |
97-
| -------------- | ------------------------------------------- |
98-
| String | `User-Agent`, `User-Agent,content-encoding` |
112+
The Cache Tag Helper is dependent on the [memory cache service](xref:performance/caching/memory). The Cache Tag Helper adds the service if it hasn't been added.
99113

100-
`vary-by-header` accepts a comma-delimited list of header values that trigger a cache refresh when they change.
114+
### `vary-by`
101115

102-
The following example monitors the header value `User-Agent`. The example caches the content for every different `User-Agent` presented to the web server:
116+
| Attribute type | Example |
117+
| -------------- | -------- |
118+
| String | `@Model` |
119+
120+
`vary-by` allows for customization of what data is cached. When the object referenced by the attribute's string value changes, the content of the Cache Tag Helper is updated. Often, a string-concatenation of model values are assigned to this attribute. Effectively, this results in a scenario where an update to any of the concatenated values invalidates the cache.
121+
122+
The following example assumes the controller method rendering the view sums the integer value of the two route parameters, `myParam1` and `myParam2`, and returns the sum as the single model property. When this sum changes, the content of the Cache Tag Helper is rendered and cached again.
123+
124+
Action:
125+
126+
```csharp
127+
public IActionResult Index(string myParam1, string myParam2, string myParam3)
128+
{
129+
int num1;
130+
int num2;
131+
int.TryParse(myParam1, out num1);
132+
int.TryParse(myParam2, out num2);
133+
return View(viewName, num1 + num2);
134+
}
135+
```
136+
137+
`Index.cshtml`:
103138

104139
```cshtml
105-
<cache vary-by-header="User-Agent">
140+
<cache vary-by="@Model">
106141
Current Time Inside Cache Tag Helper: @DateTime.Now
107142
</cache>
108143
```
109144

110-
### vary-by-query
145+
### `vary-by-cookie`
111146

112-
| Attribute Type | Examples |
113-
| -------------- | -------------------- |
114-
| String | `Make`, `Make,Model` |
147+
| Attribute type | Examples |
148+
| -------------- | -------------------------------------------------------------------------------- |
149+
| String | `.AspNetCore.Identity.Application`, `.AspNetCore.Identity.Application,HairColor` |
115150

116-
`vary-by-query` accepts a comma-delimited list of <xref:Microsoft.AspNetCore.Http.IQueryCollection.Keys*> in a query string (<xref:Microsoft.AspNetCore.Http.HttpRequest.Query*>) that trigger a cache refresh when the value of any listed key changes.
151+
`vary-by-cookie` accepts a comma-delimited list of cookie names that trigger a cache refresh when the cookie values change.
117152

118-
The following example monitors the values of `Make` and `Model`. The example caches the content for every different `Make` and `Model` presented to the web server:
153+
The following example monitors the cookie associated with ASP.NET Core Identity. When a user is authenticated, a change in the Identity cookie triggers a cache refresh:
119154

120155
```cshtml
121-
<cache vary-by-query="Make,Model">
156+
<cache vary-by-cookie=".AspNetCore.Identity.Application">
122157
Current Time Inside Cache Tag Helper: @DateTime.Now
123158
</cache>
124159
```
160+
161+
### `vary-by-culture`
125162

126-
### vary-by-route
127-
128-
| Attribute Type | Examples |
129-
| -------------- | -------------------- |
130-
| String | `Make`, `Make,Model` |
163+
| Attribute type | Examples | Default |
164+
| -------------- | --------------- | :-----: |
165+
| Boolean | `true`, `false` | `false` |
131166

132-
`vary-by-route` accepts a comma-delimited list of route parameter names that trigger a cache refresh when the route data parameter value changes.
167+
`vary-by-culture` varys the cached result by request culture. Setting the attribute to `true` means the result is varied by <xref:System.Globalization.CultureInfo.CurrentCulture%2A?displayProperty=nameWithType> and <xref:System.Globalization.CultureInfo.CurrentUICulture%2A?displayProperty=nameWithType>.
133168

134169
Example:
135170

136-
`Startup.cs`:
137-
138-
```csharp
139-
routes.MapRoute(
140-
name: "default",
141-
template: "{controller=Home}/{action=Index}/{Make?}/{Model?}");
142-
```
143-
144-
`Index.cshtml`:
145-
146171
```cshtml
147-
<cache vary-by-route="Make,Model">
172+
<cache vary-by-culture="true">
148173
Current Time Inside Cache Tag Helper: @DateTime.Now
149174
</cache>
150175
```
151176

152-
### vary-by-cookie
177+
### `vary-by-header`
153178

154-
| Attribute Type | Examples |
155-
| -------------- | -------------------------------------------------------------------------------- |
156-
| String | `.AspNetCore.Identity.Application`, `.AspNetCore.Identity.Application,HairColor` |
179+
| Attribute type | Examples |
180+
| -------------- | ------------------------------------------- |
181+
| String | `User-Agent`, `User-Agent,content-encoding` |
157182

158-
`vary-by-cookie` accepts a comma-delimited list of cookie names that trigger a cache refresh when the cookie values change.
183+
`vary-by-header` accepts a comma-delimited list of header values that trigger a cache refresh when they change.
159184

160-
The following example monitors the cookie associated with ASP.NET Core Identity. When a user is authenticated, a change in the Identity cookie triggers a cache refresh:
185+
The following example monitors the header value `User-Agent`. The example caches the content for every different `User-Agent` presented to the web server:
161186

162187
```cshtml
163-
<cache vary-by-cookie=".AspNetCore.Identity.Application">
188+
<cache vary-by-header="User-Agent">
164189
Current Time Inside Cache Tag Helper: @DateTime.Now
165190
</cache>
166191
```
167192

168-
### vary-by-user
193+
### `vary-by-query`
169194

170-
| Attribute Type | Examples | Default |
171-
| --------------- | --------------- | ------- |
172-
| Boolean | `true`, `false` | `true` |
195+
| Attribute type | Examples |
196+
| -------------- | -------------------- |
197+
| String | `Make`, `Make,Model` |
173198

174-
`vary-by-user` specifies whether or not the cache resets when the signed-in user (or Context Principal) changes. The current user is also known as the Request Context Principal and can be viewed in a Razor view by referencing `@User.Identity.Name`.
199+
`vary-by-query` accepts a comma-delimited list of <xref:Microsoft.AspNetCore.Http.IQueryCollection.Keys*> in a query string (<xref:Microsoft.AspNetCore.Http.HttpRequest.Query*>) that trigger a cache refresh when the value of any listed key changes.
175200

176-
The following example monitors the current logged in user to trigger a cache refresh:
201+
The following example monitors the values of `Make` and `Model`. The example caches the content for every different `Make` and `Model` presented to the web server:
177202

178203
```cshtml
179-
<cache vary-by-user="true">
204+
<cache vary-by-query="Make,Model">
180205
Current Time Inside Cache Tag Helper: @DateTime.Now
181206
</cache>
182207
```
183208

184-
Using this attribute maintains the contents in cache through a sign-in and sign-out cycle. When the value is set to `true`, an authentication cycle invalidates the cache for the authenticated user. The cache is invalidated because a new unique cookie value is generated when a user is authenticated. Cache is maintained for the anonymous state when no cookie is present or the cookie has expired. If the user is **not** authenticated, the cache is maintained.
185-
186-
### vary-by
209+
### `vary-by-route`
187210

188-
| Attribute Type | Example |
189-
| -------------- | -------- |
190-
| String | `@Model` |
211+
| Attribute type | Examples |
212+
| -------------- | -------------------- |
213+
| String | `Make`, `Make,Model` |
191214

192-
`vary-by` allows for customization of what data is cached. When the object referenced by the attribute's string value changes, the content of the Cache Tag Helper is updated. Often, a string-concatenation of model values are assigned to this attribute. Effectively, this results in a scenario where an update to any of the concatenated values invalidates the cache.
215+
`vary-by-route` accepts a comma-delimited list of route parameter names that trigger a cache refresh when the route data parameter value changes.
193216

194-
The following example assumes the controller method rendering the view sums the integer value of the two route parameters, `myParam1` and `myParam2`, and returns the sum as the single model property. When this sum changes, the content of the Cache Tag Helper is rendered and cached again.
217+
Example:
195218

196-
Action:
219+
`Startup.cs`:
197220

198221
```csharp
199-
public IActionResult Index(string myParam1, string myParam2, string myParam3)
200-
{
201-
int num1;
202-
int num2;
203-
int.TryParse(myParam1, out num1);
204-
int.TryParse(myParam2, out num2);
205-
return View(viewName, num1 + num2);
206-
}
222+
routes.MapRoute(
223+
name: "default",
224+
template: "{controller=Home}/{action=Index}/{Make?}/{Model?}");
207225
```
208226

209227
`Index.cshtml`:
210228

211229
```cshtml
212-
<cache vary-by="@Model">
230+
<cache vary-by-route="Make,Model">
213231
Current Time Inside Cache Tag Helper: @DateTime.Now
214232
</cache>
215233
```
216234

217-
### priority
235+
### `vary-by-user`
218236

219-
| Attribute Type | Examples | Default |
220-
| ------------------- | -------------------------------------- | -------- |
221-
| `CacheItemPriority` | `High`, `Low`, `NeverRemove`, `Normal` | `Normal` |
237+
| Attribute type | Examples | Default |
238+
| --------------- | --------------- | :-----: |
239+
| Boolean | `true`, `false` | `false` |
222240

223-
`priority` provides cache eviction guidance to the built-in cache provider. The web server evicts `Low` cache entries first when it's under memory pressure.
241+
`vary-by-user` specifies whether or not the cache resets when the signed-in user (or Context Principal) changes. The current user is also known as the Request Context Principal and can be viewed in a Razor view by referencing `@User.Identity.Name`.
224242

225-
Example:
243+
The following example monitors the current logged in user to trigger a cache refresh:
226244

227245
```cshtml
228-
<cache priority="High">
246+
<cache vary-by-user="true">
229247
Current Time Inside Cache Tag Helper: @DateTime.Now
230248
</cache>
231249
```
232250

233-
The `priority` attribute doesn't guarantee a specific level of cache retention. `CacheItemPriority` is only a suggestion. Setting this attribute to `NeverRemove` doesn't guarantee that cached items are always retained. See the topics in the [Additional Resources](#additional-resources) section for more information.
234-
235-
The Cache Tag Helper is dependent on the [memory cache service](xref:performance/caching/memory). The Cache Tag Helper adds the service if it hasn't been added.
251+
Using this attribute maintains the contents in cache through a sign-in and sign-out cycle. When the value is `true`, an authentication cycle invalidates the cache for the authenticated user. The cache is invalidated because a new unique cookie value is generated when a user is authenticated. Cache is maintained for the anonymous state when no cookie is present or the cookie has expired. If the user is **not** authenticated, the cache is maintained.
236252

237253
## Additional resources
238254

0 commit comments

Comments
 (0)