Caching in ASP.NET Core using tag helpers

facebooktwittergoogle_plusredditpinterestlinkedinmail

ASP.NET Core caching

Previous versions of ASP.NET had an output caching mechanism to improve the performance but in ASP.NET core 1.0 the output cache no longer exists. However , we have a brand new caching in ASP.NET Core using tag helpers. If you are not familiar with tag helper please go through introduction-to-tag-helpers.

Implement caching in ASP.NET Core using tag helpers

The implementation is pretty simple. Just wrap the content inside < cache > tag helper and the content will be automatically added in the MemoryCache . Every time when processing the request, the tag helper will check the content in the Memory and if it is exist, then return the content from the cache else it will process and return the content, at the same time it will store the content in the memory .

Consider the following example. Here the web site header coming from a CDN (Content delivery network), actually it is a service call. Here this header content will be cached and it will avoid the service call each time.

          <cache>
            @CDN.header("dotnetreference.com")
        </cache>

How to set Cache Expiry in cache tag helpers ?

We have 3 options to set the cache expiry expires-after, expires-on and expires-sliding.

Caching in Asp.net Core - types of cache expiry

expires-after


The expires-after attribute expects a TimeSpan value. This attribute is used to expire the cache after a specific time.

<cache expires-after="@TimeSpan.FromHours(1)" >
            @CDN.header("dotnetreference.com")
 </cache>

In the preceding code the cache will be expired after 1 hour.

expires-on


The expires-on attribute expects a specific time. This attribute is used to expire the cache in a specific time.

 
        <cache expires-on="@DateTime.Now.AddDays(10)" >
            @CDN.header("dotnetreference.com")
        </cache>

expires-sliding


The expires-sliding attribute expects a TimeSpan value . This attribute is used to expire the cache entry after it has not been accessed for a specified amount of time.

<cache expires-sliding="@TimeSpan.FromMinutes(10)">
        @CDN.header("dotnetreference.com")
    </cache>

How to set Cache Priority in cache tag helpers ?


As I mentioned, Cache tag helpers use the MemoryCache to store the data. The MemoryCache can hold only a limited amount of data. Once the MemeoryCache reaches the limit, the system automatically removes the cached data based on priority . The values for CacheItemPriority are High , Low , NeverRemove , Normal.
Caching in Asp.net Core - Set priority

Enable/Disable cache tag helper


The cache tag helper is enabled by default . However, we can disable caching by passing a value ‘false’ in ‘enabled’ attribute.

<cache expires-sliding="@TimeSpan.FromMinutes(10)"
            priority="@Microsoft.Extensions.Caching.Memory.CacheItemPriority." enabled="false">
        @CDN.header("dotnetreference.com")
    </cache>

Caching with multiple version

Cache content can be vary based on user , query parameters of request , cookie , request header , route . This can be configured by the vary-by attribute on the cache tag helper. These var-by attributes generates a dynamic cache key based on the parameters.
Caching in ASP.NET core - vary-by

vary-by-user

<cache expires-sliding="@TimeSpan.FromMinutes(10)"
            priority="@Microsoft.Extensions.Caching.Memory.CacheItemPriority." enabled="false" vary-by-user ="true">
        @CDN.header("dotnetreference.com")
    </cache>

In the preceding code you can see the implementation vary-by-user. Here the cached data will be different for each logged in user .

vary-by-route


The cache key/data is based on route data parameter.

<cache expires-sliding="@TimeSpan.FromMinutes(10)"
            priority="@Microsoft.Extensions.Caching.Memory.CacheItemPriority." enabled="false" vary-by-route="id">
        @CDN.header("dotnetreference.com")
    </cache>

vary-by-query


The cache key/data is based on the query string parameters of the request.

 
    <cache expires-sliding="@TimeSpan.FromMinutes(10)" vary-by-query="query"
            priority="@Microsoft.Extensions.Caching.Memory.CacheItemPriority." enabled="false">
        @CDN.header("dotnetreference.com")
    </cache>

vary-by-cookie


The cache key/data is based on the value stored in the cookie.

 <cache expires-sliding="@TimeSpan.FromMinutes(10)" vary-by-cookie="MyCookie">
        @CDN.header("dotnetreference.com")
    </cache>

vary-by-header


The cache key/data is based on the value of a specific HTTP request header.

 <cache expires-sliding="@TimeSpan.FromMinutes(10)" vary-by-header="Accept">
        @CDN.header("dotnetreference.com")
    </cache>

vary-by


The cache key/data is based on a string value.

  <cache expires-sliding="@TimeSpan.FromMinutes(10)" vary-by="val">
        @CDN.header("dotnetreference.com")
    </cache>
The following two tabs change content below.

Tom Mohan

Tom Mohan is a technologist who loves to code and build. He enjoys working on Microsoft Technologies. Tom specializes in ASP.NET MVC, Web API , Azure, C# ,WPF, SQL etc and holds a Bachelor engineering degree in Computer Science. Certification : MCSD , MCTS
  • Jeff Wilson

    Tom,
    Great article, but could you tell me more about the CDN.Header() helper you’re using? It’s new to me.
    Thanks!
    Jeff