博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HttpContext.Current.Cache 提示:未将对象引用设置到对象的实例
阅读量:2287 次
发布时间:2019-05-09

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

///         ///项目预警缓存         ///         /// 项目ID        /// 配置字段        /// 
public static string[] GetWoedBy(string brandid,string strConfig) { try { string cachekey = "GetWoedBy" + brandid + strConfig; if (HttpContext.Current.Cache[cachekey] != null) { return (string[])HttpContext.Current.Cache[cachekey]; } else { string keyword = System.Configuration.ConfigurationManager.AppSettings[strConfig].ToString(); string[] brandkeyword = keyword.Split(';'); string[] key = null; string[] word = null; for (int i = 0; i < brandkeyword.Length; i++) { if (brandkeyword[i] != "") { key = brandkeyword[i].Split(':'); if (key[0] == brandid) { word = key[1].Split('/'); } } } HttpContext.Current.Cache.Insert(cachekey, word, null, DateTime.Now.AddDays(7), TimeSpan.Zero); return word; } } catch (Exception e) { throw; } }

提示: [System.NullReferenceException] = {"未将对象引用设置到对象的实例。"}

原因:内存不足时,缓存 Cache 在会自动地移除

解决:将Cache用Application代替:

           HttpContext.Current.Application[cachekey]

///         ///项目预警缓存         ///         /// 项目ID        /// 配置字段        /// 
public static string[] GetWoedBy(string brandid, string strConfig) { try { string cachekey = "GetWoedBy" + brandid + strConfig; if (HttpContext.Current.Application[cachekey] != null) { return (string[])HttpContext.Current.Application[cachekey]; } else { string keyword = System.Configuration.ConfigurationManager.AppSettings[strConfig].ToString(); string[] brandkeyword = keyword.Split(';'); string[] key = null; string[] word = null; for (int i = 0; i < brandkeyword.Length; i++) { if (brandkeyword[i] != "") { key = brandkeyword[i].Split(':'); if (key[0] == brandid) { word = key[1].Split('/'); } } } HttpContext.Current.Application.Add(cachekey, word); return word; } } catch (Exception e) { throw; } }

转载地址:http://xyunb.baihongyu.com/

你可能感兴趣的文章
水利水电课程指导之建筑制图基础_第三章
查看>>
[年付99元]腾讯AMD云服务器1核1G高性能VPS
查看>>
String和Color的硬编码多达3千处,咋办?
查看>>
2020 Android 大厂面试(五)插件化、模块化、组件化、热修复、增量更新、Gradle...
查看>>
我们为什么要用fitsSystemWindows?
查看>>
如何使用BottomSheet
查看>>
在厕所遇到领导到底该说些什么?
查看>>
为什么不仅继承Observale而且使用Observale.create()?
查看>>
Flutter适配深色模式(DarkMode)
查看>>
程序员埋逻辑炸弹,被判 6 个月
查看>>
80后、90后扎心图鉴
查看>>
Android组件化跨进程通信框架Andromeda解析
查看>>
昨天你被YYYY-MM-DD坑了吗?
查看>>
2020回顾Android 的发展历程-谈一下当下最合适的架构
查看>>
公布!达摩院2020年十大科技趋势
查看>>
[译]Android原生开发的现状,截止到2019年12月
查看>>
Flutter 开发踩坑记录(干货总结)
查看>>
Android 面试经验 - 大厂 腾讯 面经
查看>>
Android一个人的组件化:组件隔离,模块调试
查看>>
周末福利:阿里巴巴29个屌炸天的开源项目!
查看>>