本文為大家分享了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
4、DATATABLE緩存
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持幫客之家。