Cache Tag Helper in ASP.Net Core MVC

Caching is a very interesting topic in any programming language. It provides drastic changes in app performance. A cache tag is provided to implement caching in the ASP.NET Core MVC.

This became possible with the tag helper features of ASP.NET Core Mvc.

What is a Cache Tag Helper?

Cache Tag Helper provides caching to the content defined within the tag. Razor syntax to define cache tag helper.

    {Data}

Cache tag helper to cached current date and time

    @DateTime.Now

Cache Tag Helper Attributes

Below are the attributes of Cache Tag Helper

  • enabled: By default value of this attribute is true. If you want, you can set it to false.
    <cache enabled="true">
    Current Date and Time: @DateTime.Now
    </cache>
    
    
  • expires-on: This attribute in Cache Tag Helper sets an expiration date for the cached data.
     <cache expires-on="@new DateTime(2020,7,25,18,06,0)">
     Date and Time Inside Cache Tag Helper: @DateTime.Now
     </cache>
    
    
  • expires-after: Sets the time duration of the cache from the time of the very first request. The default expires after the value is twenty minutes.
    <cache expires-after="@TimeSpan.FromSeconds(60)">
    Date and Time Inside Cache Tag Helper: @DateTime.Now
    </cache>
  • expires-sliding: It resets the cache expiry time as soon as a new request is raised.
    <cache expires-sliding="@TimeSpan.FromSeconds(120)">
    Date and Time Inside Cache Tag Helper: @DateTime.Now
    </cache>
    
    
  • vary-by-header: It accepts the header value and resets the cache when the value is changed.
      <cache vary-by-header="User-Agent">
      Date and Time Inside Cache Tag Helper: @DateTime.Now
      </cache>
    
    
  • vary-by-query: Accepts the cache from a query string. It refreshes the cache when a value is changed. It can accept one or more than one query strings.
     <cache vary-by-query="Country,City">
     Date and Time Inside Cache Tag Helper: @DateTime.Now
     </cache>
    
    
  • vary-by-route: It accepts a single parameter name or multiple parameter names. It refreshes the cached data when a parameter value from route data is changed.
    routes.MapRoute(
        name: "default",
        template: "{controller=Home}/{action=Index}/{Country?}/{City?}");
            
    
     <cache vary-by-route="Country,City">
     Date and Time Inside Cache Tag Helper: @DateTime.Now
     </cache>
    
    
  • vary-by-cookie: This attribute in Cache Tag Helper accepts a list of cookie names and refreshes the cached data when the value of cookies gets changed.
     <cache vary-by-cookie=".AspNetCore.Identity.Application">
     Date and Time Inside Cache Tag Helper: @DateTime.Now
     </cache>
    
    
  • vary-by-user: This attribute in Cache Tag Helper is based on the logged-in user. As soon as logged in user changes then it refreshes the cache data.
     <cache vary-by-user="true">
     Date and Time Inside Cache Tag Helper: @DateTime.Now
     </cache>
    
    

Hope you liked this blog of Cache Tag Helper. Share this blog in your community and if there is any feedback, please write in the comment box below.

Leave a Comment

RSS
YouTube
YouTube
Instagram