從角色中快取移轉至 Azure 受管理的快取服務
重要
Microsoft 建議所有新的開發都使用 Azure Redis 快取。 如需選擇 Azure 快取供應專案的目前檔和指引,請參閱 哪個 Azure 快取供應專案適合我?
使用 In-Role Cache 移轉至 Azure 受控快取服務的 Microsoft Azure 快取應用程式,可以透過對應用程式進行最少的變更來完成。 因為受控快取服務使用與In-Role快取相同的 API,所以使用 In-Role Cache 存取快取的現有程式碼可以重複使用受控快取服務。 本主題說明如何進行必要的設定和應用程式變更,以移轉In-Role快取應用程式以使用受控快取服務。
移轉步驟
下列各節說明移轉In-Role快取應用程式以使用受控快取服務所需的步驟。
選擇快取供應項目
建立快取
設定快取
解除委任角色中的快取叢集
設定快取用戶端
解除安裝角色內快取 NuGet 封裝
使用快取 NuGet 封裝來設定 Cache Client
移轉 ASP.NET 工作階段和頁面輸出 Caching
選擇快取供應項目
下列三個供應專案提供受控快取服務。
基本 - 大小從 128MB 到 1GB 的快取 (每次增量 128MB),具有一個預設具名快取
標準 - 大小介於 1GB 到 10GB 且以 1GB 為增量單位的快取,其中支援通知、高可用性以及最多 10 個具名快取
高級 - 大小從 5GB 到 150GB 的快取 (每次增量 5GB),支援通知、高可用性和最多 10 個具名快取
注意
每個供應項目的價格和功能都不相同。 如需詳細資訊,請參閱適用于 Azure 受控快取服務的快取服務定價常見問題和快取供應專案。 Additional details are also provided in the following Configure the Cache section in this migration guide.
In-Role快取的所有快取功能都可在不同的快取供應專案中取得受控快取服務。 若您的應用程式使用通知,您應該關閉標準版或基本版快取供應項目。 若您的快取已啟用高可用性,您應該選擇進階版快取供應項目。 移轉的起點是挑選具有應用程式所需快取功能的受控快取服務快取供應專案,並符合In-Role快取應用程式的容量需求。
部署應用程式之後,您可以監視快取的效能,並根據應用程式的需求相應增加或減少。 如需監視效能和調整的詳細資訊,請參閱監視 Azure 受控快取服務和調整適用于 Azure 受控快取服務的快取。
建立快取
受控快取服務中的快取實例是使用 PowerShell 腳本建立的。
重要
一旦使用 PowerShell Cmdlet 建立受控快取服務實例,就可以在Azure 管理入口網站中檢視和設定實例。
建立受控快取服務實例
開啟 Azure PowerShell 命令視窗。
注意
如需安裝和使用 Azure PowerShell 的指示,請參閱 如何安裝和設定 Azure PowerShell。
叫用
Add-AzureAccount
Cmdlet,並輸入與您帳戶相關的電子郵件地址及密碼。注意
如果您已使用帳戶的憑證來設定 Azure PowerShell,那麼就可以跳過此步驟。 如需將 Azure PowerShell 與 Azure 帳戶連接的詳細資訊,請參閱 如何安裝和設定 Azure PowerShell。
叫用
New-AzureManagedCache
Cmdlet 並指定名稱、區域、快取供應項目以及快取大小。 如果是下列範例,基本的 128MB 快取會在contosocache
地理區域中以South Central US
的名稱建立。New-AzureManagedCache -Name contosocache -Location "South Central US" -Sku Basic -Memory 128MB
注意
如需建立快取時可使用之參數和值的完整清單,請參閱 New-AzureManagedCache Cmdlet 文件。
叫用 PowerShell Cmdlet 之後,可能需要數分鐘的時間建立快取。 建立快取之後,您的新快取具有 [執行 中] 狀態,且已準備好搭配預設設定使用,而且可以在 Azure 管理入口網站中檢視和設定。
您可以在 Azure PowerShell 視窗中監視建立進度。 一旦快取可供使用,
New-AzureManagedCache
Cmdlet 會顯示快取資訊,如以下範例所示。PS C:\> Add-AzureAccount VERBOSE: Account "user@domain.com" has been added. VERBOSE: Subscription "MySubscription" is selected as the default subscription. VERBOSE: To view all the subscriptions, please use Get-AzureSubscription. VERBOSE: To switch to a different subscription, please use Select-AzureSubscription. PS C:\> New-AzureManagedCache -Name contosocache -Location "South Central US" -Sku Basic -Memory 128MB VERBOSE: Intializing parameters... VERBOSE: Creating prerequisites... VERBOSE: Verify cache service name... VERBOSE: Creating cache service... VERBOSE: Waiting for cache service to be in ready state... Name : contosocache Location : South Central US State : Active Sku : Basic Memory : 128MB PS C:\>
設定快取
In-Role快取和受控快取服務支援具名快取,可讓您彈性地為不同的資料集設定不同的快取選項。 每個受控快取服務快取都有預設的具名快取,而且在標準和進階版快取供應專案中,最多可以設定九個額外的具名快取。 每個具名快取都具有其自己的個別設定,例如高可用性、通知、以及自訂收回和到期原則。 具名快取設定會在快取的 [設定] 索引標籤中的 [管理入口網站] 中設定。
雖然In-Role快取和受控快取服務具有相同的快取功能,但下表列出稍有差異。
功能 | 受控快取服務支援 | In-Role快取支援 |
---|---|---|
名稱 |
已設定 [預設] 快取,若需要,在標準版和進階版快取供應項目中,最多可以設定 9 個額外的具名快取。 |
已設定 [預設] 快取,可設定其他具名快取。 |
高可用性 |
「標準」和「進階版」快取供應專案提供高可用性,而且無法在基本快取供應專案中使用。 如需詳細資訊,請參閱Azure 受控快取服務快取供應專案。 |
所有In-Role快取拓撲都可以啟用高可用性。 |
通知 |
標準版和進階版快取供應項目中包括通知,而基本版中則未包括。 如需詳細資訊,請參閱Azure 受控快取服務快取供應專案。 |
所有In-Role快取拓撲都可以啟用通知。 |
收回原則 |
選擇已啟用,其使用 LRU (最近最少使用) 演算法來判斷要收回或停用的項目。 預設值為 [已啟用]。 |
選項為 [LRU] (最近使用) 或 [無]。 預設值為 [LRU]。 |
時間 (分鐘) |
在 受控快取服務 中,此設定名為Time (min) 。 |
在In-Role快取中,此設定名為 存留時間 (分鐘) 。 |
到期原則 |
此設定名為 [到期 原則],且具有三個可能的值: 永不、 絕對和 滑動 ,對應至In-Role快取的 到期原則 設定。 |
此設定名為 [到期原則 ],其具有三個可能的值: [無]、[ 絕對] 和 [滑動視窗]。 |
若要從In-Role快取移轉至受控快取服務,請在新的受控快取服務快取中,從In-Role快取叢集重新建立所有具名快取。
解除委任角色中的快取叢集
設定新的受控快取服務快取之後,下一個步驟是從裝載In-Role快取的角色中移除快取。 這些步驟會根據In-Role快取的快取叢集拓撲而有所不同。
專用的角色快取叢集
共置角色快取叢集
專用的角色快取叢集
若要移除專用角色快取叢集,您必須從Visual Studio解決方案和雲端服務專案中的 [角色] 資料夾中移除快取背景工作角色專案。
在 [方案總管] 的雲端服務專案的 [角色] 資料夾中的快取背景工作角色按一下滑鼠右鍵,並選擇 [移除]。
在 [方案總管] 中的背景工作角色專案上按一下滑鼠右鍵,並選擇 [移除]。
共置角色快取叢集
若要停用 Web 或背景工作角色中的共置角色快取,您必須取消勾選角色屬性中的 [快取] 索引標籤上的 [快取] 設定。
在 [方案總管] 中的雲端服務專案角色上按一下滑鼠右鍵,並選擇 [屬性]。
切換至 [快取] 索引標籤,並取消勾選 [啟用快取] 核取方塊,再按 CTRL+S 儲存。
設定快取用戶端
建立並設定快取後,下一步是新增必要的設定和參考,讓快取用戶端可存取快取。 受控快取服務支援下列用戶端。
Azure 網站
Azure 雲端服務 Web 角色和工作角色
Azure 虛擬機器
注意
如果In-Role快取專案已經使用最新的 Azure SDK,則您只需要略過下列步驟,並遵循更新快取用戶端設定以使用快取服務中所述的程式來更新快取用戶端設定。
如果In-Role快取專案使用 Azure SDK 1.8 - 2.0,則您需要移除快取NuGet套件、移除其餘的快取用戶端設定,然後安裝目前為 2.1) 的最新Azure SDK (。 升級 SDK 之後,安裝並設定最新的快取 NuGet 封裝。 若要設定快取用戶端,請對每個快取用戶端執行下列步驟。
解除安裝角色內快取 NuGet 封裝
使用快取 NuGet 封裝來設定 Cache Client
解除安裝角色內快取 NuGet 封裝
必須先移除現有的In-Role快取組態和元件參考,才能設定用戶端應用程式受控快取服務。 這可以透過解除安裝快取 NuGet 封裝來完成。
若要卸載In-Role快取NuGet套件,請以滑鼠右鍵按一下方案總管中的用戶端專案,然後選擇 [管理NuGet套件]。 選擇 [已安裝封裝] 節點,並在 [搜尋已安裝封裝] 方塊中輸入 [Caching]。 選取[Windows Azure 快取],按一下 [卸載],然後按一下 [關閉]。
注意
在下列快照中,NuGet 封裝版本為 2.1.0.0。 若您有此版本或更高版本,可以略過此步驟。 只有在NuGet套件版本為1.8 或 2.0 時 ,才需要執行此步驟。
卸載快取In-Role快取NuGet套件會移除In-Role快取元件,以及用戶端應用程式或 web.config
應用程式中的In-Role快取專案 app.config
。 由於部分自訂設定在解除安裝 NuGet 封裝時不會加以移除,請開啟 web.config
或 app.config
,並確定已完全移除以下元素。
確定已從
configSections
項目移除dataCacheClients
項目。 請勿移除整個configSections
項目,只需移除dataCacheClients
項目 (若存在)。<configSections> <!-- Existing sections omitted for clarity. --> <section name="dataCacheClients" type="Microsoft.ApplicationServer.Caching.DataCacheClientsSection, Microsoft.ApplicationServer.Caching.Core" allowLocation="true" allowDefinition="Everywhere" /> </configSections>
確定已移除
dataCacheClients
區段。dataCacheClients
區段會類似下列範例。<dataCacheClients> <dataCacheClient name="default" channelOpenTimeout="0"> <autoDiscover isEnabled="true" identifier="CacheWorker" /> </dataCacheClient> </dataCacheClients>
移除In-Role快取設定之後,您可以如下列使用快取NuGet套件設定快取用戶端一節所述設定快取用戶端。
使用快取 NuGet 封裝來設定 Cache Client
受控快取服務提供NuGet套件,以新增必要的組態和元件參考,以允許快取用戶端存取快取。
重要
使用 受控快取服務 NuGet 套件設定快取用戶端之前,請確定In-Role快取設定已從 或 app.config
用戶端角色完全移除 web.config
,如上一節所述。
重要
這些步驟需要最新的 NuGet 封裝管理員 (版本 2.2 或更高)。 若要安裝最新的NuGet 封裝管理員,請參閱NuGet 封裝管理員。
若要使用受控快取服務 NuGet套件,請以滑鼠右鍵按一下方案總管中的用戶端專案,然後選擇 [管理NuGet套件]。
選取 [Azure Caching],並依序按一下 [安裝] 與 [我接受]。 當封裝安裝到用戶端專案後,按一下 [關閉] 即可關閉 [管理 NuGet 封裝] 視窗。
注意
如果 [Azure Caching] 未出現在清單中,請在 [線上搜尋] 文字方塊中輸入 WindowsAzure.Caching。
當受控快取服務 NuGet套件安裝至用戶端專案時,它會新增必要的組態和元件參考,讓用戶端應用程式可以存取快取。
在專案的 web.config
或 app.config
中會新增數個項目。
會新增兩個區段到
configSections
,名為dataCacheClients
和cacheDiagnostics
。<!-- 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>
新增至
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。
在此範例中,快取名為 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>-->
按一下快取儀表板中的 [管理金鑰],可以在管理入口網站中找到驗證金鑰。 如需存取金鑰及使用
sslEnabled
屬性保護快取通訊的詳細資訊,請參閱Azure 受控快取服務的安全性模型。警告
必須適當地配置這些設定,否則用戶端將無法存取快取。
針對雲端服務專案,受控快取服務 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
注意
受控快取服務和In-Role快取共用相同的 API,雖然元件名稱相同,但元件本身不同,而且位於不同的位置。 受控快取服務 NuGet套件將會移除快取元件參考In-Role,並新增正確的受控快取服務元件參考。 受控快取服務元件位於 C:\Program Files\Microsoft SDKs\Windows Azure\.NET SDK\v2.1\ref\Caching
資料夾中。
更新快取用戶端設定以使用快取服務
如果In-Role快取專案已經使用最新的 Azure SDK,則您只需要更新快取用戶端設定以指向新的受控快取服務快取。
開啟快取用戶端專案的 web.config 或 app.config,並尋找
dataCacheClients
區段。<!-- Non-cache sections omitted for space --> <dataCacheClients> <dataCacheClient name="default"> <!--To use the in-role flavor of Windows Azure Caching, set identifier to be the cache cluster role name --> <!--To use the Windows Azure Caching Service, set identifier to be the endpoint of the cache cluster --> <autoDiscover isEnabled="true" identifier="MyCacheWorkerRole" /> <!--<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>
在 區
autoDiscover
段中,將In-Role快取叢集的角色名稱取代為 屬性中identifier
受控快取服務快取的端點 URL 名稱。 在管理入口網站的快取儀表板中顯示的端點 URL。
在此範例中,快取名為 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>-->
按一下快取儀表板中的 [存取金鑰],可以在管理入口網站中找到驗證金鑰。
移轉 ASP.NET 工作階段和頁面輸出 Caching
一旦 ASP.NET Web 專案從In-Role快取移轉至受控快取服務,如設定快取用戶端中所述,只需要進行最少的變更,才能在快取中儲存 ASP.NET 會話狀態或頁面輸出快取。 若要將 ASP.NET 工作階段狀態儲存在快取中,請將下列區段新增至 system.web
中的 web.config
。
<sessionState mode="Custom" customProvider="AppFabricCacheSessionStoreProvider">
<providers>
<add name="AppFabricCacheSessionStoreProvider" type="Microsoft.Web.DistributedCache.DistributedCacheSessionStateStoreProvider, Microsoft.Web.DistributedCache" cacheName="default" useBlobMode="true" dataCacheClientName="default" />
</providers>
</sessionState>
注意
如果您的 ASP.NET Web 角色已設定為使用會話狀態提供者進行快取,則此區段已經存在,而且不需要任何變更。
更新 cacheName
來指定保存工作階段狀態的快取。 使用 default
來指定預設快取。
若要將頁面輸出 Caching 儲存在快取中,請將下列區段新增至 system.web
。
<caching>
<outputCache defaultProvider="DistributedCache">
<providers>
<add name="DistributedCache" type="Microsoft.Web.DistributedCache.DistributedCacheOutputCacheProvider, Microsoft.Web.DistributedCache" cacheName="default" dataCacheClientName="default" />
</providers>
</outputCache>
</caching>
注意
如果您的 ASP.NET Web 角色已設定為使用輸出快取提供者進行快取,則此區段已經存在,而且不需要變更。
更新 cacheName
來指定保存工作階段狀態的快取。 使用 default
來指定預設快取。
在您要快取輸出的每一個頁面中加入 OutputCache
指示詞。
<%@ OutputCache Duration="60" VaryByParam="*" %>
在此範例中,快取的頁面資料在快取中保留 60 秒,而每一個參數組合會快取不同版本的頁面。 如需可用選項的詳細資訊,請參閱 OutputCache 指示詞。
另請參閱
概念
Azure 受管理快取服務的快取供應項目
Azure 受管理快取服務的容量規劃
適用於 Azure 受管理的快取服務工作階段狀態提供者
Azure 受管理快取服務的頁面輸出快取提供者