ASP.NET Core 中的分散式快取標籤協助程式Distributed Cache Tag Helper in ASP.NET Core

Peter Kellner 提供By Peter Kellner

分散式快取標籤協助程式可將 ASP.NET Core 應用程式內容快取至分散式快取來源,因此大幅提升應用程式的效能。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.

如需標籤協助程式的概觀,請參閱 ASP.NET Core 中的標籤協助程式For an overview of Tag Helpers, see 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. IDistributedCache 介面會被傳入分散式快取標籤協助程式的建構函式。The IDistributedCache interface is passed into the Distributed Cache Tag Helper's constructor. 如果 Startup.ConfigureServices 中未建立 IDistributedCache 的具體實作 (Startup.cs),分散式快取標籤協助程式將會使用與快取標籤協助程式相同的記憶體內部提供者來儲存快取資料。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.

namename

屬性類型Attribute Type 範例Example
字串String my-distributed-cache-unique-key-101

name 是必要的。name is required. name 屬性會作為每個預存快取執行個體的索引鍵。The name attribute is used as a key for each stored cache instance. 不同于快取標籤協助程式會根據頁面名稱和Razor Razor分頁中的位置,將快取索引鍵指派給每個實例,分散式快取標籤協助程式name只會以屬性的索引鍵為基礎。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>

分散式快取標記協助程式 IDistributedCache 實作Distributed Cache Tag Helper IDistributedCache implementations

ASP.NET Core 內建兩項 IDistributedCache 實作。There are two implementations of IDistributedCache built in to ASP.NET Core. 其中一個是以 SQL Server 為基礎,另一項是以 Redis 為基礎。One is based on SQL Server, and the other is based on Redis. 協力廠商的執行也可供使用,例如NCacheThird-party implementations are also available, such as NCache. 如需這些實作的詳細資料,請參閱<xref:performance/caching/distributed>。Details of these implementations can be found at <xref:performance/caching/distributed>. 這兩個實作都涉及在 StartupIDistributedCache 中設定執行個體。Both implementations involve setting an instance of IDistributedCache in Startup.

沒有特別與使用 IDistributedCache 的任何特定實作建立關聯的標籤屬性。There are no tag attributes specifically associated with using any specific implementation of IDistributedCache.

其他資源Additional resources