Memcached原理:
lSocket 服务器端
l数据:键值对存储
l内存处理的算法:
•本质就是一个大的哈希表。key最大长度是255个字符。
•内存模型:Memcache预先将可支配的内存空间进行分区(Slab),每个分区里再分成多个块(Chunk)大小1MB,但同一个分区里:块的长度(bytes)是固定的。
•插入数据:查找适合自己长度的块,然后插入,会有内存浪费。
•LRU,闲置>过期 >最少访问
•惰性删除:它并没有提供监控数据过期的机制,而是惰性的,当查询到某个key数据时,如果过期那么直接抛弃。
l集群搭建原理:
•Memcache服务器端并没有提供集群功能,但是通过客户端的驱动程序实现了集群配置。
•客户端实现集群的原理:首先客户端配置多台集群机器的ip和端口的列表。然后客户端驱动程序在写入之前,首先对key做哈希处理得到哈希值后对总的机器的个数进行取余
然后就选择余数对应的机器。
C#操作Memcached: 首先引用数据集...
下面新建单例类,操作Memcached
public class MemcachedHelper
{
private static MemcachedClient mc = null;
private static MemcachedHelper instance = new MemcachedHelper();
/// <summary>
/// 单例化
/// </summary>
/// <returns></returns>
public static MemcachedHelper GetInstance()
{
return instance;
}
private MemcachedHelper()
{
}
static MemcachedHelper()
{
string[] serverlist = { "192.168.1.4:11211", "10.0.0.132:11211" };
//初始化池
SockIOPool 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();
mc.EnableCompression = false;
}
/// <summary>
/// 读取缓存
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public object GetCached(string key)
{
if (mc.KeyExists(key))
{
return mc.Get(key);
}
return null;
}
//写入缓存
public bool SetCached(string key, object value)
{
return mc.Set(key, value);
}
/// <summary>
/// 写入缓存,设置过期时间
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="dt"></param>
/// <returns></returns>
public bool SetCached(string key, object value, DateTime dt)
{
return mc.Set(key, value, dt);
}
/// <summary>
/// 删除缓存
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public bool DeleteCached(string key)
{
return mc.Delete(key);
}
}
{
private static MemcachedClient mc = null;
private static MemcachedHelper instance = new MemcachedHelper();
/// <summary>
/// 单例化
/// </summary>
/// <returns></returns>
public static MemcachedHelper GetInstance()
{
return instance;
}
private MemcachedHelper()
{
}
static MemcachedHelper()
{
string[] serverlist = { "192.168.1.4:11211", "10.0.0.132:11211" };
//初始化池
SockIOPool 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();
mc.EnableCompression = false;
}
/// <summary>
/// 读取缓存
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public object GetCached(string key)
{
if (mc.KeyExists(key))
{
return mc.Get(key);
}
return null;
}
//写入缓存
public bool SetCached(string key, object value)
{
return mc.Set(key, value);
}
/// <summary>
/// 写入缓存,设置过期时间
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="dt"></param>
/// <returns></returns>
public bool SetCached(string key, object value, DateTime dt)
{
return mc.Set(key, value, dt);
}
/// <summary>
/// 删除缓存
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public bool DeleteCached(string key)
{
return mc.Delete(key);
}
}
新建Common类 模拟Session
/// <summary>
/// 设置Session,如果已存在则更新Session内数据
/// </summary>
/// <param name="model"></param>
public static void SetSession(Model.User model)
{
string sessionID;
if (HttpContext.Current.Request.Cookies["sessionID"] != null)
{
sessionID = HttpContext.Current.Request.Cookies["sessionID"].Value;
}
else
{
sessionID = Guid.NewGuid().ToString(); // 新产生GUID
}
bool b = MemcachedHelper.GetInstance().SetCached(sessionID, model, DateTime.Now.AddMinutes(20));
HttpContext.Current.Response.Cookies["sessionID"].Value = sessionID;
}
/// <summary>
/// 获得Session内的User对象,如不存在返回null
/// </summary>
/// <returns></returns>
public static Model.User GetSession()
{
Model.User model = null;
if (HttpContext.Current.Request.Cookies["sessionID"] != null)
{
string sessionID = HttpContext.Current.Request.Cookies["sessionID"].Value;
model = MemcachedHelper.GetInstance().GetCached(sessionID) as Model.User;
}
return model;
}
/// <summary>
/// 移除Session
/// </summary>
public static void RemoveSession()
{
if (HttpContext.Current.Request.Cookies["sessionID"] != null)
{
string sessionID = HttpContext.Current.Request.Cookies["sessionID"].Value;
MemcachedHelper.GetInstance().DeleteCached(sessionID);
}
}
/// 设置Session,如果已存在则更新Session内数据
/// </summary>
/// <param name="model"></param>
public static void SetSession(Model.User model)
{
string sessionID;
if (HttpContext.Current.Request.Cookies["sessionID"] != null)
{
sessionID = HttpContext.Current.Request.Cookies["sessionID"].Value;
}
else
{
sessionID = Guid.NewGuid().ToString(); // 新产生GUID
}
bool b = MemcachedHelper.GetInstance().SetCached(sessionID, model, DateTime.Now.AddMinutes(20));
HttpContext.Current.Response.Cookies["sessionID"].Value = sessionID;
}
/// <summary>
/// 获得Session内的User对象,如不存在返回null
/// </summary>
/// <returns></returns>
public static Model.User GetSession()
{
Model.User model = null;
if (HttpContext.Current.Request.Cookies["sessionID"] != null)
{
string sessionID = HttpContext.Current.Request.Cookies["sessionID"].Value;
model = MemcachedHelper.GetInstance().GetCached(sessionID) as Model.User;
}
return model;
}
/// <summary>
/// 移除Session
/// </summary>
public static void RemoveSession()
{
if (HttpContext.Current.Request.Cookies["sessionID"] != null)
{
string sessionID = HttpContext.Current.Request.Cookies["sessionID"].Value;
MemcachedHelper.GetInstance().DeleteCached(sessionID);
}
}