ASP.NET Core by Lucas

The Cache Tag Helper in ASP.NET Core

The Cache Tag Helper provides the ability to improve the performance of your ASP.NET Core app by caching its content to the internal ASP.NET Core cache provider.

The cache tag helper enables you to cache regions of a Razor page in memory on the server. Typical uses for this helper are for View Components or partial views that require relatively expensive database or web service calls, but where the content doesn't change very often. This type of caching its primarily used to improve a website's performance.

Unlike most other tag helpers, the Cache tag helper doesn't target a standard HTML element. It targets the <cache> tag, which is not rendered to the browser and doesn't appear in the HTML source either.

...see more

The following Razor markup caches the current date:

<cache>@DateTime.Now</cache>

The first request to the page that contains the Tag Helper displays the current date. Additional requests show the cached value until the cache expires (default 20 minutes) or until the cached date is evicted from the cache.

...see more
  • enabled - Used to specify if this tag is enabled or not
  • expires-after - Specifies the period of time after which the cached item should expire
  • expires-on - Specifies the time at which the cached entry should expire
  • expires-sliding - The period of time after the last access that the item should expire
  • priority - Specifies the CacheItemPriority value of the cached item
  • vary-by - Used to specify the parameters that determine whether different versions of the same content should be cached

Comments