String[] serverlist = { "192.168.1.100:11211",
"192.168.1.101:11211" };
// initialize the pool for memcache servers
SockIOPool pool = SockIOPool.GetInstance("test");
pool.SetServers(serverlist);
pool.Initialize();
mc = new MemcacheClient();
mc.PoolName = "test";
mc.EnableCompression = false;
pool.Shutdown();//关闭连接池
using Memcached.ClientLibrary;
using System;
namespace PMS.Common
{
public class MemcacheHelper
{
private static readonly MemcachedClient Mc = null;
static MemcacheHelper()
{
//最好放在配置文件中
string[] serverlist = { "127.0.0.1:11211", "10.0.0.132:11211" };
//初始化池
var pool = SockIOPool.GetInstance();
pool.SetServers(serverlist);
pool.InitConnections = 3;
pool.MinConnections = 3;
pool.MaxConnections = 5;
pool.SocketConnectTimeout = 1000;
pool.SocketTimeout = 3000;
pool.MaintenanceSleep = 30;
pool.Failover = true;
pool.Nagle = false;
pool.Initialize();
// 获得客户端实例
Mc = new MemcachedClient {EnableCompression = false};
}
/// <summary>
/// 存储数据
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
public static bool Set(string key,object value)
{
return Mc.Set(key, value);
}
public static bool Set(string key, object value,DateTime time)
{
return Mc.Set(key, value,time);
}
/// <summary>
/// 获取数据
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static object Get(string key)
{
return Mc.Get(key);
}
/// <summary>
/// 删除
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static bool Delete(string key)
{
return Mc.KeyExists(key) && Mc.Delete(key);
}
}
}
public ActionResult UserLogin()
{
#region 验证码校验
var validateCode = Session["validateCode"] != null ? Session["validateCode"].ToString() : string.Empty;
if (string.IsNullOrEmpty(validateCode))
return Content("no:验证码错误!!");
Session["validateCode"] = null;
var txtCode = Request["ValidateCode"];
if (!validateCode.Equals(txtCode, StringComparison.InvariantCultureIgnoreCase))
return Content("no:验证码错误!!");
#endregion
var userName = Request["UserName"];
var userPwd = Request["PassWord"];
//查询用户是否存在
var user = UserService.LoadEntities(u => u.UserName == userName && u.PassWord == userPwd).FirstOrDefault();
if (user == null) return Content("no:登录失败");
//产生一个GUID值作为Memache的键.
var sessionId = Guid.NewGuid().ToString();
//将登录用户信息存储到Memcache中。
MemcacheHelper.Set(sessionId, SerializeHelper.SerializeToString(user), DateTime.Now.AddMinutes(20));
//将Memcache的key以Cookie的形式返回给浏览器。
Response.Cookies["sessionId"].Value = sessionId;
return Content("ok:登录成功");
}
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
//if (Session["user"] == null)
if (Request.Cookies["sessionId"] != null)
{
var sessionId = Request.Cookies["sessionId"].Value;
//根据该值查Memcache.
var obj = MemcacheHelper.Get(sessionId);
if (obj == null)
{
filterContext.Result = Redirect("/Login/Index");
return;
}
var user = SerializeHelper.DeserializeToObject<User>(obj.ToString());
LoginUser = user;
//模拟出滑动过期时间.
MemcacheHelper.Set(sessionId, obj, DateTime.Now.AddMinutes(20));
}
else
filterContext.Result = Redirect("/Login/Index");
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有