博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
System.Runtime.Caching中MemoryCache帮助类
阅读量:4558 次
发布时间:2019-06-08

本文共 1612 字,大约阅读时间需要 5 分钟。

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

 

参考资料:

 

 

 

public static class CacheHelper{    private static readonly Object _locker = new object();    public static T GetCacheItem
(String key, Func
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; }}

转载于:https://www.cnblogs.com/caianhua/p/10836205.html

你可能感兴趣的文章
树莓派设定显示分辨率
查看>>
浅析nodejs的buffer类
查看>>
C++ 基类指针,子类指针,多态
查看>>
从TWAIN设备中扫描图像
查看>>
JPA Annotation注解
查看>>
HDU 2089 HDU3555 数位DP
查看>>
【操作系统】堆和动态内存管理
查看>>
jQuery旋转插件jquery.rotate.js 让图片旋转
查看>>
python 零起点(4)
查看>>
【WPF】Combobox指定选中值用selectedValue不是很灵的时候,
查看>>
解决.net移除dll文件以后依旧报错
查看>>
网络资源
查看>>
[RoCE]网络QoS总结
查看>>
[XML] ResourceManager一个操作Resource的帮助类 (转载)
查看>>
C# Redis实战
查看>>
PHP 中英文混排截取字符串
查看>>
Csharp:user WebControl Read Adobe PDF Files In Your Web Browser
查看>>
Davesla!
查看>>
JQuery中实用小技巧总结
查看>>
软件工程--构建之法--功能测试 设计10个或者更多的测试案例完成对钉书钉的功能测试...
查看>>