System.Runtime.Caching中MemoryCache帮助类
2024-09-05 18:55:27
值得参考的几个内存缓存帮助类:
参考资料:
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;
}
}
最新文章
- Duilib源码分析(四)绘制管理器—CPaintManagerUI—(前期准备二)
- ActionMapping
- 【BZOJ】3835: [Poi2014]Supercomputer
- git pull 然后 ahead of origin/master * commit 消失
- Linux JDK 安装及卸载 http://www.cnblogs.com/benio/archive/2010/09/14/1825909.html
- Android中将布局文件转成bitmap
- Android 二维码 生成和识别(转)
- Java 在某一个时间点定时执行任务(转载)
- 安装完win8后,显卡一些其他的驱动没有被正常安装,此处出现问题的机器是索尼vpccw18fc
- [drp 6]接口和抽象类的区别,及其应用场景
- POJ 3159 Candies (栈优化spfa)
- CSS3高性能动画
- Hibernate实体对象继承策略
- LeetCode OJ 107. Binary Tree Level Order Traversal II
- ch3-模板语法({{}} v-html v-bind:id 表达式 指令 修饰符 过滤器)
- MUI 页面传值,因为用的是H5+ plus方法所以要在真机上才能测试出效果
- JAVA IO流编程 实现文件的写入、写出以及拷贝
- Linux-存储管理
- 【原创】POJ 3259 Wormholes(Bellman-Ford) &;&; 简介Bellman-Ford算法
- 9:集合collection
热门文章
- EasyDSS高性能流媒体服务器开发RTMP直播同步输出HLS(m3u8)录像功能实现时移回放的方案
- spark 更改日志输出级别
- (CSDN迁移) JAVA多线程实现-可控最大并发数线程池(newFixedThreadPool)
- Windows下编译Redis5.0.5
- [转帖]Flink(一)Flink的入门简介
- java中对对象进行判空的操作--简洁编码
- 递归实现全排列python
- C语言函数返回指针方法
- elasticsearch安全重启节点
- Redis 多级缓存架构和数据库与缓存双写不一致问题