MemCache缓存和C#自带的Cache缓存

 1、MemCache

//初始化
static SockIOPool _pool;
// 创建Memcached private static MemcachedClient Create(string poolName) { CreateServer("abc11666", "127.0.0.1:11666");//创建了一个Memcache客户端的代理类。 MemcachedClient mc = new MemcachedClient(); mc.PoolName = poolName; mc.EnableCompression = false;//是否压缩 return mc; } // 配置Memcached private static void CreateServer(string poolName, string ip) {
if (_pool != null) { } else { ArrayList serverlist = new ArrayList(); CacheUtils c = new CacheUtils(); serverlist.Add(ip); //初始化memcache服务器池 _pool = SockIOPool.GetInstance(poolName); //设置Memcache池连接点服务器端。 _pool.SetServers(serverlist); //各服务器之间负载均衡的设置 _pool.SetWeights(new int[] { 1 }); //初始化时创建的连接数 _pool.InitConnections = 5; //最小连接数 _pool.MinConnections = 40; //最大连接数 _pool.MaxConnections = 5000; //连接的最大空闲时间,下面设置为6个小时(单位ms),超过这个设置时间,连接会被释放掉 _pool.MaxIdle = 1000 * 60 * 60 * 6; //通讯的超时时间,下面设置为3秒(单位ms),.NET版本没有实现 _pool.SocketTimeout = 1000 * 3; //socket连接的超时时间,下面设置表示连接不超时,即一直保持连接状态 _pool.SocketConnectTimeout = 0; //是否对TCP/IP通讯使用Nalgle算法,.NET版本没有实现 _pool.Nagle = false; //维护线程的间隔激活时间,下面设置为60秒(单位s),设置为0表示不启用维护线程 _pool.MaintenanceSleep = 60; //设置SocktIO池的故障标志 _pool.Failover = true; //socket单次任务的最大时间,超过这个时间socket会被强行中断掉(当前任务失败) _pool.MaxBusy = 1000 * 10; _pool.Initialize(); } } // 获取 public static object GetCache(string poolName, string key) { MemcachedClient mc = Create(poolName); if (mc.KeyExists(key)) { return mc.Get(key); } else { return ""; } } // 保存 public static bool SetCache(string poolName, string key, string value, int minutes) { MemcachedClient mc = Create(poolName); return mc.Set(key, value, DateTime.Now.AddMinutes(minutes)); } // 删除 public static bool DelCache(string poolName, string key) { MemcachedClient mc = Create(poolName); return mc.Delete(key); }

调用:

//保存
SetCache("abc11666", "abc", "123", 1440);
//取值
GetCache("abc11666","abc").ToString();
//删除
DelCache("abc11666", "abc");


//将ArrayList转换成IList<NoticeModel> 
IList<NoticeModel> noticelist = new List<NoticeModel>();
//获取,取出的格式是ArrayList 
ArrayList a1 = cacheUtils.GetCache("notice");
//方法一
noticelist = a1.Cast<NoticeModel>().ToList();
//方法二
foreach (var item in a1)
{
    noticelist.Add(item as NoticeModel);
}

2、memcached分布式缓存的设置与应用

string[] servers = { "127.0.0.1:11666", "127.0.0.1:11667" };
//初始化池
SockIOPool pool = SockIOPool.GetInstance();
//设置服务器列表
pool.SetServers(servers);
//各服务器之间负载均衡的设置比例
pool.SetWeights(new int[] { 1, 10 });

注:1)在172.18.5.66,与192.168.10.121两台机器上装 memcached 服务端。
  2)pool.SetWeights 这里的1跟10意思是,负载均衡比例,假如11000条数据,大致数据分布为:172.18.5.66分布1000条数据左右。另外一台为10000条左右。
  3)memcached 服务端并不具备负载均衡的能力,而是 memcachedClient 实现的,具体存取数据实现的核心是采用一致性Hash算法,把 key-value 分布到某一台服务器中里边。

3、C#自带的Cache缓存

noticelist 存入缓存的前提是,model 必须是可序列化的,在 model 上面必须添加此特性:[System.Serializable]

//name是key
string name = "notice";
Cache cache = HttpRuntime.Cache;
//如果是DataSet类型 DataSet dst = (DataSet)cache.Get("CachedDataSet");
IList<NoticeModel> list = (IList<NoticeModel>)cache.Get(name);
//判断是否为空,如果为空,说明没有缓存存入或者缓存已经过期了。
if (list == null)
{
    NoticeModel notice = new NoticeModel();
    NoticeDAO noticedao = new NoticeDAO();
    //公告
    notice.type = "1";
    pageInfo.pageSize = 4;
    IList<NoticeModel> noticelist = noticedao.getNoticeList(notice);

    //设置在内存中的保存时间
    cache.Insert(name, noticelist, null, DateTime.Now.AddHours(1), TimeSpan.Zero);

    return noticelist;
}
else
{
    return list;
}

 

posted @ 2016-07-05 12:51    阅读(1188)  评论(0编辑  收藏  举报