First download the memcached under Windows platform and install it. After the installation is started memcached service, you can use the DOS command under CMD input, you can also be in the Computer Management, service->memcached-> start. To start the service.
This is followed by the introduction of the associated DLL in the project:
Commons.dll,icsharpcode.sharpziplib.dll,log4net.dll,memcached.clientlibrary.dll
Introduction of Memcached.ClientLibrary.dll in project references
Then you write the program, where you create an MVC program:
Create a class in the Models folder:
| 12345678910111213 |
[Serializable]publicclassVIP{public stringUserName { get; set; }publicint? Vip { get; set; }publicDateTime? VipEndDate { get; set; }publicstringMail { get; set; }publicstringQQ { get; set; }} |
If the label is not serializable, then the subsequent run program will error.
A Memcachedhelper class is then created to aid programming.
| 123456789101112131415161718192021222324252627 |
publicclassMemcachedHelper{publicstaticMemcachedClient mclient;staticMemcachedHelper(){string[] serverlist = newstring[] { "127.0.0.1:11211"};SockIOPool pool = SockIOPool.GetInstance("First");pool.SetServers(serverlist);pool.Initialize();mclient = new MemcachedClient();mclient.PoolName = "First";mclient.EnableCompression = false;}publicstaticboolset(stringkey, objectvalue, DateTime expiry){returnmclient.Set(key, value, expiry);}publicstaticobjectGet(stringkey){return mclient.Get(key);}} |
The final is the controller inside the specific implementation:
| 123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
publicclassEntityMemcachedController : Controller { // // GET: /EntityMemcached/ /// <summary> /// 序列化实体类为字节数组,将其存储到Memcached中,以缓存数据,从而减轻访问压力.... /// </summary> /// <returns></returns> public ActionResult Index() { varvipInfo = newList<VIP>{ newVIP{ UserName="张三", Vip=1, QQ="3123456", Mail="3123456", VipEndDate=(DateTime?)DateTime.Now.AddDays(1) }, newVIP{ UserName="李四", Vip=1, QQ="4123456", Mail="4123456", VipEndDate=(DateTime?)DateTime.Now.AddDays(2) }, newVIP{ UserName="王五", Vip=1, QQ="5123456", Mail="5123456", VipEndDate=(DateTime?)DateTime.Now.AddDays(3) }, newVIP{ UserName="赵六", Vip=1, QQ="6123456", Mail="6123456", VipEndDate=(DateTime?)DateTime.Now.AddDays(4) }, newVIP{ UserName="刘七", Vip=1, QQ="7123456", Mail="7123456", VipEndDate=(DateTime?)DateTime.Now.AddDays(5) } }; if(Request.Cookies["_EntityMemcached"] == null) { stringsessionId = 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));//设置缓存过期时间 returnContent("Memcached分布式缓存设置成功!!!"); } else { stringkey = Request.Cookies["_EntityMemcached"].Value.ToString(); objectobj = MemcachedHelper.Get(key); List<VIP> info = obj asList<VIP>; if(info != null) { returnView(info); } } returnContent("若显示则有‘bug‘"); } |
See the implementation:
Then exit and re-click "Implement Memcached Cache"
Application of high performance cache system memcached in ASP.