asp.net pagebase获取缓存的方法

时间:2023-03-09 03:03:04
asp.net  pagebase获取缓存的方法
        public string GetSysConfigByKey(string key)
{
if (object.Equals(HttpContext.Current.Cache["Cache_SysConfig"], null))
{
this.LoadSysConfigToCache();
}
Hashtable hashtable = (Hashtable)HttpContext.Current.Cache["Cache_SysConfig"];
if (hashtable.ContainsKey(key))
{
return hashtable[key].ToString();
}
return "";
}

  写入缓存

        public void LoadSysConfigToCache()
{
try
{
if (object.Equals(HttpContext.Current.Cache["Cache_SysConfig"], null))
{
DataTable allSysConfig = new SysConfigBLL().GetAllSysConfig();
Hashtable hashtable = new Hashtable();
foreach (DataRow row in allSysConfig.Rows)
{
hashtable.Add(row["ConfigKey"].ToString(), row["ConfigValue"].ToString());
}
HttpContext.Current.Cache.Insert("Cache_SysConfig", hashtable);
}
}
catch (Exception exception)
{
ExceptionLogFactory.CreateObject(enuExceptionType.File, exception);
throw;
}
}