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

参考资料:

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;
}
}

最新文章

  1. Duilib源码分析(四)绘制管理器—CPaintManagerUI—(前期准备二)
  2. ActionMapping
  3. 【BZOJ】3835: [Poi2014]Supercomputer
  4. git pull 然后 ahead of origin/master * commit 消失
  5. Linux JDK 安装及卸载 http://www.cnblogs.com/benio/archive/2010/09/14/1825909.html
  6. Android中将布局文件转成bitmap
  7. Android 二维码 生成和识别(转)
  8. Java 在某一个时间点定时执行任务(转载)
  9. 安装完win8后,显卡一些其他的驱动没有被正常安装,此处出现问题的机器是索尼vpccw18fc
  10. [drp 6]接口和抽象类的区别,及其应用场景
  11. POJ 3159 Candies (栈优化spfa)
  12. CSS3高性能动画
  13. Hibernate实体对象继承策略
  14. LeetCode OJ 107. Binary Tree Level Order Traversal II
  15. ch3-模板语法({{}} v-html v-bind:id 表达式 指令 修饰符 过滤器)
  16. MUI 页面传值,因为用的是H5+ plus方法所以要在真机上才能测试出效果
  17. JAVA IO流编程 实现文件的写入、写出以及拷贝
  18. Linux-存储管理
  19. 【原创】POJ 3259 Wormholes(Bellman-Ford) &amp;&amp; 简介Bellman-Ford算法
  20. 9:集合collection

热门文章

  1. EasyDSS高性能流媒体服务器开发RTMP直播同步输出HLS(m3u8)录像功能实现时移回放的方案
  2. spark 更改日志输出级别
  3. (CSDN迁移) JAVA多线程实现-可控最大并发数线程池(newFixedThreadPool)
  4. Windows下编译Redis5.0.5
  5. [转帖]Flink(一)Flink的入门简介
  6. java中对对象进行判空的操作--简洁编码
  7. 递归实现全排列python
  8. C语言函数返回指针方法
  9. elasticsearch安全重启节点
  10. Redis 多级缓存架构和数据库与缓存双写不一致问题