.net 使用memcache做缓存
前段时间去一家公司面试,面试官问到我对缓存了解多少,因为我是做B/S开发的,所以把知道的都说了。比如:Application、Cache、页面缓存、文件缓存。然后面试官说“不止这些,还有呢?”,我后来想了想说“以前在做PHP开发的时候有memcache,不知道.NET里面有没有?”。然后那面试官突然接过话来说:“对呀,有空自己看下。”。哈,然后我今天才有空看。
1、下载memcached,我下载的是memcached-win32-1.4.4-14,然后就是安装,安装过程如下:
A、在 C:\Program Files 建立一个 memcached 目录,将解压缩的文件拷贝到此目录
B、在命令窗口执行:"C:\Program Files\memcached\memcached.exe" -d install
C、在命令窗口执行:"C:\Program Files\memcached\memcached.exe" -d start
这样算是安装成功了,完了看下服务(直接命令里面:services.msc)里面有没有memcache,是否已启动。下载地址:http://www.jb51.net/softs/44843.html
2、下载Memcached Providers 1.2,解压缩。下载地址:http://memcachedproviders.codeplex.com/releases/view/10468
3、新建一项目,将第2步解压缩之后的文件夹里面的Enyim.Caching.dll和MemcachedProviders.dll添加到引用。
说明:这两个DLL都可以用来管理缓存,我测试了。
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using MemcachedProviders.Cache; using Enyim.Caching; using Enyim.Caching.Memcached; using System.Text; namespace test { public partial class Index : System.Web.UI.Page { //private static MemcachedClient client = new MemcachedClient("enyim.com/memcached"); protected void Page_Load(object sender, EventArgs e) { //DistCache.Add("test", "hello,memcache"); //Object result = DistCache.Get("test"); //Response.Write(result.ToString()); Enyim.Caching.MemcachedClient mc = new Enyim.Caching.MemcachedClient(); mc.Store(StoreMode.Add, "h", "HI"); mc.Append("h", this.StringToByte("很好", Encoding.UTF8)); //mc.Remove("h"); Response.Write(mc.Get("h")); } private Byte[] StringToByte(String s, Encoding encoding) { return encoding.GetBytes(s); } private String ByteToString(Byte[] b, Encoding encoding) { return encoding.GetString(b); } } }
4、配置Web.config
<?xml version="1.0"?> <configuration> <configSections> <!-- Memcached --> <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> </configSections> <connectionStrings> <add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" /> </connectionStrings> <enyim.com> <memcached> <servers> <add address="127.0.0.1" port="11211"/> </servers> <socketPool minPoolSize="10" maxPoolSize="100" connectionTimeout="00:00:10" deadTimeout="00:02:00"/> </memcached> </enyim.com> <cacheProvider defaultProvider="MemcachedCacheProvider"> <providers> <add name="MemcachedCacheProvider" type="MemcachedProviders.Cache.MemcachedCacheProvider, MemcachedProviders" keySuffix="_WebApp_MemoryCached_" defaultExpireTime="2000"/> </providers> </cacheProvider> <system.web> <compilation debug="true" targetFramework="4.0" /> </system.web> </configuration>
5、新建个页面试试
A、引用using MemcachedProviders.Cache;
B、在Page_Load里面加入一下代码测试就行了
DistCache.Add("test", "hello,memcache"); Object result = DistCache.Get("test"); Response.Write(result.ToString());
参考网址:
http://www.cnblogs.com/binfire/archive/2011/06/20/2085079.html
http://blog.csdn.net/zhoufoxcn/article/details/6282099
注意:memcached本身就带有负载均衡的功能,至于怎么实现的,不需要我们去管,你只需要在服务器上安装memcached,然后配置下就可以了,比如:
<enyim.com> <memcached> <servers> <add address="192.168.1.17" port="11211"/> <add address="192.168.1.18" port="11211"/> </servers> <socketPool minPoolSize="10" maxPoolSize="100" connectionTimeout="00:00:10" deadTimeout="00:02:00"/> </memcached> </enyim.com>