Step1:第一步当然是下载Memcached for c# API,
将这几个dll引用到项目中去
step2:上代码:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.Mvc; 6 using Memcached.ClientLibrary; 7 8 namespace MvcDemo.Controllers 9 { 10 public class MemCachedController : Controller 11 { 12 // 13 // GET: /MemCached/ 14 15 public ActionResult Index() 16 { 17 18 string[] servers = { "172.16.200.67:41211" }; 19 SockIOPool pool = SockIOPool.GetInstance("test"); 20 pool.SetServers(servers); 21 pool.Initialize(); 22 MemcachedClient memClient = new MemcachedClient(); 23 memClient.PoolName = "test"; 24 memClient.EnableCompression = false; 25 bool b= memClient.Set("k2", "lixiaofei",DateTime.Now.AddMinutes(2)); 26 ViewBag.Name= memClient.Get("k2"); 27 return Content(ViewBag.Name); 28 } 29 30 } 31 }
时间: 2024-10-07 03:11:25