在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
一:memcache 简介
二:memcache for windows 服务器端的软件下载与安装
三:如何用(有部分是转载别人的)
四:memcache客户端的安装(.net版本)
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using Enyim.Caching; using MemcachedProviders.Cache; using System.Threading; namespace MemcachedProject { public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { testMemcachedProviders(); } /// <summary> /// 测试Enyim.Caching(注意:Enyim.Caching只具有MemcachedProviders的一部分功能,后者更强大。还可以集成Log4Net日志框架) /// </summary> public void testEnyimCaching() { MemcachedClient client = new MemcachedClient("enyim.com/memcached"); //存值 --不带过期时间的存储,Memcached将根据LRU来决定过期策略 bool result = client.Store(Enyim.Caching.Memcached.StoreMode.Add, "name", "dinglang"); //带过期时间的缓存 //bool success = client.Store(StoreMode.Add, person.UserName, person, DateTime.Now.AddMinutes(10)); if (result) { Response.Write("成功存入缓存"); //取值 object name = client.Get("name"); if (name != null) { Response.Write("取出的值为:" + name); } else { Response.Write("取值失败"); } } else { Response.Write("存入缓存失败"); } } /// <summary> /// 使用MemcachedProviders客户端 /// </summary> public void testMemcachedProviders() { string key = "myName"; string value = "Dylan"; bool result = false; string val = string.Empty; #region 存/取最简单的数据类型 //如果缓存中没有,就尝试着去存入缓存 if (DistCache.Get(key) == null) { //DistCache.DefaultExpireTime = 1200;//缓存时间 result = DistCache.Add(key, value); //存数据 if (result) { //如果存入成功,就试着去取 Thread.Sleep(500); string ret = (string)DistCache.Get(key); //读数据 //Assert.AreEqual(value, ret); //验证 if (ret != null) { Response.Write(ret); Response.Write("<br/>"); } else { //取出来的值为null,直接移除该缓存对象 DistCache.Remove(key);//移除 // DistCache.RemoveAll();//移除所有 } } } else { //缓存中有,直接拿数据 string ret = (string)DistCache.Get(key); if (ret != null) { Response.Write(ret); Response.Write("<br/>"); } else { DistCache.Remove(key); } } #endregion #region 存/取一个Person对象 Person person = new Person() { Id = 007, Name = "Dylan" };//new 一个Person对象的实例 //如果缓存中没有,则尝试着放入缓存 if (DistCache.Get<Person>("myObj") == null) { result = DistCache.Add("myObj", person); if (result) { Thread.Sleep(500); val = DistCache.Get("myObj").ToString(); if (val != null) { Response.Write(val); Response.Write("<br/>"); } else { DistCache.Remove("myObj"); } } } else { //缓存中已经有该对象,就直接从缓存取 Person p = DistCache.Get<Person>("myObj"); val = person.ToString(); //也可以直接这样取 // val = DistCache.Get("myObj").ToString(); if (val != null) { Response.Write(val); Response.Write("<br/>"); } else { DistCache.Remove("myObj"); } } #endregion } } } Person类的定义代码: 这里需要说明的是如果我们需要向Memcached中添加自定义数据类型时,我们需要将该数据类型添加上[Serializable]标记。
web.config 配置信息: <?xml version="1.0"?> <!-- 有关如何配置 ASP.NET 应用程序的详细信息,请访问 http://go.microsoft.com/fwlink/?LinkId=169433 --> <configuration> <!--Enyim.Caching配置(省略了Log4Net框架) For Memcached--> <!--<configSections> <sectionGroup name="enyim.com"> <section name="memcached" type="Enyim.Caching.Configuration.MemcachedClientSection, Enyim.Caching" /> </sectionGroup> </configSections> <enyim.com protocol="Binary"> <memcached> <servers> <add address="127.0.0.1" port="11121" /> </servers> <socketPool minPoolSize="10" maxPoolSize="100" connectionTimeout="00:00:10" deadTimeout="00:02:00" /> </memcached> </enyim.com>--> <!--The Enyim.Caching配置 End --> <!--MemcachedProviders的配置 --> <configSections> <section name="cacheProvider" type="MemcachedProviders.Cache.CacheProviderSection, MemcachedProviders" allowDefinition="MachineToApplication" restartOnExternalChanges="true"/> <sectionGroup name="enyim.com"> <section name="memcached" type="Enyim.Caching.Configuration.MemcachedClientSection, Enyim.Caching" /> </sectionGroup> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/> </configSections> <cacheProvider defaultProvider="MemcachedCacheProvider"> <providers> <add name="MemcachedCacheProvider" type="MemcachedProviders.Cache.MemcachedCacheProvider, MemcachedProviders" keySuffix="_MySuffix_" defaultExpireTime="2000"/> </providers> </cacheProvider> <enyim.com> <memcached> <servers> <!-- put your own server(s) here--> <add address="127.0.0.1" port="11121" /> </servers> <socketPool minPoolSize="10" maxPoolSize="100" connectionTimeout="00:00:10" deadTimeout="00:02:00" /> </memcached> </enyim.com> <log4net> <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender"> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}]- %message%newline" /> </layout> </appender> <root> <priority value="WARN"/> <appender-ref ref="ConsoleAppender"> <filter type="log4net.Filter.LevelRangeFilter"> <levelMin value="WARN"/> <levelMax value="FATAL"/> </filter> </appender-ref> </root> </log4net> <!--The MemcachedProviders配置 End 配置结束,下面的都是一些其他的配置,例如连数据库之类的,和我们的 memcache 无关--> <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> </system.web> <system.webServer> <modules runAllManagedModulesForAllRequests="true"/> </system.webServer> </configuration> 五:其他的一些问题的解答
|
请发表评论