In MVC 6 you can use Tag Helpers in your views. They are similar come the HTML helpers when know but tag helpers lean more to html then C# code. I already wrote some posts (Creating a custom tag helperTag helper and dependency Injection) about tag helpers but in this one I’ll take a look at the cache tag helper.

The Cache tag helper

It’s actually very simple to use. The content that you want to be cached, should be wrapped within a cache tag. When the view is processed and a cache tag is found, the tag helper will look in its cache to see if the content is already added. If it finds the content, it will get it from the cache and will put this on the view. When the content is found in the cache, it will read it from the cache and will put this on the view. The cache tag will never be sees in the output HTML. It will be removed from the final HTML output. With this cache taghelper you can do something that is called donut caching. This allows you to cache certain regions on a webpage.

<cache>
     @DateTime.Now.ToString()
</cache>

The cache helper uses an IMemoryCache to store the cached contents. This memory cache is linked to the local hosting process. So if this process is terminated (shutdown/restart), everything from the memory cache will be lost. Because it is linked to a local process, you also have to be caution when you have multiple servers. You have to make sure that Application Request Routing (ARR) is configured. Otherwise your users might get some strange results.

Cache tag helper configuration

There are multiple options to configure how long the cached content will be available for the cache tag helper.

expires-after

<cache expires-after="@TimeSpan.FromMinutes(5)">
     @DateTime.Now.ToString()
</cache>

I passed a TimeSpan for 5 minutes to the expires-after attribute. This means that the cache entry will be evicted from the cache 5 minutes after it was added.

expires-sliding

If the cached content isn’t accessed after 5 minutes, the content gets removed from the cache.

<cache expires-sliding="@TimeSpan.FromMinutes(5)">
     @DateTime.Now.ToString()
</cache>

 expires-on

<cache expires-on="@DateTime.Now.AddDays(5)">
     @DateTime.Now.ToString()
</cache>

The cache content will only be removed after the a specific date time. In the above sample the content will be cached for 5 days. After those 5 days, it will be removed from the cache.

How Cache multiple versions?

There are times when you want to cache some content, but the content might be different because of a value of, for example, a query string parameter. You can cache multiple versions based on different parameters. This can be configured by the vary-by attribute on the cache tag helper. These var-by attributes generate a dynamic cache key based on the passed parameters. The cache content is then identified by this cache key in the cache. This way some content can have multiple versions in the cache per cache key.

vary-by-route

<cache vary-by-route="id">
     @DateTime.Now.ToString()
</cache>

The cache key is based on 1 or more route data parameters (here id).

vary-by-query

The cache key is based on  1 or more parameters from the query string.

vary-by-cookie

The cache key is based on a value stored in a cookie.

vary-by

This attribute might be the most useful one. Here can define your own cache key.

<cache vary-by="@Model.Id">
     @DateTime.Now.ToString()
</cache>

In this example the cache key is calculated by the value of the Id property from the viewmodel for the view.

Disable/Enable a cache tag helper

The cache tag helper also contains an Enabled attribute. This attribute requires a boolean value and has by default a true value. When you pass a false value, the cache tag helper will be disabled.

<cache expires-sliding="@TimeSpan.FromMinutes(1)" enabled="@Model.Enabled">
    @DateTime.Now.ToString();
</cache>

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>