缘起: 在数据驱动的web开发中,经常要重复从数据库中取出相同的数据,这种重复极大的增加了数据库负载。缓存是解决这个问题的好办法。但是ASP.NET中的虽然已经可以实现对页面局部进行缓存,但还是不够灵活。此时Memcached或许是你想要的。 里面有.net1.1 和 .net2.0的两种版本 还有一个不错的例子。 三 应用 1 将Commons.dll,ICSharpCode.SharpZipLib.dll,log4net.dll,Memcached.ClientLibrary.dll 等放到bin目录 2 引用Memcached.ClientLibrary.dll 3 代码 namespace Memcached.MemcachedBench { using System; using System.Collections; using Memcached.ClientLibrary; publicclass MemcachedBench { [STAThread] publicstaticvoid Main(String[] args) { string[] serverlist = { "10.0.0.131:11211", "10.0.0.132:11211" }; //初始化池 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; Console.WriteLine("------------测 试-----------"); mc.Set("test", "my value"); //存储数据到缓存服务器,这里将字符串"my value"缓存,key 是"test" if (mc.KeyExists("test")) //测试缓存存在key为test的项目 { Console.WriteLine("test is Exists"); Console.WriteLine(mc.Get("test").ToString()); //在缓存中获取key为test的项目 } else { Console.WriteLine("test not Exists"); } Console.ReadLine(); mc.Delete("test"); //移除缓存中key为test的项目 if (mc.KeyExists("test")) { Console.WriteLine("test is Exists"); Console.WriteLine(mc.Get("test").ToString()); } else { Console.WriteLine("test not Exists"); } Console.ReadLine(); SockIOPool.GetInstance().Shutdown(); //关闭池, 关闭sockets } } }4 运行结果 转载请保留固定链接: https://linuxeye.com/configuration/521.html |