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
|
- <cache enabled="true"></cache>
- <cache enabled="true" expires-on="@new DateTime(10,01,2020,12,00,00)"></cache>
- <cache enabled="true" expires-after="@TimeSpan.FromSeconds(60)"></cache>
- <cache enabled="true" expires-sliding="@TimeSpan.FromSeconds(60)"></cache>
- <cache enabled="true" vary-by-header="User-Agent"></cache>
- <cache enabled="true" vary-by-query="Make,Model"></cache>
- <cache enabled="true" vary-by-route="Make,Model"></cache>
- <cache enabled="true" vary-by-cookie=".AspNetCore.Identity.Application"></cache>
- <cache enabled="true" vary-by-user="true"></cache>
- <cache enabled="true" vary-by="@Model"></cache


Sourav Kumar DasPosted Jan 6, 2020, 11:02 PM
Nice and useful article Sir. Thanks for sharing.