本文共 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/