c# - Cache Code
来源:互联网 发布:项目管理系统源码 编辑:程序博客网 时间:2024/05/17 21:58
/// <summary> /// Get item using cachem, will refresh cache after 1 day. /// </summary> /// <typeparam name="T">Type of item to get</typeparam> /// <param name="cacheKey">Cache key</param> /// <param name="function">Function to get the item if not available in cache</param> /// <returns>Retrieved object</returns> public static T UsingCache<T>(string cacheKey, Func<T> function) { object cachedObject = null; if (HttpContext.Current != null) { cachedObject = HttpContext.Current.Cache.Get(cacheKey); } else { cachedObject = MemoryCache.Default.Get(cacheKey); } if (cachedObject != null) { if (cachedObject == DBNull.Value) { return default(T); } try { return (T) cachedObject; } catch { // In this case continue and get result from function } } T result = function(); if (HttpContext.Current != null) { HttpContext.Current.Cache.Add(cacheKey, (object) result ?? DBNull.Value, new CacheDependency(HttpContext.Current.Server.MapPath("~/bin")), DateTime.Now.AddDays(1), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Default, null); } else { MemoryCache.Default.Add( cacheKey, (object) result ?? DBNull.Value, new CacheItemPolicy() { AbsoluteExpiration = new DateTimeOffset(DateTime.Now.AddDays(1)) }); } return result; }
0 0
- c# - Cache Code
- CODE 143:LRU Cache
- Writing Cache-friendly Code
- Leet Code LRU Cache
- Cache friendly code
- Nginx lua code cache开关
- C# Code
- C# 缓存(Cache)
- ARM cache miss-match test (C++ Code)
- Code Fragment-使用disk cache存储图片
- leetcode 日经贴,Cpp code -LRU Cache
- leetcode 日经贴,Cpp code -LRU Cache
- C#读取IE的Cache
- C# 数据缓存操作Cache
- C#中Cache的使用
- C#中Cache的使用
- C# 数据缓存操作Cache
- C#中Cache的使用
- eclipse中的android布局文件的快捷键Alt+/不起作用的一种解决方法
- UVa--400 Unix ls(格式输出)
- python中的引用
- iOS常见的错误大全
- 重写前奏~继承和多态
- c# - Cache Code
- IOS开发 钥匙串 证书“此证书的签发者无效”解决方法(详细!!)
- Android应用程序权限机制的源代码分析(1)
- Android中保存网络图片到SD卡
- jquery.validate 自定义验证方法
- HTML5物理游戏开发 - 越野山地自行车(二)创建一辆可操控的自行车
- iOS之UIRefreshControl系统下拉刷新
- 深入浅出Python装饰器
- _exit 和 exit的区别