安装
一 Memcached服务器端的安装 (此处将其作为系统服务安装)
下载文件:memcached 1.2.1 for Win32 binaries (Dec 23, 2006)
1 解压缩文件到c:\memcached
2 命令行输入 'c:\memcached\memcached.exe -d install'
3 命令行输入 'c:\memcached\memcached.exe -d start' ,该命令启动 Memcached ,默认监听端口为 11211
通过 memcached.exe -h 可以查看其帮助
系统中会添加下面的服务
二 .NET memcached client library
下载文件:https://sourceforge.net/projects/memcacheddotnet/
里面有.net1.1 和 .net2.0的两种版本 还有一个不错的例子。
版本更新很慢,在.Net 的开发技术中十分少用,首微软的开发解决方案上面不赞成此种共享内存方式。
代码
引用下面的组件
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using Memcached.ClientLibrary; using System.Collections; namespace MemcachedSample { public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { int runs = 100; int start = 200; //if (args.Length > 1) //{ // runs = int.Parse(args[0]); // start = int.Parse(args[1]); //} string[] serverlist = { "192.168.1.80:11211", "192.168.1.80:11211" }; //只能用IP // 初始缓存服务器 SockIOPool pool = SockIOPool.GetInstance(); pool.SetServers(serverlist); //连接数 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 MemcachedClient(); // 启用压缩 mc.EnableCompression = false; // 缓存Key string keyBase = "testKey"; string obj = "放到缓存中的内容对象"; long begin = DateTime.Now.Ticks; //循环runS - start 次 添加这么多次的块缓存,内容全是一个 for (int i = start; i < start + runs; i++) { mc.Set(keyBase + i, obj); //存储数据到缓存服务器,Key 是 testKey100,testKey101...... } long end = DateTime.Now.Ticks; long time = end - begin;//计算缓存用去的时间 Response.Write(runs + " 次的存储时间: " + new TimeSpan(time).ToString() + "ms<br />"); begin = DateTime.Now.Ticks; int hits = 0; int misses = 0; for (int i = start; i < start + runs; i++) { string str = (string)mc.Get(keyBase + i); //获取当前次数的缓存值 if (str != null) ++hits; //存在计数 else ++misses; //不存在计数,这种实力如果出现不存在就出问题了 } end = DateTime.Now.Ticks; time = end - begin; //计算时间 Response.Write(runs + " 获取缓存内容的时间: " + new TimeSpan(time).ToString() + "ms" + "<br />"); Response.Write("Cache hits: " + hits.ToString() + "<br />"); Response.Write("Cache misses: " + misses.ToString() + "<br />"); IDictionary stats = mc.Stats(); foreach (string key1 in stats.Keys) { Response.Write("配置地址做Key,也就是此地址当前的配置信息:"+key1 + "<br />而值也是一个名值对每个名是状态名,值是状态值:<br />"); Hashtable values = (Hashtable)stats[key1]; //地址的状态集合 foreach (string key2 in values.Keys) //遍历输出 { Response.Write(key2 + ":" + values[key2] + "<br />"); } Response.Write("<p />"); } // if (mc.KeyExists("test")) // { // Console.WriteLine("test is Exists"); // Console.WriteLine(mc.Get("test").ToString()); // } SockIOPool.GetInstance().Shutdown(); ////关闭池, 关闭sockets } } }
结果
代码下载:MemcachedSample.rar
冯瑞涛
分类:
[01] .Net X
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?