-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Tag Helpers: Create a 'cache' Tag Helper #1552
Description
A new Tag Helper that utilizes the conditional content mode (aspnet/Razor#221) to cache the output of its contents via ICache. Supports varied results via attributes. In the case of a cache hit, the contents would not even be executed.
Usage
<cache [vary-by=""]
[vary-by-header=""]
[vary-by-query=""]
[vary-by-route=""]
[vary-by-cookie=""]
[vary-by-user=""]
[expires-on=""]
[expires-after=""]
[expires-sliding=""]
[priority=""]>Arguments
| Attribute Name | Type | Details |
|---|---|---|
| vary-by | string |
A string to vary the result by. The value is used as part of the cache entry key |
| vary-by-header | string |
A header name to vary the cached result by. The header value is used as part of the cache entry key. |
| vary-by-query | string |
A comma-separated list of query string parameter names that the result should be varied by. The query string parameter values are used as part of the cache entry key. |
| vary-by-route | string |
A comma-separated list of route data parameter names that the result should be varied by. The route data parameter values are used as part of the cache entry key. |
| vary-by-cookie | string |
A comma-separated list of cookie names that the result should be varied by. The cookie values are used as part of the cache entry key. |
| vary-by-user | bool |
A boolean indicating whether the result should be varied by user. The user name will be used as part of the cache entry key. |
| expires-on | DateTime |
The exact date/time the cache entry should be evicted. |
| expires-after | TimeSpan |
The time from right now that the cache entry should be evicted. |
| expires-sliding | TimeSpan |
The amount of time from last access that the cache entry should be evicted. |
| priority | CachePreservationPriority |
Specifies how items are prioritized for preservation during a memory pressure triggered cleanup. |
Cache Entry Key Generation
The cache entry key will be generated by combining a number of elements, each separated by a hyphen -:
- the prefix "CacheTagHelper-"
- the
TagHelperContext.UniqueId - the values from the various vary-by-* attributes specified. Each one must be prefixed with a constant to avoid key/value clashes and include both the key and value from the underlying store (if applicable) with the format
prefix([sourceKey1]=[sourceValue2],[sourceKey2]=[sourceValue3]). Attributes not specified should be omitted from the key.
Examples:
Url: /resource?page=2, HTML: <cache vary-by-param="page">Page @page results...</cache>, Key: CacheTagHelper-34980348710984387343556-param(page=2)
Url: /resource?page=2&category=45, HTML: <cache vary-by-param="page,category">Page @page results...</cache>, Key: CacheTagHelper-34980348710984387343556-param(page=2,category=45)
Header: My-Header=Hello, HTML: <cache vary-by-header="My-Header">Some content</cache>, Key: CacheTagHelper-34980348710984387343556-header(My-Header=Hello)
Url: /resource?page=2&category=45, Header: My-Header=Hello, HTML: <cache vary-by-param="page,category" vary-by-header="My-Header">Some content</cache>, Key: CacheTagHelper-34980348710984387343556-param(page=2,category=45)-header(My-Header=Hello)
Example
<cache vary-by-header="User-Agent">
<div>
Your browser is @Request.Headers["User-Agent"]
</div>
@(Component.Invoke<MyViewComponent>(1, "test"))
</cache>