memcache的分布式配置

public static class MemcacheHelper
{
private static MemcachedClient mc;

static MemcacheHelper()
{
String[] serverlist = { "127.0.0.1:11213", "127.0.0.1:11214" };
SockIOPool pool = SockIOPool.GetInstance("MyMemcached");
pool.SetServers(serverlist);
//各服务器之间负载均衡的设置比例
pool.SetWeights(new int[] { 1, 1 });
//初始化时创建连接数
pool.InitConnections = 3;
//最小连接数
pool.MinConnections = 3;
//最大连接数
pool.MaxConnections = 5;
//连接的最大空闲时间,下面设置为6个小时(单位ms),超过这个设置时间,连接会被释放掉
pool.MaxIdle = 1000 * 60 * 60 * 6;
//socket连接的超时时间,下面设置表示不超时(单位ms),即一直保持链接状态
pool.SocketConnectTimeout = 0;
//通讯的超市时间,下面设置为3秒(单位ms),.Net版本没有实现
pool.SocketTimeout = 1000 * 3;
//维护线程的间隔激活时间,下面设置为30秒(单位s),设置为0时表示不启用维护线程
pool.MaintenanceSleep = 30;
//设置SocktIO池的故障标志
pool.Failover = true;
//是否对TCP/IP通讯使用nalgle算法,.net版本没有实现
pool.Nagle = false;
//socket单次任务的最大时间(单位ms),超过这个时间socket会被强行中端掉,当前任务失败。
pool.MaxBusy = 1000 * 10;
pool.Initialize();
mc = new MemcachedClient();
mc.PoolName = "MyMemcached";
mc.EnableCompression = false;
}

public static bool Set(string key, object value, DateTime expiry)
{
return mc.Set(key, value, expiry);
}

public static object Get(string key)
{
return mc.Get(key);
}

public static bool Delete(string key)
{
return mc.Delete(key);
}

public static bool KeyExists(string key)
{
return mc.KeyExists(key);
}
}

posted @ 2017-02-15 09:34  菜鸟飞飞飞  阅读(351)  评论(0编辑  收藏  举报