WindowsAzure 之AppFabricCache
1. 使用 windows azure平台帳號(Live ID)登陸 https://windows.azure.com/
2. 建立AppFabricCache
點擊 ”Service Bus, Access Control&Caching” 導航
選擇AppFabric 下的 Cache 節點,點擊New按鈕
輸入cache的命名空間,選擇地區,點擊Create Namespace
系統將顯示cache 正在activing ,大概需要15分鐘左右才能ready:
查看用戶端配置,這裡的your access token 和 yournamespace name將在下面的實驗中用到,千萬不要泄漏這兩項資訊,因為有了這兩項資訊就可以訪問(讀寫)cache 了,到時可是要付美元的哦:
3. 如何在程式中使用AppFabric Cache
a. Web.config 配置如下(注意替換YourNamespaceName和 YourAccessToken):
<?xml version="1.0"?><!-- For more information on how to configure your ASP.NET application, please visithttp://go.microsoft.com/fwlink/?LinkId=169433 --><configuration> <configSections> <!-- Append below entry to configSections. Do not overwrite the full section. --> <section name="dataCacheClients" type="Microsoft.ApplicationServer.Caching.DataCacheClientsSection, Microsoft.ApplicationServer.Caching.Core" allowLocation="true" allowDefinition="Everywhere"/> </configSections> <!-- Cache exposes one endpoint: one simple--> <dataCacheClients> <dataCacheClient name="default"> <hosts> <host name="YourNamespaceName.cache.windows.net" cachePort="22233" /> </hosts> <securityProperties mode="Message"> <messageSecurity authorizationInfo="YourAccessToken"> </messageSecurity> </securityProperties> </dataCacheClient> <dataCacheClient name="SslEndpoint"> <hosts> <host name="YourNamespaceName.cache.windows.net" cachePort="22243" /> </hosts> <securityProperties mode="Message" sslEnabled="true"> <messageSecurity authorizationInfo="YourAccessToken"> </messageSecurity> </securityProperties> </dataCacheClient> </dataCacheClients> <system.diagnostics> <trace> <listeners> <add type="Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener, Microsoft.WindowsAzure.Diagnostics, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="AzureDiagnostics"> <filter type="" /> </add> </listeners> </trace> </system.diagnostics> <connectionStrings> <!--<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" />--> </connectionStrings> <system.web> <compilation debug="true" targetFramework="4.0" /> <authentication mode="Forms"> <forms loginUrl="~/Account/Login.aspx" timeout="2880" /> </authentication> <membership> <providers> <clear/> <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" /> </providers> </membership> <profile> <providers> <clear/> <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/"/> </providers> </profile> <roleManager enabled="false"> <providers> <clear/> <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" /> <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" /> </providers> </roleManager> <!-- If session state needs to be saved in AppFabric Caching service, add the following to web.config inside system.web. If ssl is required, then change dataCacheClientName to "SslEndpoint". --> <sessionState mode="Custom" customProvider="AppFabricCacheSessionStoreProvider"> <providers> <add name="AppFabricCacheSessionStoreProvider" type="Microsoft.Web.DistributedCache.DistributedCacheSessionStateStoreProvider, Microsoft.Web.DistributedCache" cacheName="default" useBlobMode="true" dataCacheClientName="SslEndpoint" /> </providers> </sessionState> <!-- If output cache content needs to be saved in AppFabric Caching service, add the following to web.config inside 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> </system.web> <system.webServer> <modules runAllManagedModulesForAllRequests="true"/> </system.webServer></configuration>
b. 建立一個類,用於類比cache要存的資訊:
using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace WebRole1{ public class Person { public string ID; public string Name = string.Empty; public int Age = 25; }}
c. 用代碼存取cache:
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using Microsoft.WindowsAzure;using Microsoft.WindowsAzure.StorageClient;using Microsoft.WindowsAzure.ServiceRuntime;using System.Threading;using System.Data.SqlClient;using System.Data;using Microsoft.ApplicationServer.Caching;namespace WebRole1{ public partial class TestCache : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { Session["101"] = new Person { ID = "101", Name = "Abraham Cheng", Age = 26 }; using (DataCacheFactory dataCacheFactory = new DataCacheFactory()) { DataCache dataCache = dataCacheFactory.GetDefaultCache(); Person person = new Person { ID = "101", Name = "Abraham Cheng", Age = 26 }; dataCache.Put(person.ID.ToString(), person); } using (DataCacheFactory dataCacheFactory = new DataCacheFactory()) { DataCache dataCache = dataCacheFactory.GetDefaultCache(); Person person = (Person)dataCache.Get("101"); this.Response.Write("Get the person which ID is 101 from the cache:" + person.Name); } } } }}
d. 將程式部署到雲端(注意cache本地調試可能會有問題,本地無法解析cache的地址),效果如下
注: 如何建立並部署雲應用程式,請參考http://blog.csdn.net/farawayplace613/article/details/6933915