Azure 角色中快取內的組態模型

重要

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

本主題涵蓋在 Microsoft Azure Cache 中使用組態檔。 組態設定有兩種類型。

  1. 角色設定

  2. 用戶端組態

角色設定

In-Role Cache 支援在 Azure 角色內裝載快取的功能。 這種類型的快取會設定為雲端服務的一部分。 一般而言,這會在Visual Studio中完成。

注意

本節所述的組態設定僅適用于角色型In-Role快取。 共用快取僅支援存取快取 default 。 共用快取不支援改變快取的屬性 default

若要瞭解組態設定,將這些設定與Visual Studio中的使用者介面選項相互關聯會很有説明。 下列螢幕擷取畫面顯示 [角色內容] 對話方塊中 [快取] 索引標籤的一部分。

Caching Co-located Properites Window

除了啟用In-Role快取之外,您也可以建立一或多個具名快取。 每個快取都會指定它自己的內容。 下列螢幕擷取畫面顯示 [快取] 內容索引標籤的這個部分。

Caching Properties for Named Caches

這些使用者介面選取項目都儲存於設定檔中。 首先, 快取 模組會載入 ServiceDefinition.csdef 檔案的 Imports 區段中。 下列 XML 程式碼片段會顯示這一行。

<Import moduleName="Caching" />

所有其他In-Role快取設定都會儲存在 ServiceConfiguration.cscfg 檔案中。 例如,以下為 ServiceConfiguration.cscfg 檔案中適用於先前所示之 [快取] 索引標籤選取項目的設定。

  <Role name="WebRole1">
    <Instances count="2" />
    <ConfigurationSettings>
      <Setting name="Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString" value="UseDevelopmentStorage=true" />
      <Setting name="Microsoft.WindowsAzure.Plugins.Caching.NamedCaches" value="{&quot;caches&quot;:[{&quot;name&quot;:&quot;NamedCache1&quot;,&quot;policy&quot;:{&quot;eviction&quot;:{&quot;type&quot;:0},&quot;expiration&quot;:{&quot;defaultTTL&quot;:20,&quot;isExpirable&quot;:true,&quot;type&quot;:2},&quot;serverNotification&quot;:{&quot;isEnabled&quot;:true}},&quot;secondaries&quot;:1},{&quot;name&quot;:&quot;NamedCache2&quot;,&quot;policy&quot;:{&quot;eviction&quot;:{&quot;type&quot;:-1},&quot;expiration&quot;:{&quot;defaultTTL&quot;:25,&quot;isExpirable&quot;:true,&quot;type&quot;:1},&quot;serverNotification&quot;:{&quot;isEnabled&quot;:false}},&quot;secondaries&quot;:0}]}" />
      <Setting name="Microsoft.WindowsAzure.Plugins.Caching.Loglevel" value="" />
      <Setting name="Microsoft.WindowsAzure.Plugins.Caching.CacheSizePercentage" value="30" />
      <Setting name="Microsoft.WindowsAzure.Plugins.Caching.ConfigStoreConnectionString" value="UseDevelopmentStorage=true" />
    </ConfigurationSettings>
  </Role>

在此角色設定中,大部分In-Role快取設定都與 [ 取] 索引標籤上的選項有簡單的相互關聯。不過,設定 Microsoft.WindowsAzure.Plugins.Caching.NamedCaches 較難閱讀。 它使用 JSON 語法來描述每個具名快取的內容,而且使用 &quot; 來取代所有的雙引號。 如需角色組態設定的參考,請參閱設定 (ServiceConfiguration.cscfg) 中的角色快取角色組態。

用戶端組態

取用戶端 是存取 Azure 快取的任何應用程式程式碼。 在程式碼中,每個快取用戶端都會與 DataCacheFactory 物件相關聯。 Factory 會傳回用來存取快取的 DataCache 物件。 快取用戶端的實際設定可以從應用程式或 web.config 設定檔載入。

下列範例示範在組態檔中設定名為 default的 dataCacheClient區段的相關區段。 這是以角色為基礎的In-Role快取特有的。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="dataCacheClients" type="Microsoft.ApplicationServer.Caching.DataCacheClientsSection, Microsoft.ApplicationServer.Caching.Core" allowLocation="true" allowDefinition="Everywhere" />
  </configSections>
  <dataCacheClients>
    <tracing sinkType="DiagnosticSink" traceLevel="Error" />
    <dataCacheClient name="default">
      <autoDiscover isEnabled="true" identifier="WebRole1" />
    </dataCacheClient>
  </dataCacheClients>
</configuration>

如需這些用戶端組態設定的參考,請參閱角色快取用戶端組態 設定 (Web.config) 。 如需示範如何設定角色型In-Role快取用戶端的逐步解說,請參閱 如何:使用 Azure SDK In-Role快取

另請參閱

概念

Azure 快取的角色中快取功能
角色中快取角色組態設定 (ServiceConfiguration.cscfg)
角色中快取用戶端組態設定 (Web.config)