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[] { });
//初始化时创建的连接数
_pool.InitConnections = ;
//最小连接数
_pool.MinConnections = ;
//最大连接数
_pool.MaxConnections = ;
//连接的最大空闲时间,下面设置为6个小时(单位ms),超过这个设置时间,连接会被释放掉
_pool.MaxIdle = * * * ;
//通讯的超时时间,下面设置为3秒(单位ms),.NET版本没有实现
_pool.SocketTimeout = * ;
//socket连接的超时时间,下面设置表示连接不超时,即一直保持连接状态
_pool.SocketConnectTimeout = ;
//是否对TCP/IP通讯使用Nalgle算法,.NET版本没有实现
_pool.Nagle = false;
//维护线程的间隔激活时间,下面设置为60秒(单位s),设置为0表示不启用维护线程
_pool.MaintenanceSleep = ;
//设置SocktIO池的故障标志
_pool.Failover = true;
//socket单次任务的最大时间,超过这个时间socket会被强行中断掉(当前任务失败)
_pool.MaxBusy = * ; _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", "", );
//取值
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)在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 = "";
pageInfo.pageSize = ;
IList<NoticeModel> noticelist = noticedao.getNoticeList(notice); //设置在内存中的保存时间
cache.Insert(name, noticelist, null, DateTime.Now.AddHours(), TimeSpan.Zero); return noticelist;
}
else
{
return list;
}

MemCache缓存和C#自带的Cache缓存的更多相关文章

  1. paip.cache 缓存架构以及性能提升总结

    paip.cache 缓存架构以及性能提升总结 1         缓存架构以及性能(贯穿读出式(LookThrough) 旁路读出式(LookAside) 写穿式(WriteThrough) 回写式 ...

  2. Nginx 负载均衡的Cache缓存批量清理的操作记录

    1)nginx.conf配置 [root@inner-lb01 ~]# cat /data/nginx/conf/nginx.conf user www; worker_processes 8; #e ...

  3. spring 缓存(spring自带Cache)(入门)源码解读

    spring自带的缓存类有两个基础类:Cache(org.springframework.cache.Cache)类,CacheManager(org.springframework.cache.Ca ...

  4. spring 缓存(spring自带Cache)(入门)

    spring的缓存机制,是方法纬度的缓存机制, 这就意味着我们并不用关注 底层是否使用了数据库以及通过什么方式访问的数据库: 因此,此缓存方法既适用于dao层,也适用于service层. spring ...

  5. 注释驱动的 Spring cache 缓存介绍

    概述 Spring 3.1 引入了激动人心的基于注释(annotation)的缓存(cache)技术,它本质上不是一个具体的缓存实现方案(例如 EHCache 或者 OSCache),而是一个对缓存使 ...

  6. [转]注释驱动的 Spring cache 缓存介绍

    原文:http://www.ibm.com/developerworks/cn/opensource/os-cn-spring-cache/ 概述 Spring 3.1 引入了激动人心的基于注释(an ...

  7. 注释驱动的 Spring cache 缓存介绍--转载

    概述 Spring 3.1 引入了激动人心的基于注释(annotation)的缓存(cache)技术,它本质上不是一个具体的缓存实现方案(例如 EHCache 或者 OSCache),而是一个对缓存使 ...

  8. tp5.带标签的缓存 创建和清除 测试

    原文:http://www.upwqy.com/details/24.html 测试设置了标签的缓存的获取方式 和清除标签缓存. 有时候我们可能会对同类型的一些数据做统一缓存.和统一清除更新处理. 那 ...

  9. 缓存系列之一:buffer、cache与浏览器缓存

    缓存系列之一:buffer.cache与浏览器缓存 一:缓存是为了调节速度不一致的两个或多个不同的物质的速度,在中间对速度较快的一方起到一个加速访问速度较慢的一方的作用,比如CPU的一级.二级缓存是保 ...

随机推荐

  1. c# appdomain

    http://www.cnblogs.com/Terrylee/archive/2005/11/28/285809.html

  2. Area of Simple Polygons

    poj1389:http://poj.org/problem?id=1389 题意:求矩形面积的并题解:扫描线加线段树 同poj1389 #include<iostream> #inclu ...

  3. Android Wear开发 - 卡片通知 - 第一节 : 添加Android Wear通知特性

    一. 前言说明 Android Wear大部分显示形式是卡片的形式,而最简单地支持Android Wear方式就是用通知**Notification**.而实现最简单的,非高度自定义的通知,则只需要在 ...

  4. 【HDOJ】2369 Broken Keyboard

    字符串. #include <cstdio> #include <cstring> ]; ]; int main() { int n, len; int i, j, k, tm ...

  5. Unity NGUI实现序列帧动画播放

    如题,要实现序列帧的播放导入图片的时候需要注意: (1)图片的命名要连续,如图: (2)将这些图片在NGUI中打包成Altas图集的时候图片应该在同一个Altas中: 这里以播放特效为例,满足条件时播 ...

  6. svn图形客户端:smartsvn,svnmanager,rapidsvn,svnworkbench,rabbitsvn,Esvn, trac

    svn图形客户端: smartsvn,http://www.oschina.net/p/smartsvn, 不用安装直接运行 qsvn, http://www.oschina.net/p/qsvn r ...

  7. 数据结构(树链剖分,堆):HNOI 2016 network

    2215. [HNOI2016]网络 ★★★☆   输入文件:network_tenderRun.in   输出文件:network_tenderRun.out   简单对比时间限制:2 s   内存 ...

  8. 数据结构(主席树):HDU 5654 xiaoxin and his watermelon candy

    Problem Description During his six grade summer vacation, xiaoxin got lots of watermelon candies fro ...

  9. server 2008 ftp 环境重点说明

    最近 在弄ftp  环境,但是 到server 2008 r2  这个系统之后,按照之前的方法 不行了 具体情况如下 利用本机 资源管理器 访问不了,根本不出现 登录框 提示 然后 到ftp  站点 ...

  10. Search Insert Position——LeetCode

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...