高性能缓存系统Memcached在ASP.NET MVC中应用

首先下载windows平台下的memcached,然后安装。安装完之后就是启动memcached服务了,你可以在cmd下用dos命令输入,也可以在计算机管理->服务->memcached->启动.来开启服务.

随后就是在项目中引入相关dll:
Commons.dll,ICSharpCode.SharpZipLib.dll,log4net.dll,Memcached.ClientLibrary.dll
在项目的引用中引入Memcached.ClientLibrary.dll

随后就是编写程序了,在这里创建一个MVC程序:
在Models文件夹中创建一个类:

1
2
3
4
5
6
7
8
9
10
11
12
13
[Serializable]
public class VIP
{
public string UserName { getset; }
 
public int? Vip { getset; }
 
public DateTime? VipEndDate { getset; }
 
public string Mail { getset; }
 
public string QQ { getset; }
}

若没有标注为可序列化,则后续运行程序将会报错。

随后创建一个MemcachedHelper类来辅助编程.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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里面的具体实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
public class EntityMemcachedController : Controller
    {
        //
        // GET: /EntityMemcached/
        /// <summary>
        /// 序列化实体类为字节数组,将其存储到Memcached中,以缓存数据,从而减轻访问压力....
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            var vipInfo = new List<VIP>{
                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<VIP> info = obj as List<VIP>;
 
                if (info != null)
                {
                    return View(info);
                }
 
            }
 
            return Content("若显示则有'bug'");
        }

看看实现:

然后退出来,重新点击”实现memcached缓存”

 

posted @ 2017-12-04 09:57  fightingyy  阅读(373)  评论(0编辑  收藏  举报