title | author | description | ms.author | ms.custom | ms.date | uid |
---|---|---|---|---|---|---|
Distributed Cache Tag Helper in ASP.NET Core |
pkellner |
Learn how to use the Distributed Cache Tag Helper. |
tdykstra |
mvc |
01/24/2020 |
mvc/views/tag-helpers/builtin-th/distributed-cache-tag-helper |
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 xref:mvc/views/tag-helpers/intro.
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 xref:Microsoft.Extensions.Caching.Distributed.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.
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
.
Example:
<distributed-cache name="my-distributed-cache-unique-key-101">
Time Inside Cache Tag Helper: @DateTime.Now
</distributed-cache>
There are two implementations of xref:Microsoft.Extensions.Caching.Distributed.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 xref:performance/caching/distributed. Both implementations involve setting an instance of IDistributedCache
in Startup
.
There are no tag attributes specifically associated with using any specific implementation of IDistributedCache
.
- xref:mvc/views/tag-helpers/builtin-th/cache-tag-helper
- xref:fundamentals/dependency-injection
- xref:performance/caching/distributed
- xref:performance/caching/memory
- xref:security/authentication/identity