ASP. NET 4 Common Ways of Page Caching

  • 2021-08-21 20:16:13
  • OfStack

In this article, we share four common ASP. NET page caching methods for your reference. The specific contents are as follows

1. Distributed cache Memcached, tutorial download

2. Memory cache, which occupies server resources


#region  Memory cache  
 public class MemoryCache 
 { 
 #region  Write  
 /// <summary> 
 ///  Write data cache to memory  
 /// </summary> 
 /// <remarks>TOMMYHU2011-7-28 10:25 Create </remarks> 
 /// <param name="cachekey"> Cache identification key </param> 
 /// <param name="cacheresult"> Data to be stored </param> 
 /// <param name="cachetime"> Unit second </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  Read  
 /// <summary> 
 ///  Reading memory cache information according to cache identification  
 /// </summary> 
 /// <remarks>TOMMYHU2011-7-28 10:25 Create </remarks> 
 /// <param name="cachekey"> Cache identification key </param> 
 /// <returns> Memory cache data </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 cache, which is the most common


 #region xml Cache  
 public static class XmlCache 
 { 
 private static string m_CacheFolderName = null; 
 
 #region  Get / Method for generating cache file ( None cmdParams) 
 /// <summary> 
 ///  Get / Method for generating cache file (DataTable) 
 /// </summary> 
 /// <param name="SQL"> Used to set the Sql Convert to MD5 String </param> 
 /// <param name="CacheFilePath"> Storage Xml Relative path of file ( Root directory is preset )</param> 
 /// <param name="CacheTime"> Cache time </param> 
 /// <param name="DataUsed"> The link string of the database used </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;// Used to set the Sql Convert to MD5 String  
 
 string StrSqlByMd5 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(SqlMd5, "MD5");// Generated Md5 Filename  
 
 string MyCacheFilePath = ""; 
 
 if (CacheTime >= 10000)// If greater than 20 In days, the system defaults to persistence  
 { 
  MyCacheFilePath = XmlCache.CacheFolderNameP + CacheFilePath;// For storage Xml Relative path of file  
 } 
 else 
 { 
  MyCacheFilePath = XmlCache.CacheFolderName + CacheFilePath;// For storage Xml Relative path of file  
 } 
 
 
 int MyCacheTime = CacheTime;// Cache time ( Points ) 
 
 DataTable dt = new DataTable();// Storing data records DataTable 
 
 DataTable CacheDt = new DataTable();// Object of the cached data record DataTable 
 
 
 // Try to get a cached data record  
 try 
 { 
  CacheDt = DataTableCache.GetDTCache(MyCacheTime, MyCacheFilePath, StrSqlByMd5); 
 } 
 catch 
 { 
  CacheDt = null; 
 } 
 if (CacheDt != null)// Object to the cache XML Documents  
 { 
  dt = CacheDt; 
 } 
 else// Cached XML Documents  
 { 
  // Generate DataTable( If you use Query For enquiries, please use Query.ProcessSql Method ) 
  //dt = Query.ProcessSql(SqlMd5, DataUsed); 
  dt = QuerySql.retDT(SQL); 
 
  // Will DataTable Save as XML Documents  
  try 
  { 
  if (dt.DataSet != null) 
  { 
  dt.DataSet.Tables.Remove(dt); 
  } 
 
  DataTableCache.SetDTCache(MyCacheFilePath, StrSqlByMd5, dt); 
  } 
  catch (Exception ex) 
  { 
  //Log.GetComLogger().Error(" Will DataTable Save as XML File error: " + ex.Message); 
  } 
 } 
 return dt; 
 } 
 
 /// <summary> 
 ///  Get / Method for generating cache file (DataSet) Add By WJ 08-10-23 
 /// </summary> 
 /// <param name="SQL"> Used to set the Sql Convert to MD5 String </param> 
 /// <param name="CacheFilePath"> Storage Xml Relative path of file ( Root directory is preset )</param> 
 /// <param name="CacheTime"> Cache time </param> 
 /// <param name="DBName"> The link string of the database used </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;// Used to set the Sql Convert to MD5 String  
 
 string StrSqlByMd5 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(SqlMd5, "MD5");// Generated Md5 Filename  
 
 string MyCacheFilePath = XmlCache.CacheFolderName + CacheFilePath;// For storage Xml Relative path of file  
 
 int MyCacheTime = CacheTime;// Cache time ( Points ) 
 
 DataSet ds = new DataSet();// Storing data records DataSet 
 
 DataSet CacheDs = new DataSet();// Object of the cached data record DataSet 
 
 // Try to get a cached data record  
 try 
 { 
  CacheDs = DataTableCache.GetDSCache(MyCacheTime, MyCacheFilePath, StrSqlByMd5); 
 } 
 catch 
 { 
  CacheDs = null; 
 } 
 if (CacheDs != null)// Object to the cache XML Documents  
 { 
  ds = CacheDs; 
 } 
 else// Cached XML Documents  
 { 
  // Generate DataSet 
  //ds = Query.ProcessMultiSql(SqlMd5, DBName); 
  ds = QuerySql.retDS(SQL); 
 
  // Will DataSet Save as XML Documents  
  try 
  { 
  DataTableCache.SetDSCache(MyCacheFilePath, StrSqlByMd5, ds); 
  } 
  catch 
  { } 
 } 
 return ds; 
 } 
 
 #endregion 
 
 #region  Get / Method for generating cache file ( Have cmdParams) 
 /// <summary> 
 ///  Get / Method for generating cache file (DataTable) 
 /// </summary> 
 /// <param name="SQL"> Used to set the Sql Convert to MD5 String </param> 
 /// <param name="CacheFilePath"> Storage Xml Relative path of file ( Root directory is preset )</param> 
 /// <param name="CacheTime"> Cache time </param> 
 /// <param name="DataUsed"> The link string of the database used </param> 
 /// <param name="cmdParams"> Provided as an array SqlCommand List of parameters used in the command </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;// Used to set the Sql Convert to MD5 String  
 
 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");// Generated Md5 Filename  
 
 string MyCacheFilePath = ""; 
 
 if (CacheTime >= 10000)// If greater than 20 In days, the system defaults to persistence  
 { 
  MyCacheFilePath = XmlCache.CacheFolderNameP + CacheFilePath;// For storage Xml Relative path of file  
 } 
 else 
 { 
  MyCacheFilePath = XmlCache.CacheFolderName + CacheFilePath;// For storage Xml Relative path of file  
 } 
 
 int MyCacheTime = CacheTime;// Cache time ( Points ) 
 
 DataTable dt = new DataTable();// Storing data records DataTable 
 
 DataTable CacheDt = new DataTable();// Object of the cached data record DataTable 
 
 
 // Try to get a cached data record  
 CacheDt = DataTableCache.GetDTCache(MyCacheTime, MyCacheFilePath, StrSqlByMd5); 
 
 if (CacheDt != null)// Object to the cache XML Documents  
 { 
  dt = CacheDt; 
 } 
 else// Cached XML Documents  
 { 
  // Generate DataTable( If you use Query For enquiries, please use Query.ProcessSql Method ) 
  //dt = Query.ProcessSql(SqlMd5, DataUsed); 
  dt = QuerySql.retDT(SqlMd5); 
  DataTable dt1 = new DataTable(); 
  if (dt != null) 
  { 
  dt1 = dt.Copy(); 
  } 
  // Will DataTable Save as XML Documents  
  DataTableCache.SetDTCache(MyCacheFilePath, StrSqlByMd5, dt1); 
 } 
 return dt; 
 } 
 
 
 /// <summary> 
 ///  Get / Method for generating cache file (DataSet) Add By wjf 
 /// </summary> 
 /// <param name="SQL"> Used to set the Sql Convert to MD5 String </param> 
 /// <param name="CacheFilePath"> Storage Xml Relative path of file ( Root directory is preset )</param> 
 /// <param name="CacheTime"> Cache time </param> 
 /// <param name="DBName"> The link string of the database used </param> 
 /// <param name="cmdParams"> Provided as an array SqlCommand List of parameters used in the command </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;// Used to set the Sql Convert to MD5 String  
 
 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");// Generated Md5 Filename  
 
 string MyCacheFilePath = XmlCache.CacheFolderName + CacheFilePath;// For storage Xml Relative path of file  
 
 int MyCacheTime = CacheTime;// Cache time ( Points ) 
 
 DataSet ds = new DataSet();// Storing data records DataSet 
 
 DataSet CacheDs = new DataSet();// Object of the cached data record DataSet 
 
 // Try to get a cached data record  
 CacheDs = DataTableCache.GetDSCache(MyCacheTime, MyCacheFilePath, StrSqlByMd5); 
 
 if (CacheDs != null)// Object to the cache XML Documents  
 { 
  ds = CacheDs; 
 } 
 else// Cached XML Documents  
 { 
  // Generate DataSet 
  //ds = Query.ProcessMultiSql(SqlMd5, DBName); 
  ds = QuerySql.retDS(SqlMd5); 
  DataSet ds1 = new DataSet(); 
  if (ds != null) 
  { 
  ds1 = ds.Copy(); 
  } 
  // Will DataSet Save as XML Documents  
  DataTableCache.SetDSCache(MyCacheFilePath, StrSqlByMd5, ds1); 
 } 
 return ds; 
 } 
 #endregion 
 
 #region  Non-persistent maintenance  
 /// <summary> 
 ///  Non-persistent maintenance  
 /// </summary> 
 public static string CacheFolderName 
 { 
 get 
 { 
  if (m_CacheFolderName == null)// If Global.asax No path is defined in  
  { 
  m_CacheFolderName = System.Configuration.ConfigurationManager.AppSettings.GetValues("CachePathRoot")[0]; 
  if (m_CacheFolderName == null)// If Web.Config No path is defined in  
  { 
  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  Persistent preservation ( Such as storage MapBar Cached data or needs 3 Data updated for more than three months, etc. ) 
 /// <summary> 
 ///  Persistent preservation ( Such as storage MapBar Cached data or needs 3 Data updated for more than three months, etc. ) 
 /// </summary> 
 public static string CacheFolderNameP 
 { 
 get 
 { 
  if (m_CacheFolderName == null)// If Global.asax No path is defined in  
  { 
  m_CacheFolderName = System.Configuration.ConfigurationManager.AppSettings.GetValues("CachePathRoot")[0]; 
  if (m_CacheFolderName == null)// If Web.Config No path is defined in  
  { 
  return "/CacheData/"; 
  } 
  else 
  { 
  return m_CacheFolderName; 
  } 
  } 
  else 
  { 
  return m_CacheFolderName; 
  } 
 } 
 
 set 
 { 
  m_CacheFolderName = value; 
 } 
 } 
 #endregion 
 } 
 #endregion 

4. DATATABLE Cache


Related articles: