Share via


適用於 Azure 受管理的快取服務的 ASP.NET 頁面輸出快取提供者組態設定

重要

Microsoft 建議所有新的開發都使用 Azure Redis 快取。 如需選擇 Azure 快取供應專案的目前檔和指引,請參閱 哪個 Azure 快取供應專案適合我?

本主題涵蓋適用于 ASP.NET 之 Azure 受控快取服務輸出快取提供者的組態設定。 這些設定是在 web.config 檔案中outputCache元素的providers區段中指定。

輸出快取組態設定

屬性 描述

需要名稱 ()

outputCache元素用來參考提供者的提供者「易記」名稱。

類型 (必要)

提供者的 .NET Framework 型別字串。 必要值請參閱下面的附註。

cacheName (必要)

Azure 快取的名稱。 這必須設定為 “default”

dataCacheClientName (選擇性)

要從 dataCacheClients 組態區段使用的 dataCacheClient 區 段名稱。 只有在web.config檔案中指定多個 dataCacheClient 區段時,才需要這個屬性。 根據預設,提供者將使用名為 “default”的 dataCacheClient區段。

applicationName (選擇性)

在建立用於儲存輸出快取資料的快取金鑰時,提供者所使用的字串值。 預設為空字串。 未設定此屬性時,提供者會使用 HttpRuntime.AppDomainAppId 的值做為其內部使用的快取索引鍵的一部分。 與工作階段狀態功能不同,您不會想要在不同 ASP.NET 應用程式間共用輸出快取資料 (例如,/contoso 和 /AdventureWorks 無法共用輸出快取資料)。 取而代之的是確保相同應用程式的不同實體例項全都可以存取相同的輸出快取資料。 有兩種不統方式可以完成此動作:

  • 如果未明確設定 applicationName 提供者屬性,則在建構快取金鑰時,提供者會在內部使用 HttpRuntime.AppDomainAppId 。 這表示相同應用程式的每個實體例項 (即主控 /contoso 應用程式的每個 Web 伺服器) 都必須以相同 Metabase 路徑安裝在 IIS 中。 如需中繼基底路徑如何搭配SQL Server和跨進程會話狀態提供者使用的說明,請參閱 https://support.microsoft.com/kb/325056 。 雖然會話狀態是不同的功能,但相同的問題會讓中繼基底路徑保持同步。這適用于未在 web.config 檔案中設定 applicationName 屬性時的輸出快取。

  • 較簡單的方法是針對裝載 /contoso 應用程式的每個網頁伺服器上,ASP.NET 應用程式的每個實例 (,) 在web.config檔案中使用相同的applicationName屬性。 這可讓相同應用程式的不同實體例項讀取和寫入相同的輸出快取資料。 在此情況下,提供者在建構快取索引鍵時不會使用 AppDomainAppId ,因此不會有不相符中繼基底路徑的風險。

retryInterval (選擇性)

一段時間,在與快取通訊時若發生錯誤,每次重試之間要等候的時間長度。 此值所使用的字串格式為 "HH:MM:SS"。 根據預設值,提供者會休息一秒鐘。

retryCount (選擇性)

整數值,讓提供者知道要在快取通訊發生錯誤時重試幾次。 請注意,並非所有操作都可重試。 預設值為重試三次。 提供者會在每次重試嘗試之間睡眠已設定 的 retryInterval 時間。

注意

類型屬性應設定為 “Microsoft.Web.DistributedCache.DistributedCacheOutputCacheProvider, Microsoft.Web.DistributedCache”

範例

<!-- Non-cache sections omitted for space -->
<configuration>
  <configSections>
    <section name="dataCacheClients" 
        type="Microsoft.ApplicationServer.Caching.DataCacheClientsSection, Microsoft.ApplicationServer.Caching.Core" 
        allowLocation="true" 
        allowDefinition="Everywhere" />
    <section name="cacheDiagnostics" 
        type="Microsoft.ApplicationServer.Caching.AzureCommon.DiagnosticsConfigurationSection, Microsoft.ApplicationServer.Caching.AzureCommon" 
        allowLocation="true" 
        allowDefinition="Everywhere" />
  </configSections>
  <system.web>
    <!-- Azure Caching output caching provider -->
    <!--Uncomment this section to use Azure Caching for output caching-->
    <caching>
      <outputCache defaultProvider="AFCacheOutputCacheProvider">
        <providers>
          <add name="AFCacheOutputCacheProvider" 
            type="Microsoft.Web.DistributedCache.DistributedCacheOutputCacheProvider, Microsoft.Web.DistributedCache"
            cacheName="default"
            dataCacheClientName="default"
            applicationName="AFCacheOutputCache" />
        </providers>
      </outputCache>
    </caching>
  </system.web>
  <dataCacheClients>
    <dataCacheClient name="default">
      <!--To use the in-role flavor of Azure Caching, set identifier to be the cache cluster role name -->
      <!--To use the Azure Caching Service, set identifier to be the endpoint of the cache cluster -->
      <autoDiscover
        isEnabled="true" 
        identifier="[Cache role name or Service Endpoint]" />
      <!--<localCache isEnabled="true" sync="TimeoutBased" objectCount="100000" ttlValue="300" />-->
      <!--Use this section to specify security settings for connecting to your cache. 
          This section is not required if your cache is hosted on a role that is a part 
          of your cloud service. -->
      <!--<securityProperties mode="Message" sslEnabled="false">
        <messageSecurity authorizationInfo="[Authentication Key]" />
      </securityProperties>-->
    </dataCacheClient>
    <cacheDiagnostics>
      <crashDump dumpLevel="Off" dumpStorageQuotaInMB="100" />
    </cacheDiagnostics>
  </dataCacheClients>
</configuration>

注意

僅有快取 NuGet 封裝會新增 cacheDiagnostics 區段到 ASP.NET Web 角色用戶端。 如需詳細資訊,請參閱關於 Azure 受控快取服務的 ClientDiagnosticLevel

另請參閱

概念

Azure 受管理快取服務的頁面輸出快取提供者