在memcached中实体类型未经序列化不能在memcached中缓存,因此需要对实体类进行处理,才能缓存下来.
Memcached是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载。它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态、数据库驱动网站的速度。Memcached基于一个存储键/值对的hashmap。其守护进程(daemon )是用C写的,但是客户端可以用任何语言来编写,并通过memcached协议与守护进程通信。
我们可以使用Memcached缓存string类型等已经内部实现了序列化的类型,但是对于我们自定义的类型,我们并不能在Memcached中缓存下来,因为Memcached只能缓存序列化之后的数据,因此,在这里我们将自定义的实体类型序列化一下就可以在Memcached中存储了。
首先下载windows平台下的memcached,然后安装。安装完之后就是启动memcached服务了,你可以在cmd下用dos命令输入,也可以在计算机管理->服务->memcached->启动.来开启服务.
随后就是在项目中引入相关dll:
Commons.dll,ICSharpCode.SharpZipLib.dll,log4net.dll,Memcached.ClientLibrary.dll
在项目的引用中引入Memcached.ClientLibrary.dll
随后就是编写程序了,在这里创建一个MVC程序:
在Models文件夹中创建一个类:
[Serializable]public class VIP{public string UserName { get; set; }public int? Vip { get; set; }public DateTime? VipEndDate { get; set; }public string Mail { get; set; }public string QQ { get; set; }}
登录后复制
若没有标注为可序列化,则后续运行程序将会报错。
随后创建一个MemcachedHelper类来辅助编程.
public class MemcachedHelper{public static MemcachedClient mclient;static MemcachedHelper(){string[] serverlist = new string[] { "127.0.0.1:11211" };SockIOPool pool = SockIOPool.GetInstance("First");pool.SetServers(serverlist);pool.Initialize();mclient = new MemcachedClient();mclient.PoolName = "First";mclient.EnableCompression = false;}public static bool set(string key, object value, DateTime expiry){return mclient.Set(key, value, expiry);}public static object Get(string key){return mclient.Get(key);}}
登录后复制
最后就是Controller里面的具体实现:
public class EntityMemcachedController : Controller { // // GET: /EntityMemcached/ ////// 序列化实体类为字节数组,将其存储到Memcached中,以缓存数据,从而减轻访问压力.... /// /// public ActionResult Index() { var vipInfo = new List{ new VIP{ UserName="张三", Vip=1, QQ="3123456", Mail="3123456", VipEndDate=(DateTime?)DateTime.Now.AddDays(1) }, new VIP{ UserName="李四", Vip=1, QQ="4123456", Mail="4123456", VipEndDate=(DateTime?)DateTime.Now.AddDays(2) }, new VIP{ UserName="王五", Vip=1, QQ="5123456", Mail="5123456", VipEndDate=(DateTime?)DateTime.Now.AddDays(3) }, new VIP{ UserName="赵六", Vip=1, QQ="6123456", Mail="6123456", VipEndDate=(DateTime?)DateTime.Now.AddDays(4) }, new VIP{ UserName="刘七", Vip=1, QQ="7123456", Mail="7123456", VipEndDate=(DateTime?)DateTime.Now.AddDays(5) } }; if (Request.Cookies["_EntityMemcached"] == null) { string sessionId = Guid.NewGuid().ToString(); Response.Cookies["_EntityMemcached"].Value = sessionId; Response.Cookies["_EntityMemcached"].Expires = DateTime.Now.AddMinutes(1);//设置cookie过期时间 MemcachedHelper.set(sessionId, vipInfo, DateTime.Now.AddMinutes(1));//设置缓存过期时间 return Content("Memcached分布式缓存设置成功!!!"); } else { string key = Request.Cookies["_EntityMemcached"].Value.ToString(); object obj = MemcachedHelper.Get(key); List info = obj as List; if (info != null) { return View(info); } } return Content("若显示则有'bug'"); }
登录后复制
看看实现:
然后退出来,重新点击”实现memcached缓存”
我设置了一分钟之内的缓存,因此在这一分钟之内将一直是这个界面,不得不说memcached还是不错!后续接着研究OutputCached + Monogodb的缓存策略
以上就是高性能缓存系统(Memcached)的实例介绍的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2441143.html