Caching specific areas using CacheTagHelper
You can also cache specific areas of the view. In a scenario where you are not able to cache an entire view, you would be able to just cache a specific area by surrounding it with the CacheTagHelper
.
To test that, add the following snippet to the index.cshtml
, that you can find in the /Views/Home/
folder:
<div> <p> The current time is: @DateTime.Now.ToLongTimeString() </p> </div> <cache expires-sliding="@TimeSpan.FromSeconds(7)"> <div> <p> The current time is: @DateTime.Now.ToLongTimeString() </p> </div> </cache>
This snippet contains two identical p-tags that write out the current time.
The second one is wrapped in a CacheTagHelper
that has a sliding expiration of 7 seconds defined.