共用方式為


如何:設定 Azure 受控快取服務的快取用戶端

重要

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

受控快取服務藉由提供NuGet套件來新增必要的元件參考和組態,讓您輕鬆地設定快取用戶端。 快取 NuGet 封裝也會提供 web.config 註解的區段,讓 Web 專案啟用快取工作階段狀態和頁面輸出快取提供者。 本主題提供使用快取 NuGet 封裝來設定快取用戶端的逐步解說。

本主題內容

  • 使用快取 NuGet 封裝來設定 Cache Client

  • 在 ASP.NET 專案中使用工作階段狀態提供者

  • 在 ASP.NET 專案中使用輸出快取提供者

使用快取 NuGet 封裝來設定 Cache Client

設定受控快取服務用戶端應用程式很簡單。 受控快取服務提供NuGet套件,以新增必要的組態和元件參考,以允許快取用戶端存取快取。

注意

受控快取服務支援 Azure 網站、雲端服務和虛擬機器用戶端應用程式。 這些不同的用戶端應用程式類型的設定步驟十分相似,僅有些微的不同。 快取 NuGet 封裝是用來自動處理大部分的不同之處,而這些相異點會在本主題中充分說明。 如需差異的摘要,請參閱 快取服務用戶端應用程式類型

如果您要建立雲端服務應用程式,請下載您確定使用的是 Azure SDK 2.1 版或更高版本。 若您建立了 Azure 網站或虛擬機器用戶端應用程式,快取 Cache NuGet 封裝會安裝您開始使用所需的一切項目。

重要

這些步驟需要 NuGet 封裝管理員 2.6.40627.9000 版或更高版本。 若要安裝最新的NuGet 封裝管理員,請參閱NuGet 封裝管理員

若要使用受控快取服務 NuGet套件,請以滑鼠右鍵按一下方案總管中的用戶端專案,然後選擇 [管理NuGet套件]。

選取 [Azure Caching],並依序按一下 [安裝] 與 [我接受]。 當封裝安裝到用戶端專案後,按一下 [關閉] 即可關閉 [管理 NuGet 封裝] 視窗。

注意

如果 [Azure Caching] 未出現在清單中,請在 [線上搜尋] 文字方塊中輸入 WindowsAzure.Caching

Windows Azure Cache NuGet Package

當受控快取服務 NuGet套件安裝至用戶端專案時,它會新增必要的組態和元件參考,讓用戶端應用程式可以存取快取。

在快取專案的 web.configapp.config 中會新增數個項目。

  1. 會新增兩個區段到 configSections,名為 dataCacheClientscacheDiagnostics

    <!-- Non-cache sections omitted for space -->
      <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>
    
  2. 新增至 dataCacheClients 區段的 configuration 區段。

    <!-- Non-cache sections omitted for space -->
      <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>
      </dataCacheClients>
    

    [快取角色名稱或服務端點] 取代為管理入口網站中快取儀表板上顯示的端點 URL。

    Endpoint URL for Windows Azure Cache Service
    在此範例中,快取名為 applicationcache,而端點 URL 會 applicationcache.cache.windows.net

    <autoDiscover isEnabled="true" identifier="applicationcache.cache.windows.net" />
    

    取消批註 securityProperties 區段,並將 [驗證金鑰] 取代為驗證金鑰。

    <!--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>-->
    

    按一下快取儀表板中的 [存取金鑰],可以在管理入口網站中找到驗證金鑰。

    Manage Access Keys for Windows Azure Cache Service

    警告

    必須適當地配置這些設定,否則用戶端將無法存取快取。

針對雲端服務專案,受控快取服務 NuGet套件也會將ClientDiagnosticLevel設定新增至 ConfigurationSettings 中快取用戶端角色的 ServiceConfiguration.cscfg 。 下列範例是 WebRole1來自 ClientDiagnosticLevel為 1 之檔案的區段 ServiceConfiguration.cscfg ,這是預設的 ClientDiagnosticLevel

<Role name="WebRole1">
  <Instances count="1" />
  <ConfigurationSettings>
    <!-- Other settings omitted for space... -->
    <Setting name="Microsoft.WindowsAzure.Plugins.Caching.ClientDiagnosticLevel" value="1" />
  </ConfigurationSettings>
</Role>

注意

如需快取診斷層級的詳細資訊,請參閱關於 Azure 受控快取服務 的 ClientDiagnosticLevel

除了新增必要的組態之外,受控快取服務 NuGet套件也會新增下列元件參考。

  • Microsoft.ApplicationServer.Caching.Client.dll

  • Microsoft.ApplicationServer.Caching.Core.dll

  • Microsoft.ApplicationServer.Caching.AzureCommon.dll

  • Microsoft.ApplicationServer.Caching.AzureClientHelper.dll

  • Microsoft.WindowsFabric.Common.dll

  • Microsoft.WindowsFabric.Data.Common.dll

如果專案是 Web 專案,則也會新增下列組件參考。

  • Microsoft.Web.DistributedCache.dll

在 ASP.NET 專案中使用工作階段狀態提供者

Microsoft Azure快取會話狀態提供者是 ASP.NET 應用程式的跨進程儲存機制。 此提供者可讓您在 快取中儲存工作階段狀態,而非在記憶體中或 SQL Server 資料庫中儲存。 當您使用快取 NuGet 封裝啟用快取時,會新增註解區段以啟用快取工作階段狀態提供者。

  1. 首先,使用快取 ASP.NET 提供者設定快取,如先前使用快取NuGet套件設定快取用戶端一節中所述。

  2. 移除任何設定會話狀態快取的現有 sessionState 元素。 請勿移除NuGet套件所新增的批註sessionState區段。

  3. 在 web.config 檔案中,取消批註 sessionState 區段。

    <!-- Azure Caching session state provider -->
    <!--<sessionState mode="Custom" customProvider="AFCacheSessionStateProvider">
      <providers>
        <add name="AFCacheSessionStateProvider" type="Microsoft.Web.DistributedCache.DistributedCacheSessionStateStoreProvider, Microsoft.Web.DistributedCache" cacheName="default" dataCacheClientName="default" applicationName="AFCacheSessionState"/>
      </providers>
    </sessionState>-->
    

以下清單提供會影響工作階段狀態提供者的選擇性設定變更。

  • 使用add元素的cacheName屬性變更目標具名快取。

  • 使用add元素的dataCacheClientName屬性變更快取用戶端設定的來源。 將此屬性設定為 web.config 檔案中現有 dataCacheClient 區段的名稱。

  • 藉由變更相關聯 dataCacheClient 區段中的設定,修改快取用戶端的行為。 例如,使用 isCompressionEnabled 屬性啟用壓縮。

    <dataCacheClient name="default" isCompressionEnabled="true">
      <!-- Other cache settings omitted -->
    </dataCacheClient>
    

    如需可用組態選項的清單,請參閱Azure 受控快取服務的快取用戶端組態設定。 請注意,快取的 ASP.NET 提供者不支援二進位或自訂序列化類型。 如果這些序列化程式用於會話狀態,就會發生下列例外狀況:「在元件 'Microsoft.Web.DistributedCache.SerializableSessionStateStoreData' 中輸入 'Microsoft.Web.DistributedCache, Version=101.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' 未標示為可序列化」

  • 變更工作階段狀態提供者設定。 如需可用組態選項的清單,請參閱ASP.NET 適用于 Azure 受控快取服務 的會話狀態提供者組態設定。

在 ASP.NET 專案中使用輸出快取提供者

Microsoft Azure快取輸出快取提供者是輸出快取資料的跨進程儲存機制。 此資料特別適用於完整 HTTP 回應 (頁面輸出快取)。 提供者插入 ASP.NET 4 中導入的新輸出快取提供者擴充點。 當您使用快取NuGet套件來啟用快取時,它會新增批註區段來啟用快取頁面輸出提供者。

  1. 首先,使用快取 ASP.NET 提供者設定快取,如先前使用快取NuGet套件設定快取用戶端一節中所述。

  2. 使用 NuGet,在 Visual Studio 中將快取支援新增至您的 ASP.NET 專案。

    重要

    這些步驟需要最新的 NuGet 封裝管理員 (版本 2.6.40627.9000 或更高)。 若要安裝最新的NuGet 封裝管理員,請參閱NuGet 封裝管理員

  3. 移除任何設定輸出快 取的現有快取 專案。 請勿移除NuGet套件所新增的批註快區段。

  4. 在 web.config 檔案中,取消批註 快取 區段。

        <!-- 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>-->
    

以下清單提供會影響輸出快取提供者的選擇性設定變更。

  • 使用add元素的cacheName屬性變更目標具名快取。

  • 使用add元素的dataCacheClientName屬性變更快取用戶端設定的來源。 將此屬性設定為 web.config 檔案中現有 dataCacheClient 區段的名稱。

  • 藉由變更相關聯 dataCacheClient 區段中的設定,修改快取用戶端的行為。 例如,使用 isCompressionEnabled 屬性啟用壓縮。

    <dataCacheClient name="default" isCompressionEnabled="true">
    

    如需可用組態選項的清單,請參閱Azure 受控快取服務的快取用戶端組態設定。 請注意,快取的 ASP.NET 提供者不支援二進位或自訂序列化類型。

  • 變更輸出快取提供者設定。 如需可用組態選項的清單,請參閱Azure 受控快取服務 ASP.NET 網頁輸出快取提供者組態設定。