WindowsAzure 之AppFabricCache

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 visit http://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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值