分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 教程案例

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

发布时间:2023-09-06 01:28责任编辑:苏小强关键词:.NETMVC缓存

首先下载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]
publicclassVIP
{
publicstringUserName {get;set; }
publicint? Vip {get;set; }
publicDateTime? VipEndDate {get;set; }
publicstringMail {get;set; }
publicstringQQ {get;set; }
}

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

随后创建一个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
publicclassMemcachedHelper
{
publicstaticMemcachedClient mclient;
staticMemcachedHelper()
{
string[] serverlist =newstring[] {"127.0.0.1:11211"};
SockIOPool pool = SockIOPool.GetInstance("First");
pool.SetServers(serverlist);
pool.Initialize();
mclient =newMemcachedClient();
mclient.PoolName ="First";
mclient.EnableCompression =false;
}
publicstaticboolset(stringkey,objectvalue, DateTime expiry)
{
returnmclient.Set(key, value, expiry);
}
publicstaticobjectGet(stringkey)
{
returnmclient.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
publicclassEntityMemcachedController : Controller
{
//
// GET: /EntityMemcached/
/// <summary>
/// 序列化实体类为字节数组,将其存储到Memcached中,以缓存数据,从而减轻访问压力....
/// </summary>
/// <returns></returns>
publicActionResult 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 = objasList<VIP>;
if(info !=null)
{
returnView(info);
}
}
returnContent("若显示则有‘bug‘");
}

看看实现:

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

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

原文地址:http://www.cnblogs.com/momjs/p/7975693.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved