System.Runtime.Caching中MemoryCache帮助类
值得参考的几个内存缓存帮助类:
参考资料:
https://github.com/Hendy/memory-cache-helper
https://gist.github.com/jdalley/0cc8caed02845ad5a6006e4db1267720
https://gist.github.com/jwcarroll/4060539
https://github.com/khalidsalomao/SimpleHelpers.Net/blob/master/SimpleHelpers/MemoryCache.cs
public static class CacheHelper
{
private static readonly Object _locker = new object(); public static T GetCacheItem<T>(String key, Func<T> cachePopulate, TimeSpan? slidingExpiration = null, DateTime? absoluteExpiration = null)
{
if(String.IsNullOrWhiteSpace(key)) throw new ArgumentException("Invalid cache key");
if(cachePopulate == null) throw new ArgumentNullException("cachePopulate");
if(slidingExpiration == null && absoluteExpiration == null) throw new ArgumentException("Either a sliding expiration or absolute must be provided"); if(MemoryCache.Default[key] == null)
{
lock(_locker)
{
if(MemoryCache.Default[key] == null)
{
var item = new CacheItem(key, cachePopulate());
var policy = CreatePolicy(slidingExpiration, absoluteExpiration); MemoryCache.Default.Add(item, policy);
}
}
} return (T)MemoryCache.Default[key];
} private static CacheItemPolicy CreatePolicy(TimeSpan? slidingExpiration, DateTime? absoluteExpiration)
{
var policy = new CacheItemPolicy(); if(absoluteExpiration.HasValue)
{
policy.AbsoluteExpiration = absoluteExpiration.Value;
}
else if(slidingExpiration.HasValue)
{
policy.SlidingExpiration = slidingExpiration.Value;
} policy.Priority = CacheItemPriority.Default; return policy;
}
}
System.Runtime.Caching中MemoryCache帮助类的更多相关文章
- C# System.Runtime.Caching使用
System.Runtime.Caching命名空间是.NET 4.0新增的,目的是将以前的.NET 版本中的System.Web.Caching单独提取出来,独立使用,这样web和其他.NET程序如 ...
- HttpContext.Current.Cache和HttpRuntime.Cache的区别,以及System.Runtime.Caching
先看MSDN上的解释: HttpContext.Current.Cache:为当前 HTTP 请求获取Cache对象. HttpRuntime.Cache:获取当前应用程序的Cac ...
- System.Runtime.InteropServices.COMException 检索COM类工厂中CLSID{xxxxxxxxx}的组件时失败解决方法
iis7.5中设定应用程序池中<进程模型>中<标识>为localSystem 提示:System.Runtime.InteropServices.COMException: 命 ...
- System.Runtime.InteropServices.COMException: 检索 COM 类工厂中 CLSID 为 {0002E510-0000-0000-C000-000000000046} 的组件时失败,原因是出现以下错误: 80040154
这个问题困恼我好几天了,今天终于解决. 开始我在网上左百度右google,都没搜到最终的解决方案,今天我把解决方案贴出来,以供大家分享! 网上有些是报80070005错误的,跟我这个80040154错 ...
- System.Web.Caching.Cache缓存帮助类
/// <summary> /// 缓存帮助类 /// </summary> public class CacheHelper { /// <summary> // ...
- System.Web.Caching.Cache类 缓存 各种缓存依赖
原文:System.Web.Caching.Cache类 缓存 各种缓存依赖 Cache类,是一个用于缓存常用信息的类.HttpRuntime.Cache以及HttpContext.Current.C ...
- System.Runtime.Serialization.SerializationException”类型的未经处理的异常在 System.Runtime.Serialization.dll 中发生
异常信息: “System.Runtime.Serialization.SerializationException”类型的未经处理的异常在 System.Runtime.Serialization. ...
- 常用工具类(System,Runtime,Date,Calendar,Math)
一.System: 一个java.lang包中的静态工具类. 三大字段: static PrintStream err “标准”错误输出流. static InputStream in “标准”输入流 ...
- System.Web.Caching.Cache类 缓存 各种缓存依赖(转)
转自:http://www.cnblogs.com/kissdodog/archive/2013/05/07/3064895.html Cache类,是一个用于缓存常用信息的类.HttpRuntime ...
随机推荐
- os.environ详解
我们想要用Python获得一些有关系统的各种信息的时候就不得不想到os的environ,那这里面都具体包含了那些内容呢? 简介 对于官方的解释,environ是一个字符串所对应环境的映像对象.这是什么 ...
- 使用抓包工具将抓到的接口存放Jmeter中
1. jmeter工作台新增 HTTP代理服务器.端口设置为:8888 2. 抓包工具上面设置代理服务器地址 3.代理服务器未启动时,抓包工具界面显示无法联网. 4.在Jmeter中点击[启动]HTT ...
- JAVA中用StopWatch计算代码耗时的方法
StopWatch翻译过来的意思就是秒表,其作用也就像我们平时使用的秒一样.spring中就有提供这个工具类(org.springframework.util.StopWatch). 日常开发中,经常 ...
- Serializable笔记
什么是序列化与反序列化? 把对象转换为字节序列的过程称为对象的序列化.把字节序列恢复为对象的过程称为对象的反序列化. 序列化的用途? 简单来说,是为了方便存储与传输. 存储:在很多应用中,需要对某些对 ...
- U盘安装Windows Server2008 R2
安装Windows 2008 r2 提示windows 无法安装到这个磁盘.选中的磁盘采用GPT分区形式 利用U盘装系统的步骤 第一 进入BIOS,找SECURITY—SECURE BOOT中的SEC ...
- C# FluentFTP类上传下载文件
前言:最近要实现从FTP服务器下载和上传文件,在网上搜了一下据说 FluentFTP 是个客户端FTP功能的实现,使用还比较顺畅,所以对此展开研究,无奈网上给出的案例并没有想象中的那么简洁,所以想着自 ...
- 绘制UML图的工具
目前在用processOn网站进行绘制:https://www.processon.com/ 学习其简单的入门教程: https://www.processon.com/support https:/ ...
- jmeter Dashboard Report
说明:详情参考:https://jmeter.apache.org/usermanual/generating-dashboard.html JMeter3.0以后引入了Dashboard Repor ...
- MyISAM 和 InnoDB 索引的区别
阅读目录 一 MyISAM索引实现 二 InnoDB索引实现 三 InnoDB索引和MyISAM索引的区别 回到顶部 一 MyISAM索引实现 1. 主键索引 MyISAM引擎使用B+树作为索引结 ...
- Forbidden (CSRF token missing or incorrect.):
CSRF令牌失效或丢失,Ajax请求页面报错(403 Forbidden ) csrftoken存在 页面响应为CSRF验证失败请求被中断,经过测试,该错误并非是没有在表单中加入{% csrf_tok ...