ASP.NET基本分布式缓存Memcached测试实例
本文首发:http://www.xueit.com/html/2009-11-12/21-932220455859.html
下面测试下分布式缓存Memcached软件,一直在学习关注大访问量网站的缓存是如何实现,之前看过Memcached的资料,忙于没有时间来真正测试一下,本文测试分布式缓存Memcached的环境如下:(两台电脑作为服务器)
第一台:
CPU:Inter(R) Pentium(R) 4 CPU 2.8G
内存:1G
系统:windows 7
IIS: IIS 7
IP:172.10.1.97
环境:本地
安装:memcached 1.2.1 for Win32
第二台:
CPU:Inter(R) Pentium(R) 4 CPU 3.0G
内存:2G
系统:windows Server 2003
IIS: IIS 6
IP:172.10.1.236
环境:远程
安装:memcached 1.2.1 for Win32
测试程序部署到本地环境(172.10.1.97),开发工具VS2008 .NET3.5
本文使用到memcached 1.2.1 for Win32下载地址:
http://jehiah.cz/projects/memcached-win32/
更多memcached版本大全请进入
http://www.xueit.com/html/2009-11-12/32-1550931594781.html
好了,下面我们按步骤来测试:
第一、首先到把下载好的memcached 1.2.1解压到C:\memcached目录,分别复制到两台服务器中。
第二、安装memcached服务,在命令提示符输入CD c:\memcached进入到memcached目录,如下图:

之后输入memcached -h 回车,看帮助说明,接下来输入memcached -d install 回车即可自动安装memcached服务了,如下图:

安装memcached服务图
安装好安装memcached服务后,输入memcached -d start 回车启动memcached服务,如下图:

启动memcached服务图
在172.10.1.97与172.10.1.236两台电脑都按以上操作来安装启动memcached。
第三、下载.NET版memcached客户端API组件来写测试程序。
本文使用memcacheddotnet,下载地址如下:
http://sourceforge.net/projects/memcacheddotnet/
下载好之后把这些文件Commons.dll,ICSharpCode.SharpZipLib.dll,log4net.dll,Memcached.ClientLibrary.dll放到bin目录(少一个都不行),之后再到测试项目开发环境引用Memcached.ClientLibrary.dll,如下图

引用Memcached.ClientLibrary.dll图
第四、测试程序:
| 以下为引用的内容: using System; using System.Collections; using System.Text; // 须引用Memcached using Memcached.ClientLibrary; namespace test { public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { if (Request["action"] == "clear") this.clear(); else this.test(); } } /// <summary> /// 清空缓存 /// </summary> public void clear() { string[] servers = { "172.10.1.97:11211", "172.10.1.236:11211" }; //初始化池 SockIOPool pool = SockIOPool.GetInstance(); pool.SetServers(servers); pool.InitConnections = 3; pool.MinConnections = 3; pool.MaxConnections = 5; pool.SocketConnectTimeout = 1000; pool.SocketTimeout = 3000; pool.MaintenanceSleep = 30; pool.Failover = true; pool.Nagle = false; pool.Initialize(); MemcachedClient mc = new Memcached.ClientLibrary.MemcachedClient(); mc.EnableCompression = false; mc.Delete("cache"); mc.Delete("endCache"); Response.Write("清空缓存成功"); } /// <summary> /// 测试缓存 /// </summary> public void test() { //分布Memcachedf服务IP 端口 string[] servers = { "172.10.1.97:11211","172.10.1.236:11211" }; //初始化池 SockIOPool pool = SockIOPool.GetInstance(); pool.SetServers(servers); pool.InitConnections = 3; pool.MinConnections = 3; pool.MaxConnections = 5; pool.SocketConnectTimeout = 1000; pool.SocketTimeout = 3000; pool.MaintenanceSleep = 30; pool.Failover = true; pool.Nagle = false; pool.Initialize(); //客户端实例 MemcachedClient mc = new Memcached.ClientLibrary.MemcachedClient(); mc.EnableCompression = false; StringBuilder sb = new StringBuilder(); //写入缓存 sb.AppendLine("写入缓存测试:"); sb.AppendLine("<br>_______________________________________<br>"); if (mc.KeyExists("cache")) { sb.AppendLine("缓存cache已存在"); } else { mc.Set("cache", "写入缓存时间:" DateTime.Now.ToString()); sb.AppendLine("缓存已成功写入到cache"); } sb.AppendLine("<br>_______________________________________<br>"); sb.AppendLine("读取缓存内容如下:<br>"); sb.AppendLine(mc.Get("cache").ToString()); //测试缓存过期 sb.AppendLine("<br>_______________________________________<br>"); if (mc.KeyExists("endCache")) { sb.AppendLine("缓存endCache已存在,过期时间为:" mc.Get("endCache").ToString()); } else { mc.Set("endCache", DateTime.Now.AddMinutes(1).ToString(), DateTime.Now.AddMinutes(1)); sb.AppendLine("缓存已更新写入到endCache,写入时间:" DateTime.Now.ToString() " 过期时间:" DateTime.Now.AddMinutes(1).ToString()); } //分析缓存状态 Hashtable ht = mc.Stats(); sb.AppendLine("<br>_______________________________________<br>"); sb.AppendLine("Memcached Stats:"); sb.AppendLine("<br>_______________________________________<br>"); foreach (DictionaryEntry de in ht) { Hashtable info = (Hashtable)de.Value; foreach (DictionaryEntry de2 in info) { sb.AppendLine(de2.Key.ToString() ": " de2.Value.ToString() "<br>"); } } Response.Write(sb.ToString()); } } |
第五、 运行看效果:

缓存效果图我在本地172.10.1.97运行memcached -d
stop来停止memcached服务,运行上面程序,一样正确,说明缓存也同样保存到远程172.10.1.236这台服务器了。这样简单就可以实现分布式缓存,使用缓存又多了一个选择,不必使用.NET自带的Application与cache了,访问量大的网站实现分布式缓存有很多好处。有什么问题请指正,下期再出其它教程。转载请注明文章来源及原始链接,谢谢合作!原文:http://www.xueit.com/html/2009-11-12/21-932220455859.html
附:
常用命名:
memcached -h 可以看到帮助,这里参数很少,应该会比较简单些
memcached 1.2.0
-p <num> port number to listen on
-s <file> unix socket path to listen on (disables network support)
-l <ip_addr> interface to listen on, default is INDRR_ANY
-d run as a daemon
-r maximize core file limit
-u <username> assume identity of <username> (only when run as root)
-m <num> max memory to use for items in megabytes, default is 64 MB
-M return error on memory exhausted (rather than removing items)
-c <num> max simultaneous connections, default is 1024
-k lock down all paged memory
-v verbose (print errors/warnings while in event loop)
-vv very verbose (also print client commands/reponses)
-h print this help and exit
-i print memcached and libevent license
-b run a managed instanced (mnemonic: buckets)
-P <file> save PID in <file>, only used with -d option
-f <factor> chunk size growth factor, default 1.25
-n <bytes> minimum space allocated for key+value+flags, default 48
.NET 应用2
前面说个memcached支持多种语言的client API,上发布的Enyim
- 下载:Enyim
- 建立项目,引用dll后,配置App.Config文件
<configuration>
<configsections>
<sectiongroup name="enyim.com">
<section name="memcached" type="Enyim.Caching.Configuration.MemcachedClientSection,Enyim.Caching"/>
</sectiongroup>
<enyim.com>
<memcached>
<servers>
<!-- put your own server(s) here-->
<add address="127.0.0.1" port="11211">
<add address="127.0.0.1" port="11212">
</servers>
<socketpool minpoolsize="10" maxpoolsize="100" connectiontimeout="00:10:00" deadtimeout="00:02:00"/>
</memcached>
</enyim.com>
</configsections>
</configuration> - 也可以通过工具MemCacheD Manager进行配置和管理
- 使用MemcachedClient
MemcachedClient mc = new MemcachedClient();
mc.FlushAll(); // Flush the cache for this example
mc.Store(StoreMode.Set, "key1" , object1);
浙公网安备 33010602011771号