首页 > 编程 > .NET > 正文

ASP.NET页面缓存常见的4种方式

2024-07-10 12:49:36
字体:
来源:转载
供稿:网友

本文为大家分享了4种常见的ASP.NET页面缓存方式,供大家参考,具体内容如下

1、分布式缓存Memcached,教程下载

2、内存缓存,此占用服务器资源

#region 内存缓存  public class MemoryCache  {  #region 写  /// <summary>  /// 向内存写入数据缓存  /// </summary>  /// <remarks>TOMMYHU2011-7-28 10:25创建</remarks>  /// <param name="cachekey">缓存标识关键字</param>  /// <param name="cacheresult">需要存放的数据</param>  /// <param name="cachetime">单位秒</param>  public static void InertMemoryCache(string cachekey, object cacheresult, int cachetime)  {  if (cacheresult != null)  {   System.Web.HttpRuntime.Cache.Insert(cachekey, cacheresult, null   , System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(cachetime));  }  }  #endregion  #region 读  /// <summary>  /// 根据缓存标识读取内存缓存信息  /// </summary>  /// <remarks>TOMMYHU2011-7-28 10:25创建</remarks>  /// <param name="cachekey">缓存标识关键字</param>  /// <returns>内存缓存数据</returns>  public static object ReadMemoryCache(string cachekey)  {  object obj = System.Web.HttpRuntime.Cache.Get(cachekey);  if (obj != null)  {   return obj;  }  return null;  }  #endregion  }  #endregion 

3、XML缓存,这种最为常见

 #region xml缓存  public static class XmlCache  {  private static string m_CacheFolderName = null;   #region 获取/生成缓存文件方法(无cmdParams)  /// <summary>  /// 获取/生成缓存文件方法(DataTable)  /// </summary>  /// <param name="SQL">用于将Sql转化为MD5字符串</param>  /// <param name="CacheFilePath">存放Xml文件的相对路径(根目录已预置)</param>  /// <param name="CacheTime">缓存时间</param>  /// <param name="DataUsed">使用的数据库的链接字符串</param>  /// <returns></returns>  public static DataTable CacheFileByDataTable(string SQL, string CacheFilePath, int CacheTime)  {  SQLHelper.SQLHelper QuerySql = new SQLHelper.SQLHelper();  if (CacheFilePath != string.Empty && CacheFilePath.StartsWith("/"))  {   CacheFilePath = CacheFilePath.Remove(0, 1);  }   string SqlMd5 = SQL;//用于将Sql转化为MD5字符串   string StrSqlByMd5 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(SqlMd5, "MD5");//生成的Md5文件名   string MyCacheFilePath = "";   if (CacheTime >= 10000)//如果大于20天的话系统默认为持久  {   MyCacheFilePath = XmlCache.CacheFolderNameP + CacheFilePath;//用于存放Xml文件的相对路径  }  else  {   MyCacheFilePath = XmlCache.CacheFolderName + CacheFilePath;//用于存放Xml文件的相对路径  }    int MyCacheTime = CacheTime;//缓存时间(分)   DataTable dt = new DataTable();//存放数据记录的DataTable   DataTable CacheDt = new DataTable();//获取缓存数据记录的DataTable    //尝试获取缓存数据记录  try  {   CacheDt = DataTableCache.GetDTCache(MyCacheTime, MyCacheFilePath, StrSqlByMd5);  }  catch  {   CacheDt = null;  }  if (CacheDt != null)//获取到缓存的XML文件  {   dt = CacheDt;  }  else//未获取到缓存的XML文件  {   //生成DataTable(如果用Query查询请用Query.ProcessSql方法)   //dt = Query.ProcessSql(SqlMd5, DataUsed);   dt = QuerySql.retDT(SQL);    //将DataTable存为XML文件   try   {   if (dt.DataSet != null)   {   dt.DataSet.Tables.Remove(dt);   }    DataTableCache.SetDTCache(MyCacheFilePath, StrSqlByMd5, dt);   }   catch (Exception ex)   {   //Log.GetComLogger().Error("将DataTable存为XML文件出错:" + ex.Message);   }  }  return dt;  }   /// <summary>  /// 获取/生成缓存文件方法(DataSet) Add By WJ 08-10-23  /// </summary>  /// <param name="SQL">用于将Sql转化为MD5字符串</param>  /// <param name="CacheFilePath">存放Xml文件的相对路径(根目录已预置)</param>  /// <param name="CacheTime">缓存时间</param>  /// <param name="DBName">使用的数据库的链接字符串</param>  /// <returns></returns>  public static DataSet CacheFileByDataSet(string SQL, string CacheFilePath, int CacheTime)  {  SQLHelper.SQLHelper QuerySql = new SQLHelper.SQLHelper();  if (CacheFilePath != string.Empty && CacheFilePath.StartsWith("/"))  {   CacheFilePath = CacheFilePath.Remove(0, 1);  }   string SqlMd5 = SQL;//用于将Sql转化为MD5字符串   string StrSqlByMd5 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(SqlMd5, "MD5");//生成的Md5文件名   string MyCacheFilePath = XmlCache.CacheFolderName + CacheFilePath;//用于存放Xml文件的相对路径   int MyCacheTime = CacheTime;//缓存时间(分)   DataSet ds = new DataSet();//存放数据记录的DataSet   DataSet CacheDs = new DataSet();//获取缓存数据记录的DataSet   //尝试获取缓存数据记录  try  {   CacheDs = DataTableCache.GetDSCache(MyCacheTime, MyCacheFilePath, StrSqlByMd5);  }  catch  {   CacheDs = null;  }  if (CacheDs != null)//获取到缓存的XML文件  {   ds = CacheDs;  }  else//未获取到缓存的XML文件  {   //生成DataSet   //ds = Query.ProcessMultiSql(SqlMd5, DBName);   ds = QuerySql.retDS(SQL);    //将DataSet存为XML文件   try   {   DataTableCache.SetDSCache(MyCacheFilePath, StrSqlByMd5, ds);   }   catch   { }  }  return ds;  }   #endregion   #region 获取/生成缓存文件方法(有cmdParams)  /// <summary>  /// 获取/生成缓存文件方法(DataTable)  /// </summary>  /// <param name="SQL">用于将Sql转化为MD5字符串</param>  /// <param name="CacheFilePath">存放Xml文件的相对路径(根目录已预置)</param>  /// <param name="CacheTime">缓存时间</param>  /// <param name="DataUsed">使用的数据库的链接字符串</param>  /// <param name="cmdParams">以数组形式提供SqlCommand命令中用到的参数列表</param>  /// <returns></returns>  public static DataTable CacheFileByDataTable(string SQL, string CacheFilePath, int CacheTime, string DBConstr, params SqlParameter[] cmdParams)  {  SQLHelper.SQLHelper QuerySql = new SQLHelper.SQLHelper();  if (CacheFilePath != string.Empty && CacheFilePath.StartsWith("/"))  {   CacheFilePath = CacheFilePath.Remove(0, 1);  }   string SqlMd5 = SQL;//用于将Sql转化为MD5字符串   string sqlmd5params = SQL;  if (cmdParams != null)  {   for (int i = 0; i < cmdParams.Length; i++)   {   if (cmdParams[i].Value != null)   sqlmd5params += cmdParams[i].Value.ToString();   }  }   string StrSqlByMd5 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sqlmd5params, "MD5");//生成的Md5文件名   string MyCacheFilePath = "";   if (CacheTime >= 10000)//如果大于20天的话系统默认为持久  {   MyCacheFilePath = XmlCache.CacheFolderNameP + CacheFilePath;//用于存放Xml文件的相对路径  }  else  {   MyCacheFilePath = XmlCache.CacheFolderName + CacheFilePath;//用于存放Xml文件的相对路径  }   int MyCacheTime = CacheTime;//缓存时间(分)   DataTable dt = new DataTable();//存放数据记录的DataTable   DataTable CacheDt = new DataTable();//获取缓存数据记录的DataTable    //尝试获取缓存数据记录  CacheDt = DataTableCache.GetDTCache(MyCacheTime, MyCacheFilePath, StrSqlByMd5);   if (CacheDt != null)//获取到缓存的XML文件  {   dt = CacheDt;  }  else//未获取到缓存的XML文件  {   //生成DataTable(如果用Query查询请用Query.ProcessSql方法)   //dt = Query.ProcessSql(SqlMd5, DataUsed);   dt = QuerySql.retDT(SqlMd5);   DataTable dt1 = new DataTable();   if (dt != null)   {   dt1 = dt.Copy();   }   //将DataTable存为XML文件   DataTableCache.SetDTCache(MyCacheFilePath, StrSqlByMd5, dt1);  }  return dt;  }    /// <summary>  /// 获取/生成缓存文件方法(DataSet) Add By wjf  /// </summary>  /// <param name="SQL">用于将Sql转化为MD5字符串</param>  /// <param name="CacheFilePath">存放Xml文件的相对路径(根目录已预置)</param>  /// <param name="CacheTime">缓存时间</param>  /// <param name="DBName">使用的数据库的链接字符串</param>  /// <param name="cmdParams">以数组形式提供SqlCommand命令中用到的参数列表</param>  /// <returns></returns>  public static DataSet CacheFileByDataSet(string SQL, string CacheFilePath, int CacheTime, string DBConstr, params SqlParameter[] cmdParams)  {  SQLHelper.SQLHelper QuerySql = new SQLHelper.SQLHelper();  string SqlMd5 = SQL;//用于将Sql转化为MD5字符串   string sqlmd5params = SQL;  if (cmdParams != null)  {   for (int i = 0; i < cmdParams.Length; i++)   {   if (cmdParams[i].Value != null)   sqlmd5params += cmdParams[i].Value.ToString();   }  }   string StrSqlByMd5 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sqlmd5params, "MD5");//生成的Md5文件名   string MyCacheFilePath = XmlCache.CacheFolderName + CacheFilePath;//用于存放Xml文件的相对路径   int MyCacheTime = CacheTime;//缓存时间(分)   DataSet ds = new DataSet();//存放数据记录的DataSet   DataSet CacheDs = new DataSet();//获取缓存数据记录的DataSet   //尝试获取缓存数据记录  CacheDs = DataTableCache.GetDSCache(MyCacheTime, MyCacheFilePath, StrSqlByMd5);   if (CacheDs != null)//获取到缓存的XML文件  {   ds = CacheDs;  }  else//未获取到缓存的XML文件  {   //生成DataSet   //ds = Query.ProcessMultiSql(SqlMd5, DBName);   ds = QuerySql.retDS(SqlMd5);   DataSet ds1 = new DataSet();   if (ds != null)   {   ds1 = ds.Copy();   }   //将DataSet存为XML文件   DataTableCache.SetDSCache(MyCacheFilePath, StrSqlByMd5, ds1);  }  return ds;  }  #endregion   #region 非持久保持  /// <summary>  /// 非持久保持  /// </summary>  public static string CacheFolderName  {  get  {   if (m_CacheFolderName == null)//如果Global.asax中未定义路径   {   m_CacheFolderName = System.Configuration.ConfigurationManager.AppSettings.GetValues("CachePathRoot")[0];   if (m_CacheFolderName == null)//如果Web.Config中未定义路径   {   return "/CacheData/" + DateTime.Now.ToString("yyyyMMdd") + "/";   }   else   {   return m_CacheFolderName + DateTime.Now.ToString("yyyyMMdd") + "/";   }   }   else   {   return m_CacheFolderName + DateTime.Now.ToString("yyyyMMdd") + "/";   }  }   set  {   m_CacheFolderName = value;  }  }  #endregion   #region 持久保存(例如存放MapBar缓存的数据或需要3个月以上更新的数据等)  /// <summary>  /// 持久保存(例如存放MapBar缓存的数据或需要3个月以上更新的数据等)  /// </summary>  public static string CacheFolderNameP  {  get  {   if (m_CacheFolderName == null)//如果Global.asax中未定义路径   {   m_CacheFolderName = System.Configuration.ConfigurationManager.AppSettings.GetValues("CachePathRoot")[0];   if (m_CacheFolderName == null)//如果Web.Config中未定义路径   {   return "/CacheData/";   }   else   {   return m_CacheFolderName;   }   }   else   {   return m_CacheFolderName;   }  }   set  {   m_CacheFolderName = value;  }  }  #endregion  }  #endregion             
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表