public static class MemCacheHelper
{
private static MemcachedClient mc;
static MemCacheHelper()
{
string[] serverlist = { "192.168.3.2: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 static object Get(string key)
{
return mc.Get(key);
}
/// <summary>
/// 向缓存中插入值
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public static void Insert(string key,object value)
{
mc.Set(key, value);
}
/// <summary>
/// 设置过期时间
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="datetime"></param>
public static void Insert(string key, object value, DateTime datetime)
{
mc.Set(key, value, datetime);
}
/// <summary>
/// 删除缓存中的值
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static bool Delete(string key)
{
if (mc.KeyExists(key))
{
return mc.Delete(key);
}
return false;
}