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 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 cache from the time of very first request. The default expires-after value is twenty minutes.
    <cache expires-after="@TimeSpan.FromSeconds(60)">
    Date and Time Inside Cache Tag Helper: @DateTime.Now
    </cache>
  • expires-sliding : It reset 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 header value and reset 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 refresh the cache when a value is changed. It can accepts one or more than one query string.
     <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 name. It refresh the cached data when a parameter value from a 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 Cookies name and refresh 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 logged in user. As soon as logged in user change 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