MemoryCache缓存 ---缓存时效
MemoryCache缓存 ---缓存时效测试
var cachePool = new MyCachePool();
//Thread.Sleep(1000);
var value = cachePool.GetFileValue();
/// <summary>
/// MemoryCache缓存
/// </summary>
public class MyCachePool
{
ObjectCache cache = MemoryCache.Default;
const string cacheKey = "TestCacheKey";
public string GetValue()
{
var content = cache[cacheKey] as string;
if (content == null)
{
//Console.WriteLine("Get New Item"); var policy = new CacheItemPolicy() { AbsoluteExpiration = DateTime.Now.AddSeconds() };
content = Guid.NewGuid().ToString();
cache.Set(cacheKey, content, policy);
}
else
{
Console.WriteLine("Get cached item");
} return content;
}
public string GetFileValue()
{
string strCacheKey = "FileCacheKey";
var content = cache[strCacheKey] as string;
if (content == null)
{
//Console.WriteLine("Get New Item"); //var file = @"E:\test.txt";
//CacheItemPolicy policy = new CacheItemPolicy();
//policy.ChangeMonitors.Add(new HostFileChangeMonitor(new List<string> { file })); //content = File.ReadAllText(file);
//cache.Set(strCacheKey, content, policy); CacheItemPolicy policy = new CacheItemPolicy();
policy.AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(); content = Guid.NewGuid().ToString(); CacheItem item = new CacheItem("cachedText", content); List<string> keys = new List<string> { strCacheKeyChange }; policy.ChangeMonitors.Add(cache.CreateCacheEntryChangeMonitor(keys)); //依赖某个值变化 cache.Set(item, policy);
}
else
{
Console.WriteLine("Get cached item");
} return content;
}
}
缓存用起来也就是通过Key来增删改查,内存缓存还可以在config中的配置对内存的使用情况
/// <summary>
/// 从内存缓存中读取配置。若缓存中不存在,则重新从文件中读取配置,存入缓存
/// </summary>
/// <param name="cacheKey">缓存Key</param>
/// <returns>配置词典</returns>
private static Dictionary<string, string> GetConfigDictionary(string cacheKey)
{
Dictionary<string, string> configs = null; //1、获取内存缓存对象
ObjectCache cache = MemoryCache.Default; //2、通过Key判断缓存中是否已有词典内容(Key在存入缓存时设置)
if (cache.Contains(cacheKey))
{
//3、直接从缓存中读取词典内容
configs = cache.GetCacheItem(cacheKey).Value as Dictionary<string, string>;
}
else
{
//3、读取配置文件,组成词典对象,准备放到缓存中
configs = GetFromXml(); //4、检查是否读取到配置内容
if (configs != null)
{
//4、新建一个CacheItemPolicy对象,该对象用于声明配置对象在缓存中的处理策略
CacheItemPolicy policy = new CacheItemPolicy(); //5、因为配置文件一直需要读取,所以在此设置缓存优先级为不应删除
// 实际情况请酌情考虑,同时可以设置AbsoluteExpiration属性指定过期时间
policy.Priority = CacheItemPriority.NotRemovable; //6、将词典内容添加到缓存,传入 缓存Key、配置对象、对象策略
// Set方法首先会检查Key是否在缓存中存在,如果存在,更新value,不存在则创建新的
// 这里先加入缓存再加监视的原因是:在缓存加入时,也会触发监视事件,会导致出错。
cache.Set(cacheKey, configs, policy); //7、监视文件需要传入一个IList对象,所以即便只有一个文件也需要新建List对象
List<string> filePaths = new List<string>() { "c:\config.xml" }; //8、新建一个文件监视器对象,添加对资源文件的监视
HostFileChangeMonitor monitor = new HostFileChangeMonitor(filePaths); //9、调用监视器的NotifyOnChanged方法传入发生改变时的回调方法
monitor.NotifyOnChanged(new OnChangedCallback((o) =>
{
cache.Remove(cacheKey);
}
)); //10、为配置对象的缓存策略加入监视器
policy.ChangeMonitors.Add(monitor);
}
}
return configs;
}
MemoryCache缓存 ---缓存时效的更多相关文章
- 基于MemoryCache的缓存辅助类
背景: 1. 什么是MemoryCache? memoryCache就是用电脑内存做缓存处理 2.使用范围? 可用于不常变的数据,进行保存在内存中,提高处理效率 代码: /// <summary ...
- 在.NET项目中使用PostSharp,使用MemoryCache实现缓存的处理(转)
在之前一篇随笔<在.NET项目中使用PostSharp,实现AOP面向切面编程处理>介绍了PostSharp框架的使用,试用PostSharp能给我带来很多便利和优势,减少代码冗余,提高可 ...
- 在.NET项目中使用PostSharp,使用MemoryCache实现缓存的处理
在之前一篇随笔<在.NET项目中使用PostSharp,实现AOP面向切面编程处理>介绍了PostSharp框架的使用,试用PostSharp能给我带来很多便利和优势,减少代码冗余,提高可 ...
- .Net Core缓存组件(MemoryCache)【缓存篇(二)】
一.前言 .Net Core缓存源码 1.上篇.NET Core ResponseCache[缓存篇(一)]中我们提到了使用客户端缓存.和服务端缓存.本文我们介绍MemoryCache缓存组件,说到服 ...
- EhCache RMI 分布式缓存/缓存集群
EhCache 系统简介 EhCache 是一个纯 Java 的进程内缓存框架,具有快速.精干等特点. EhCache 的主要特性有: 快速.精干 简单: 多种缓存策略: 缓存数据有两级:内存和磁盘, ...
- EhCache 分布式缓存/缓存集群
开发环境: System:Windows JavaEE Server:tomcat5.0.2.8.tomcat6 JavaSDK: jdk6+ IDE:eclipse.MyEclipse 6.6 开发 ...
- EhCache 分布式缓存/缓存集群(转)
开发环境: System:Windows JavaEE Server:tomcat5.0.2.8.tomcat6 JavaSDK: jdk6+ IDE:eclipse.MyEclipse 6.6 开发 ...
- HTTP缓存缓存机制
http协议无状态,所以缓存设定从两方面考虑.客户端浏览器和服务器端. 浏览器端实现过期机制. 服务器端实现验证机制. 缓存机制. 为了减轻服务器负担,也减少网络传输数量.http1.0定义了Expi ...
- Unity3D性能优化小tips——把this.transform缓存缓存起来
Unity3D开发时中有一个小tips,这在官方的文档里其实有提及的,但不那么显眼,这里小说一下: 在MonoBehaviour进行编程时,我们经常会用this.transform, this.gam ...
随机推荐
- CENTOS 7 升级安装 Python 3.5
写在前面的话 本文采取源码的方式安装 Python 3.5.2,如果是其它版本会有或多或少的差异,且写这篇的时候官网最新的是 Python 3.7,个人使用 3.5 就足够了,没必要更新到最新,否则出 ...
- POJ - 2528Mayor's posters (离散化+线段树区间覆盖)
The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign h ...
- 转载文章 MySQL与Oracle的区别
MySQL与Oracle的区别 1. Oracle是大型数据库而Mysql是中小型数据库,Oracle市场占有率达40%,Mysql只有20%左右,同时Mysql是开源的而Oracle价格非常高 ...
- Python集合字典运算符
1.集合2.字典3.运算符优先级 1.集合 创建:{} set([]) 注意:创建空的集合要用set() 特点:元素唯一,无序 运算: & 交集 | 并集 - 差集 方法: s ...
- ssh免密登录linux服务器
Ssh免密登录 sshd服务 sshd简介: SSH 密钥为登录 Linux 服务器提供了更好且安全的机制.运行 ssh-keygen 后,将会生成公私密钥对.你可以将公钥放置到任意服务器,从持有私钥 ...
- 奇妙的 clip-path 几何图形
CSS 新属性 clip-path,意味裁剪路径的意思,让我们可以很便捷的生成各种几何图形. clip-path 通过定义特殊的路径,实现我们想要的图形.而这个路径,正是 SVG 中的 path . ...
- Luogu P5201 [USACO19JAN]Shortcut 最短路树???
最短路树...开眼界了...之前想也没想过.... 先跑出来1到每个点最短路,然后建树时要标记点的入度,否则会多连边...然后深搜时更新新答案就是 #include<cstdio> #in ...
- rest_framework 的验证,权限,频率
回到顶部 快速实例 Quickstart 回到顶部 序列化 创建一个序列化类 简单使用 开发我们的Web API的第一件事是为我们的Web API提供一种将代码片段实例序列化和反序列化为诸如json之 ...
- NETSpider 网络蜘蛛采集工具
NETSpider网站数据采集软件是一款基于.Net平台的开源软件.软件部分功能是基本Soukey软件进行开发的.这个版本采用VS2010+.NET3.5进行开发的.NETSpider采摘当前提供的主 ...
- div css 伪类 不固定图片大小 居中, css div 实现三角形
div css 伪类 不固定图片大小 居中 <style> .pic_box{width:300px; height:300px; background-color:#beceeb; fo ...