Windows Azure - 使用Cache Service來讓Web Site網頁有快取效果
在Windows Azure上,要擴充網站,第一個遇到的問題就是存放Session,在Windows Azure的歷史上,從存放Session到Share Cache ( 2014停止支援 ),然後還有存放到Table Storage (官方也不建議了),然後到Azure SQL Database,然後Cloud Service上的Cache機制…
而雖然Cloud Service上的Web已經有解了,但是Web Site還是只能快SQL Database撐著;而如今隨著時代的演進,Azure上也提供了Cache Service (其實已經提供一段時間了…Orz… )
那今天我們就利用Cache Service來存放網頁的page,來達到提供網站的效能吧!!
首先,我們要先建立一個Cache Service。
基本上,如果沒有特別需求,選擇Basic就夠了…但如果想嘗到Cache的HA,那就必須選擇高階的選項 (最低從5G開始),而如果想要有通知功能,至少也要有中階…簡單的說,口袋不夠深,只選擇Basic,就真的只提供Cache….
嗯,小弟的口袋本來就不夠深,所以只能先嘗試看看Basic,看看未來有沒有人願意提供摳摳,小弟再來嘗試看看比較高階的應用XDDD
Cache建構的時間比較久,幾乎可以泡完一碗泡麵….而建立完成後,如下圖,就可以點進去做一些設定。
我們進入Configure裡面,就可以針對Cache進行一些設定,例如時間等等…而這邊因為沒有啟用高階或是中階的功能,所以也不能設定通知和高可用性QQ…. ( 未來有需要測試,再來設定看看吧… ) 另外,也因為是Basic,所以只能定義一組,如果是其他等級的,就可以定義10組以上的Cache設定,給不同的狀況下來使用。至於這些詳細的設定,目前MSDN也提供中文的敘述了,就直接貼給大家當作參考吧 !!~
接下來切到Scale底下;如果有需要,也可以從這邊進行一些設定,切換成中階的Cache或是高階的Cache,基本上,這是給有錢人看的…哭哭…
大致上就這樣,接下來,我們就來看看ASP.NET MVC上,怎樣使用Cache。
首先,我們還是要用老朋友NuGet來取得套件;這邊當然就是Windows Azure Cache了~
完成之後,會自動幫我們加上一堆設定檔在Web.config上,但我們還是需要調整一下Web.config。
調整的內容如下,我們要把<caching>區段的註解拿掉,然後在<autoDiscover>裡面的identifier屬性輸入Caching的url,並且把<securityProperties>區段的註解拿掉,並且在authorizationInfo裡面也填入金鑰。
基本上如下圖;另外因為這邊沒有使用到將Session存放到Cache的功能,所以<providers>的區段我們就維持註解。
另外,這邊也補充一下Web.config對應的關係其實是如下圖所示。
這邊是完成後的Web.config,金鑰的地方已經被小弟改成Key了,別忘了要輸入自己的Key喔!
<?xml version="1.0" encoding="utf-8"?>
<!--
如需如何設定 ASP.NET 應用程式的詳細資訊,請瀏覽
http://go.microsoft.com/fwlink/?LinkId=301880
-->
<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>
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
<!-- Windows Azure Cache 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>--><!-- Windows Azure Cache output cache provider --><!--Uncomment this section to use Windows Azure Cache for output cache-->
<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" isCompressionEnabled="true">
<!--To use the in-role flavor of Windows Azure Cache, set identifier to be the cache cluster role name -->
<!--To use the Windows Azure Cache Service, set identifier to be the endpoint of the cache cluster -->
<autoDiscover isEnabled="true" identifier="test12.cache.windows.net" />
<!--<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="Key" />
</securityProperties>
</dataCacheClient>
</dataCacheClients></configuration>
接下來,我們要用asp.net mvc程式來測試一下,這邊我們在整個Action上面加上OutputCache輸出,並且另用Duration參數設定為120秒。
using Microsoft.ApplicationServer.Caching;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.UI;
namespace WebApplication1.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
[OutputCache(Duration = 120, Location = OutputCacheLocation.Server, VaryByParam = "none")]
public ActionResult Index()
{
ViewData["Date"] = DateTime.Now.Millisecond;
return View();
}
}
}
原則上,設定完後,就會有效果了,但我們為了確認真的有使用到Azure Cache Service,所以我們這邊稍微對Cache Service的Configure做一個調整,我們把時間調整成1min;這樣做的意思是表示,這個Cache的生命只有一分鐘的時間( 好可憐… ),當一分鐘過後,這個Cache就會消失了…
但我們在MVC的OutputCache是設定120秒阿(兩分鐘),是的,所以如果真的有存到Azure Cache Service的話,雖然MVC的OutputCache是設定兩分鐘,但實際上,到一分鐘的時候,在Azure上的Cache就會消失了,是沒辦法到兩分鐘的;而如果設定失敗…那就會有存活兩分鐘的現象… (通常也會直接報錯了…)
最終執行的結果如下,第一次執行的毫秒是584.
過了一分鐘後,變成346…好吧,我想這樣大家也看不出來…總之,如果有興趣的話,大家可以試試看,這部分還滿簡單的喔!
這樣就完成了Cache..
後記
其實如果要使用Session也很簡單,只要稍微調整一下,原本打算一併寫的,但畢竟現在已經有中文的資料了,所以只拋磚引玉的寫一篇關於OutputCache的,而未來如果有需要,會再寫一篇關於Session的,那現在如果有很急的朋友們,可以先去參考看看中文的資料喔謝謝
參考資料
- http://www.windowsazure.com/en-us/manage/services/cache/net/how-to-cache-service/
- http://msdn.microsoft.com/zh-tw/library/WindowsAzure/dn448831.aspx#CacheServicePreviewProgramSignup
- http://msdn.microsoft.com/zh-tw/library/WindowsAzure/dn386142.aspx
- http://msdn.microsoft.com/zh-tw/library/WindowsAzure/dn448829.aspx#ConfigureNuGet