首页 > 代码库 > Memcached 之 .NET(C#)实例分析
Memcached 之 .NET(C#)实例分析
一:Memcached的安装
step1. 下载memcache(http://jehiah.cz/projects/memcached-win32)的windows稳定版(这里我下载了memcached 1.2.1 for Win32 binaries (Dec 23, 2006) 这个版本),解压放某个盘下面,比如在c:\memcached
step2. 在终端(也即cmd命令界面)下输入 ‘c:\memcached\memcached.exe -d install’ 安装
step3. 再输入: ‘c:\memcached\memcached.exe -d start’ 启动。
PS: 以后memcached将作为windows的一个服务每次开机时自动启动。这样服务器端已经安装完毕了。
二: .NET memcached client library
下载文件:https://sourceforge.net/projects/memcacheddotnet/
将Commons.dll,ICSharpCode.SharpZipLib.dll,log4net.dll,Memcached.ClientLibrary.dll 等放到bin目录
引用Memcached.ClientLibrary.dll
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->using System;using System.Collections;using Memcached.ClientLibrary;using System.Text;namespace Memcache{ 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(); } } public void clear() { string[] servers = { "192.168.1.113:11211", "192.168.202.128: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("清空缓存成功"); } public void test() { //分布Memcachedf服务IP 端口 string[] servers = { "192.168.1.113:11211", "192.168.202.128: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()); } }}
Memcached 之 .NET(C#)实例分析
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。