Distributed Cache Tag Helper in ASP.NET Core

By Peter Kellner

The Distributed Cache Tag Helper provides the ability to dramatically improve the performance of your ASP.NET Core app by caching its content to a distributed cache source.

For an overview of Tag Helpers, see Tag Helpers in ASP.NET Core.

The Distributed Cache Tag Helper inherits from the same base class as the Cache Tag Helper. All of the Cache Tag Helper attributes are available to the Distributed Tag Helper.

The Distributed Cache Tag Helper uses constructor injection. The IDistributedCache interface is passed into the Distributed Cache Tag Helper's constructor. If no concrete implementation of IDistributedCache is created in Startup.ConfigureServices (Startup.cs), the Distributed Cache Tag Helper uses the same in-memory provider for storing cached data as the Cache Tag Helper.

Distributed Cache Tag Helper Attributes

Attributes shared with the Cache Tag Helper

  • enabled
  • expires-on
  • expires-after
  • expires-sliding
  • vary-by-header
  • vary-by-query
  • vary-by-route
  • vary-by-cookie
  • vary-by-user
  • vary-by
  • priority

The Distributed Cache Tag Helper inherits from the same class as Cache Tag Helper. For descriptions of these attributes, see the Cache Tag Helper.


Attribute Type Example
String my-distributed-cache-unique-key-101

name is required. The name attribute is used as a key for each stored cache instance. Unlike the Cache Tag Helper that assigns a cache key to each instance based on the Razor page name and location in the Razor page, the Distributed Cache Tag Helper only bases its key on the attribute name.


<distributed-cache name="my-distributed-cache-unique-key-101">
    Time Inside Cache Tag Helper: @DateTime.Now

Distributed Cache Tag Helper IDistributedCache implementations

There are two implementations of IDistributedCache built in to ASP.NET Core. One is based on SQL Server, and the other is based on Redis. Third-party implementations are also available, such as NCache. Details of these implementations can be found at Distributed caching in ASP.NET Core. Both implementations involve setting an instance of IDistributedCache in Startup.

There are no tag attributes specifically associated with using any specific implementation of IDistributedCache.

Additional resources