Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Tag Helpers: Create a 'cache' Tag Helper #1552

Closed
DamianEdwards opened this issue Nov 13, 2014 · 4 comments
Closed

Tag Helpers: Create a 'cache' Tag Helper #1552

DamianEdwards opened this issue Nov 13, 2014 · 4 comments

Comments

@DamianEdwards
Copy link
Member

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>
@DamianEdwards
Copy link
Member Author

A thought: this Tag Helper (along with #1553) doesn't actually have any dependency on MVC. It depends on more than Razor however (ICache for this one, IHostingEnvironment for #1553) so I'm not sure another logical location exists. Open to suggestions.

@yishaigalatzer
Copy link
Contributor

We need to review the design for this prior to commencing coding with @loudej @davidfowl

@yishaigalatzer
Copy link
Contributor

We are going to coordinate the design for this with #1544.

The main questions we have at the moment are

  1. What gets cached (do we cache the string collection in the cache writer, or put it all in a large buffer).
  2. How does it work with Flush (one of the current thoughts in flush was to break out individual buffers).

@yishaigalatzer yishaigalatzer modified the milestones: 6.0.0-rc1, 6.0.0-beta2 Dec 9, 2014
@DamianEdwards DamianEdwards changed the title Create a 'cache' Tag Helper Tag Helpers: Create a 'cache' Tag Helper Dec 11, 2014
pranavkm added a commit that referenced this issue Jan 7, 2015
pranavkm added a commit that referenced this issue Jan 7, 2015
pranavkm added a commit that referenced this issue Jan 7, 2015
pranavkm added a commit that referenced this issue Jan 13, 2015
@pranavkm
Copy link
Contributor

Follow up issue to support Flush inside the tag helper - #1819

Rick-Anderson pushed a commit to dotnet/AspNetCore.Docs that referenced this issue Jul 8, 2017
* Put in more reasonable cache times for examples.

* Added to ImageTagHelper.md a block on the top and guessed at updating assetid by incrementing 1.  Renamed all Additional Resources to Resources per @Devguard, fixed incorrect expires-sliding code example, removed warning about Cache NeverRemove.

* fixed blank lines below toc entry, made consistent "Additional resources", put back in warning about NeverRemove based on feedback from team: #2698 (comment)

* clean up

* created Distributed and Environment Tag Helper Doc Pages and worked on environment.

* Reworked sections from @Rick-Anderson previous commit and removed review comments.

* Updated Anchor Tag Helper's definition of asp-action and asp-controller to be more explicit about defaults as well as say "href" instead of "final URL".

* I realized that asp-all-route-data also behaves like asp-route-* with routing parameters so I updated the doc to reflect that.

* Updated var-by-route definition following comments from Damien in old github post aspnet/Mvc#1552

* Cleanup of Tag Helper Docs

* changed author to pkellner from Peter Kellner per @mairaw

* Update AnchorTagHelper.md

* Update CacheTagHelper.md

* Minor tweaks
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants