值得参考的几个内存缓存帮助类:

参考资料:

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帮助类的更多相关文章

  1. C# System.Runtime.Caching使用

    System.Runtime.Caching命名空间是.NET 4.0新增的,目的是将以前的.NET 版本中的System.Web.Caching单独提取出来,独立使用,这样web和其他.NET程序如 ...

  2. HttpContext.Current.Cache和HttpRuntime.Cache的区别,以及System.Runtime.Caching

    先看MSDN上的解释:      HttpContext.Current.Cache:为当前 HTTP 请求获取Cache对象.      HttpRuntime.Cache:获取当前应用程序的Cac ...

  3. System.Runtime.InteropServices.COMException 检索COM类工厂中CLSID{xxxxxxxxx}的组件时失败解决方法

    iis7.5中设定应用程序池中<进程模型>中<标识>为localSystem 提示:System.Runtime.InteropServices.COMException: 命 ...

  4. System.Runtime.InteropServices.COMException: 检索 COM 类工厂中 CLSID 为 {0002E510-0000-0000-C000-000000000046} 的组件时失败,原因是出现以下错误: 80040154

    这个问题困恼我好几天了,今天终于解决. 开始我在网上左百度右google,都没搜到最终的解决方案,今天我把解决方案贴出来,以供大家分享! 网上有些是报80070005错误的,跟我这个80040154错 ...

  5. System.Web.Caching.Cache缓存帮助类

    /// <summary> /// 缓存帮助类 /// </summary> public class CacheHelper { /// <summary> // ...

  6. System.Web.Caching.Cache类 缓存 各种缓存依赖

    原文:System.Web.Caching.Cache类 缓存 各种缓存依赖 Cache类,是一个用于缓存常用信息的类.HttpRuntime.Cache以及HttpContext.Current.C ...

  7. System.Runtime.Serialization.SerializationException”类型的未经处理的异常在 System.Runtime.Serialization.dll 中发生

    异常信息: “System.Runtime.Serialization.SerializationException”类型的未经处理的异常在 System.Runtime.Serialization. ...

  8. 常用工具类(System,Runtime,Date,Calendar,Math)

    一.System: 一个java.lang包中的静态工具类. 三大字段: static PrintStream err “标准”错误输出流. static InputStream in “标准”输入流 ...

  9. System.Web.Caching.Cache类 缓存 各种缓存依赖(转)

    转自:http://www.cnblogs.com/kissdodog/archive/2013/05/07/3064895.html Cache类,是一个用于缓存常用信息的类.HttpRuntime ...

随机推荐

  1. WPF--控件模板的视觉效果呈现流程及逻辑

    外部通过属性把数据--传递给-->(破拆后)内部可视化树 ----> 内部可视化树呈现出视觉效果 ----> 各种内部可视化组件的视觉效果组合  --- 呈现 -->  外部的 ...

  2. 【Maven学习】定制库到Maven本地资源库

    目标:手工操作将一个jar安装到本地仓库 第一步:首先获取到jar包,可以是第三方的 也可以是自己创建的,放到本地任意目录 比如:joda-time-2.10.3,放到C:\jar\  目录下面 第二 ...

  3. Node.js实现PC端类微信聊天软件(三)

    Github StackChat Redux学习回顾 Redux的主要功能就是管理复杂交错的State,比如需要讲state提升到顶层组件的场景中,使用Redux就很合适 Redux主要提供三个东西来 ...

  4. odoo连接外部数据库

    odoo框架默认的访问时Postgres数据库,但在实际的应用场景中,不可避免的使用到其他数据库,所以有必要研究如何连接其他第三方数据库,这里分享下OCA的相关模块,具体的源代码在这里. 我将第三方的 ...

  5. RSA非对称式加解密笔记

    1.服务器生成[公钥]和[私钥],成对生成: 2.客户端生成证书信息,使用[公钥]进行加密,前提是有公钥,并生成证书信息: 3.客户端发送自身的计算机名.MAC.用户名.证书内容给服务器: 4.服务器 ...

  6. LeetCode 912. 排序数组(Sort an Array) 43

    912. 排序数组 912. Sort an Array 题目描述 每日一算法2019/6/15Day 43LeetCode912. Sort an Array

  7. Django-05-视图函数

    http请求中产生两个核心对象: http请求:HttpRequest对象 http响应:HttpResponse对象 所在位置:django.http 之前我们用到的参数request就是HttpR ...

  8. python学习-36 文件处理b模式

    文件处理b模式 1.以2进制的方式读取 f = open('test.txt','rb') #以b模式就不能指定encoding data = f.read() print(data) f.close ...

  9. C复习 (C premier plus和C和指针)

  10. PB 选择继承父类的注意事项

    1.父类的datewindow 的祖先类最好不是n_dw_single ,最好是n_dw_grid,n_dw_single  的标题行没有阴影.排序,没有行聚焦等功能(非常初始的).n_dw_grid ...