Cache Tag Helper In ASP.NET Core MVC

Introduction 
 
Caching tag helper can improve the performance and scalability of the application dramatically and it caches the data into the internal ASP.NET Core cache provider.
 
The tag helper cache is used to cache the data.
  1. <cache>  
  2.   
  3. </cache>  
The default cache expires in 20 minutes, so the first request of page contains the current datetime but the subsequent requests show the old time due to cache. Once caching expires, it will show a new time.
 
Cache Tag Helper Attributes
 
enabled
 
It determines whether the content enclosed by the tag helper is cached. It is boolean type. Its default value is set to true. If it is set to false, the tag helper will not cache the data.
 
Example
  1. <cache enabled="true">  
  2.     Current Date Time: @DateTime.Now  
  3. </cache>  
expires-on
 
It sets an absolute expiration date time for the cache. It is DateTime type. In the following example, the content will be cached until 6:24 PM on January 24, 2020.
 
Example
  1. <cache expires-on="@new DateTime(2020,1,24,18,24,0)">  
  2.     Current Date Time: @DateTime.Now  
  3. </cache>  
expires-after
 
It sets the time span from the first request time to cache the content. It is TimeSpan type. In the following example, content will be cached for 150 seconds from the first request.
  1. <cache expires-after="@TimeSpan.FromSeconds(150)">  
  2.     Current Date Time: @DateTime.Now  
  3. </cache>  
expires-sliding
 
It is very similar to expires-after but the expiry will be renewed after every access. It is TimeSpan type.
 
Example
  1. <cache expires-sliding="@TimeSpan.FromSeconds(150)">  
  2.     Current Date Time: @DateTime.Now  
  3. </cache>  
vary-by-header
 
It accepts single/multiple header value (comma separated values) and cache refresh is triggered when this value is changed. In the following example, it monitors the header value User-Agent and it refreshes for every different User-Agent. It is string type.
 
Example
  1. <cache vary-by-header="User-Agent">  
  2.     Current Date Time: @DateTime.Now  
  3. </cache>  
vary-by-query
 
It accepts single / multiple header value (comma separated) and cache refresh is triggered when this value is changed. In the following example, it looks at the values of Make and Model. It is string type.
 
Example
  1. <cache vary-by-query="Make,Model">  
  2.     Current Date Time: @DateTime.Now  
  3. </cache>  
vary-by-route
 
It accepts single / multiple header value (comma separated) and cache refresh is triggered when the route data parameter value is changed. In the following example, it looks at the values of id and name in the route. It is string type.
 
Example
  1. routes.MapRoute(  
  2.     name: "default",  
  3.     template: "{controller=Home}/{action=Index}/{id?}/{name?}");  
  1. <cache vary-by-route="id,name">  
  2.     Current Date Time: @DateTime.Now  
  3. </cache>  
vary-by-cookie
 
It accepts single / multiple header value (comma separated) value and cache refresh is triggered when this value is changed. It is string type. In the following example, it looks at the cookie associated with ASP.NET Identity and cookie is changed cache gets refreshed.
  1. <cache vary-by-cookie=".AspNetCore.Identity.Application">  
  2.     Current Date Time: @DateTime.Now  
  3. </cache>  
vary-by-user
 
It specifies whether the cache should reset or not when a logged-in user or Principal context change. It is boolean type. The default value is false. When this attribute value is set to true, the cache will maintain through a log-in and log-out cycle and these actions invalidate the cache for authenticated user. For anonymous state, cache is maintained when no cookie is present. So, the cache will be maintained even if no user is logged-in.
 
Example
  1. <cache vary-by-user="true">  
  2.     Current Date Time: @DateTime.Now  
  3. </cache>  
vary-by
 
It allows for customization of the data caching. It is string type. When the reference assigned to this attribute value changes, the cache gets updated. Any change made in the referenced value invalidates the cache.
 
In the following example, cache gets invalid when any changes are made to the model.

Example
  1. <cache vary-by="@Model"">  
  2.     Current Date Time: @DateTime.Now  
  3. </cache>  
priority
 
It provides the cache removal guidance to the built-in cache provider. It will remove the cache entry first for low priority under memory pressure. The expected values are High, Low, NeverRemove and Normal. It does not guarantee a specific level of cache retention.
 
Example
  1. <cache priority="High">  
  2.     Current Date Time: @DateTime.Now  
  3. </cache>  
Summary

Caching tag helper can improve the performance and it caches the data into the internal ASP.NET Core cache provider. It means that the Cache Tag Helper is dependent on the memory cache service. It gets added to the service if it has not been added.

Up Next
    Ebook Download
    View all
    Learn
    View all