惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - Franky
N
Netflix TechBlog - Medium
Google Online Security Blog
Google Online Security Blog
月光博客
月光博客
量子位
酷 壳 – CoolShell
酷 壳 – CoolShell
V
V2EX
腾讯CDC
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
M
MIT News - Artificial intelligence
Vercel News
Vercel News
The GitHub Blog
The GitHub Blog
Hugging Face - Blog
Hugging Face - Blog
博客园 - 【当耐特】
Apple Machine Learning Research
Apple Machine Learning Research
aimingoo的专栏
aimingoo的专栏
博客园 - 三生石上(FineUI控件)
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
MongoDB | Blog
MongoDB | Blog
H
Help Net Security
The Cloudflare Blog
Blog — PlanetScale
Blog — PlanetScale
F
Full Disclosure
G
Google Developers Blog
罗磊的独立博客
Jina AI
Jina AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Y
Y Combinator Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
J
Java Code Geeks
A
About on SuperTechFans
IT之家
IT之家
大猫的无限游戏
大猫的无限游戏
S
SegmentFault 最新的问题
有赞技术团队
有赞技术团队
GbyAI
GbyAI
雷峰网
雷峰网
T
The Blog of Author Tim Ferriss
The Register - Security
The Register - Security
U
Unit 42
D
Docker
Martin Fowler
Martin Fowler
L
LINUX DO - 热门话题
NISL@THU
NISL@THU
阮一峰的网络日志
阮一峰的网络日志
C
Cybersecurity and Infrastructure Security Agency CISA
博客园_首页
Google DeepMind News
Google DeepMind News

博客园 - 蓝蓝的天2016

学习materialize VS2013 启用avalon 智能提示 Intelligence 推荐一款开源的C#TCP通讯框架 js/jquery 获取本地文件的文件路劲 获取input框中type=‘file’ 中的文件路径(转载) HTML5的 input:file上传类型控制(转载) tinymce4.x 上传本地图片 (转载) Jquery append()总结(一) 转载 Visual Studio各版本工程文件之间的转换 [转载] 创建数据库表(转载) 用IIS防止mdb数据库被下载(转载) 放映PPT幻灯片演示文稿如何让演讲者备注不投影到屏幕上(转载) 操作App.config的类(转载) 转载css3 图片圆形显示 如何CSS将正方形图片显示为圆形图片布局 微信 学习网址 winform 路径 CodeSimth - .Net Framework Data Provider 可能没有安装。解决方法[转载 ] 加载js代码 tinymce 上传图片空间(转) 页面跳转
C# MemoryCache 类[转载]
蓝蓝的天2016 · 2016-06-01 · via 博客园 - 蓝蓝的天2016

原网址:http://www.cmono.net/post/read/156

MemoryCache 类是.Net 4.0推出的类库,主要是为了方便在Winform和Wpf中构建缓存框架的
ObjectCache cache = MemoryCache.Default;    //得到MemoryCache全局实例
string myData = cache["mydata"] as string;  //访问缓存数据

if (myData == null)
{
    CacheItemPolicy policy = new CacheItemPolicy();  //创建缓存项策略

    //过期时间设置,以下两种只能设置一种
    policy.AbsoluteExpiration = new DateTimeOffset(DateTime.Now.AddMinutes(5)); //设定某个时间过后将逐出缓存
    policy.SlidingExpiration = new TimeSpan(0, 0, 10);    //设定某个时间段内未被访问将逐出缓存

    //逐出通知,以下两种只能设置一种
    policy.UpdateCallback = arguments => { Console.WriteLine("即将逐出缓存" + arguments.Key); };  //逐出前执行的方法
    //policy.RemovedCallback = arguments => { Console.WriteLine("已经逐出缓存" + arguments.CacheItem.Key); };  //逐出后执行的方法

    //缓存监视类有4种
    //CacheEntryChangeMonitor;
    //FileChangeMonitor;
    //HostFileChangeMonitor;
    //SqlChangeMonitor;
    //用法
    //List<string> filePaths = new List<string>();
    //filePaths.Add("c:\\cache\\example.txt");
    //policy.ChangeMonitors.Add(new HostFileChangeMonitor(filePaths));
    myData = "缓存数据"+DateTime.Now.ToShortTimeString();

     cache.Set("mydata", myData, policy);  //设置缓存数据,如果已存在则覆盖
}

Console.WriteLine("得到" + myData);

《C# MemoryCache 类》 由 常伟华 创作。 
本作品采用知识共享署名-相同方式共享 4.0 国际许可协议进行许可。