Cache Tag Helper in ASP.NET Core MVC

We can cache particular sections of our Razor views using the Cache Tag Helpers in ASP.NET Core MVC.

Cache Tag Helper in ASP.NET Core MVC
Cache, Cache Tag Helper, Asp.Net Core, Programming, thetechfoyer, thetechfoyer.com

This Articles helps you to understand the concept of Model binding in ASP.NET Core Razor Views

This helps us in improving our performance, by reducing the further requirement to regenerate the same content on each request.


Attributes of the Cache Tag Helper:

  • expires-after: How long should the content be kept in the cache?
  • expires-on: a content's absolute expiration time.
  • expires-sliding: A sliding expiration duration.
  • vary-by: a list of strings to vary the cache by, separated by commas. beneficial for caching content that changes according on specific parameters (such as query string values, user agent, etc.).
  • vary-by-user: Caches content by user. if set to true, different users will have their own cached versions.
  • vary-by-cookie: Do Caches on the basis of specified cookie name(s).
  • vary-by-header: Do Caches on the basis of specified request header name(s).
  • vary-by-query: Do Caches on the basis of specified query string parameter(s).



Register Custom Tag Helper & Custom Tag Helper in _ViewImports.cshtml

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper *, WebApplication3
@addTagHelper *, WebApplication3.TagHelpers



Cache specific Portion of your view
To indicate the part of the view you wish to cache, use the  tag helper.



Cache profiles:
Use cache profiles in Startup.cs (or Program.cs in.NET 6 and later) if you have several cache tags with similar settings.

By default, cached content is kept in memory. Distributed caching may be something to think about if your application restarts or operates in a load-balanced or web farm environment. Caching needs to be done correctly. Content that is not updated frequently or is largely static should be cached. Users may receive outdated data if content is over-cleaned or frequently changes in the cache.


Cache Tag Helpers Examples

<!-- Until the cache expires (by default, in 20 minutes) or the cached date is removed,
additional requests display the cached value. -->
<cache>
  <p>This Content is Cached for 20 Min </p>
  Current Time: @DateTime.Now
</cache>


<!-- To Enable Cache set to true (By Default it is true.) -->
<cache enabled="true">
   <p>This Content is Cached for 20 Min </p>
    Current Time: @DateTime.Now
</cache>


<!-- Sets an absolute expiration date, this will cached our content until 04:10 AM on November 22, 2050 -->
<cache expires-on="@new DateTime(2050,11,22,04,10,0)">
  <p>This Content is Cached until 04:10 AM on November 22, 2050 </p>
  Current Time: @DateTime.Now
</cache>


<!-- caches a portion of the view for 15 minutes. -->
<cache expires-after="@TimeSpan.FromMinutes(15)">
  <p>This Content is Cached for 15 minutes </p>
  Current Time: @DateTime.Now
</cache>


<!-- The expiry will be renewed on each access, if access/refreshed with in 10 min -->
<cache expires-sliding="@TimeSpan.FromMinutes(10)">
  <p>This Content is Cached for 10 Min, cache time revised for next 10 Min if refreshed </p>
  Current Time: @DateTime.Now
</cache>


<!-- Takes a list of header values separated by commas, which, when changed, causes a cache refresh.  -->
<cache vary-by-header="User-Agent">
  <p>Different Content cache is generated for each User Agent </p>
   Current Time: @DateTime.Now
</cache>


<!-- Accepts a list of keys separated by commas from query string. This will refresh the cache if there is any change in value of listed keys -->
<cache vary-by-query="Name,Id">
  <p>This Content is Cached until there is any change in name or id in querystring </p>
  Current Time: @DateTime.Now
</cache>


<!-- Accepts a list of cookie names separated by commas, which causes a cache refresh when the cookie values change. -->
<cache vary-by-cookie=".AspNetCore.Identity.Application">
  <p>This Content is Cached until cookie value changed </p>
  Current Time: @DateTime.Now
</cache>


<!-- Any change in the signed-in user's (or Context Principal's) identity causes the cache to reset.
In a Razor view use @User.Identity.Name, to refer the current user as the Request Context Principal. -->
<cache vary-by-user="true">
  <p>This Content is Cached until Current user changed </p>
  Current Time: @DateTime.Now
</cache>


<!-- The Cache content is updated when the object that the attribute's string value references changes. -->
<cache vary-by="@Model">
  <p>This Content is Cached until Model or Model values changed </p>
  Current Time: @DateTime.Now
</cache>


<!-- Content should be cached based on a particular query string value. Different cached content will be kept for various category query string values. -->
<cache vary-by="@Context.Request.Query["group"]">
  Showing articles for group: @Context.Request.Query["group"]
  <br/>
  Current Time: @DateTime.Now
</cache>