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

参考资料:

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. es查询示例

    1. 建立连接 from elasticsearch import Elasticsearch es = Elasticsearch(["localhost:9200"]) 2. ...

  2. VLOOKUP使用方法

    VLOOKUP函数是常用的一个内容查找函数,用于通过某一条件查询数据源中需要的内容.语法:=VLOOKUP(查询值,数据源,显示序列,匹配参数)1)查询值:匹配的key值2)数据源:查找范围,1)起点 ...

  3. 理解CNN中的感受野(receptive-field)

    1. 阅读论文:Understanding the Effective Receptive Field in Deep Convolutional Neural Networks 理解感受野 定义:r ...

  4. [LeetCode] 734. Sentence Similarity 句子相似度

    Given two sentences words1, words2 (each represented as an array of strings), and a list of similar ...

  5. [Arch] 域名解析常用两步设置

    主站   A 记录        (对应IPv4)   主机记录: @ (表示解析到不带 www 主域名)   值为 IP 主站 CNAME 记录 (表示别名) 主机记录: www   (表示解析到带 ...

  6. python入门之格式化输出

    目录 扩展: 保留几位小数 一.占位符格式化输出 1.1 %s 1.2 %d 二..format()方式 三.f-string 扩展: 保留几位小数 保留两位小数 a = 12345.2487 pri ...

  7. .NET Core 之 Nancy 基本使用

    Nancy简介 Nancy是一个轻量级的独立的框架,下面是官网的一些介绍: Nancy 是一个轻量级用于构建基于 HTTP 的 Web 服务,基于 .NET 和 Mono 平台,框架的目标是保持尽可能 ...

  8. JVM中内存的设置和分配(最大内存,总内存,剩余内存的区别)

    1.设置分配的内存大小 -vmargs -Xms128M -Xmx512M -XX:PermSize=64M -XX:MaxPermSize=128M -vmargs 说明后面是VM的参数,所以后面的 ...

  9. jdk的下载路径和环境变量的配置

    一:jdk百度网盘的下载路径: 链接:https://pan.baidu.com/s/1pF41oGcTqouULsWKEBn3hw 提取码:p1y2 复制这段内容后打开百度网盘手机App,操作更方便 ...

  10. Python知识之 方法与函数、偏函数、轮询和长轮询、流量削峰、乐观锁与悲观锁

    方法与函数 函数需要手动传参self.cls,方法自动传,比如对象方法自动传self,类方法自动传cls,而函数相对而言需要手动传,比如静态绑定的函数,self是需要手动传值得,比如我们平常使用的函数 ...