Cache And Distributed Cache Tag Helper In ASP.NET Core 3.1

Today we will discuss about 2 tag helpers related to caching in asp.net core: cache tag helper and distributed cache tag helper. Caching is used to improve the performance of application pages. In asp.net MVC core application we have got cache tag helper and distributed cache tag helper. We will understand how to use them in our application step by step.
 
Here is the code
 

Cache tag helper

 
The cache tag helper is used to improve the performance of application in asp.net core which is used for displaying data and other activities. The cache tag has some attributes. To enable cache in application it has Boolean attribute true and false nut default is true.
 
The default time duration is 20 minutes but we can set time date in cache tag helper. To set date time in cache we use DateTimeOffSet attribute.
 

Distributed cache tag helper

 
The distribute tag helper is used to dynamically improve the performance of asp.net core application. Cache tag helper and distributed cache inherit from the same base class. Both the tag helpers have same attribute to use or set it property. The Distributed Cache Tag Helper uses constructor injection. The IDistributedCache interface is passed into the Distributed Cache Tag Helper's constructor.
 
Attribute
Attribute type
Example
Default
Enabled
Boolean
true, false
True
Expires-on
DateTimeOffset
@new DateTime(2020,1,10,12,00,0)
 
Expires-after
TimeSpan
@TimeSpan.FromSeconds(60)
20 minutes
Expires-sliding
TimeSpan
@TimeSpan.FromSeconds(60)
 
vary-by-header
String
User-Agent, User-Agent,content-encoding
 
vary-by-query
String
Make, Make,Model
 
vary-by-route
String
Make, Make,Model
 
vary-by-cookie
String
.AspNetCore.Identity.Application,
.AspNetCore.Identity.Application,HairColor
 
vary-by-user
Boolean
true, false
true
vary-by
String
@Model
 
priority
CacheItemPriority
High, Low, NeverRemove, Normal
Normal
  1. <cache enabled="true"></cache>  
  2. <cache enabled="true" expires-on="@new DateTime(10,01,2020,12,00,00)"></cache>  
  3. <cache enabled="true" expires-after="@TimeSpan.FromSeconds(60)"></cache>  
  4. <cache enabled="true" expires-sliding="@TimeSpan.FromSeconds(60)"></cache>  
  5. <cache enabled="true" vary-by-header="User-Agent"></cache>  
  6. <cache enabled="true" vary-by-query="Make,Model"></cache>  
  7. <cache enabled="true" vary-by-route="Make,Model"></cache>  
  8. <cache enabled="true" vary-by-cookie=".AspNetCore.Identity.Application"></cache>  
  9. <cache enabled="true" vary-by-user="true"></cache>  
  10. <cache enabled="true" vary-by="@Model"></cache  
Step 1
 
Create ASP.NET web application project in visual studio 2019.
 
Step 2
 
Create class employee under Models folder.
  1. using System.ComponentModel.DataAnnotations;  
  2.   
  3. namespace CacheAndDistributedCacheTagHelper__Demo.Models  
  4. {  
  5.     public class Employee  
  6.     {  
  7.         [Key]  
  8.         public int Id { get; set; }  
  9.   
  10.         [Required]  
  11.         [StringLength(100)]  
  12.         public string Name { get; set; }  
  13.   
  14.         [Required]  
  15.         [StringLength(100)]  
  16.         public string Position { get; set; }  
  17.   
  18.         [Required]  
  19.         [StringLength(100)]  
  20.         public string Office { get; set; }  
  21.   
  22.         [Required]  
  23.         public int Age { get; set; }  
  24.   
  25.         [Required]  
  26.         public int Salary { get; set; }  
  27.     }  
  28. }  
Step 3
 
“Add” DbSet in ApplicationDbContext which is located under data folder of your project.
  1. using CacheAndDistributedCacheTagHelper__Demo.Models;  
  2. using Microsoft.AspNetCore.Identity.EntityFrameworkCore;  
  3. using Microsoft.EntityFrameworkCore;  
  4.   
  5. namespace CacheAndDistributedCacheTagHelper__Demo.Data  
  6. {  
  7.     public class ApplicationDbContext : IdentityDbContext  
  8.     {  
  9.         public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)  
  10.             : base(options)  
  11.         {  
  12.         }  
  13.   
  14.         public DbSet<Employee> Employees { get; set; }  
  15.     }  
  16. }  
Step 4
 
Now in package manager console Add-migration InitialModel hit enter then update the database. It will add employee table with all the properties.
 
Step 5
 
Open same HomeController or new controller write following code on controller class.
  1. using CacheAndDistributedCacheTagHelper__Demo.Data;  
  2. using Microsoft.AspNetCore.Mvc;  
  3. using System.Linq;  
  4.   
  5. namespace CacheAndDistributedCacheTagHelper__Demo.Controllers  
  6. {  
  7.     public class HomeController : Controller  
  8.     {  
  9.         private readonly ApplicationDbContext dbContext;  
  10.   
  11.         public HomeController(ApplicationDbContext context)  
  12.         {  
  13.             dbContext = context;  
  14.         }  
  15.   
  16.         public IActionResult Index()  
  17.         {  
  18.             var employee = dbContext.Employees.ToList();  
  19.             return View(employee);  
  20.         }  
  21.     }  
  22. }  
Step 6
 
Similarly use same index view or “Add” new view if you wish to.
  1. @model IEnumerable<CacheAndDistributedCacheTagHelper__Demo.Models.Employee>  
  2.   
  3. @{  
  4.     ViewData["Title"] = "Home Page";  
  5. }  
  6.   
  7. <div class="container py-4">  
  8.     <cache enabled="true">  
  9.         <label class="control-label">Date Time:</label> @DateTime.Now  
  10.         <table class="table table-bordered">  
  11.             <thead>  
  12.                 <tr>  
  13.                     <th>@Html.DisplayNameFor(m => m.Name)</th>  
  14.                     <th>@Html.DisplayNameFor(m => m.Position)</th>  
  15.                     <th>@Html.DisplayNameFor(m => m.Office)</th>  
  16.                     <th>@Html.DisplayNameFor(m => m.Age)</th>  
  17.                     <th>@Html.DisplayNameFor(m => m.Salary)</th>  
  18.                 </tr>  
  19.             </thead>  
  20.             <tbody>  
  21.                 @foreach (var item in Model)  
  22.                 {  
  23.                     <tr>  
  24.                         <td>@item.Name</td>  
  25.                         <td>@item.Position</td>  
  26.                         <td>@item.Office</td>  
  27.                         <td>@item.Age</td>  
  28.                         <td>@item.Salary</td>  
  29.                     </tr>  
  30.                 }  
  31.             </tbody>  
  32.         </table>  
  33.     </cache>  
  34. </div>  
Cache And Distributed Cache Tag Helper In ASP.NET Core 3.1

Summary

 
In this article, I have covered cache and distributed cache tag helper. It will be helpful for beginners and experienced level developers or programmers to understand what cache and distributed cache tag means and how to use them.