This article is reproduced from: https://blog.csdn.net/mss359681091/article/details/51076712
1. HttpRuntime.Cache is equivalent to a specific implementation class of cache, although this class is placed under the System.Web namespace. But non-Web applications can also be used.
2. HttpContext.Cache is the encapsulation of the above cache classes. Since it is encapsulated into HttpContext, it can only be used when HttpContext is known, that is, it can only be used in Web applications.
To sum up, if possible, try to use HttpRuntime.Cache instead of HttpContext.Cache.
1. There are the following rules for caching data
First, data may be used frequently, and this data can be cached.
Second, the access frequency of data is very high, or the access frequency of a data is not high, but its life cycle is very long, such data is best cached.
The third is a problem that is often overlooked. Sometimes we cache too much data. Usually on an X86 machine, if the data you want to cache exceeds 800M, there will be a memory overflow error. So the cache is limited. In other words, you should estimate the size of the cache set and limit the size of the cache set to less than 10, otherwise it may go wrong. In Asp.net, if the cache is too large, an out-of-memory error will also be reported, especially if the cache is large DataSet objects.
You should analyze your program carefully. Depending on the actual situation, see where to use it and where not to use it. For example, excessive use of the cache will also increase the pressure on the server. The full page output cache will affect the update of data. If you really need to cache a large amount of data, you can consider static techniques.
Second, the following introduces the common methods of HttpRuntime.Cache
- <strong>using System;
- using System.Web;
- using System.Collections;
- public class CookiesHelper
- {
- /**//// <summary>
- /// Get the data cache
- /// </summary>
- /// <param name="CacheKey">键</param>
- public static object GetCache(string CacheKey)
- {
- System.Web.Caching.Cache objCache = HttpRuntime.Cache;
- return objCache[CacheKey];
- }
- /**//// <summary>
- /// Set up the data cache
- /// </summary>
- public static void SetCache(string CacheKey, object objObject)
- {
- System.Web.Caching.Cache objCache = HttpRuntime.Cache;
- objCache.Insert(CacheKey, objObject);
- }
- /**//// <summary>
- /// Set up the data cache
- /// </summary>
- public static void SetCache(string CacheKey, object objObject, TimeSpan Timeout)
- {
- System.Web.Caching.Cache objCache = HttpRuntime.Cache;
- objCache.Insert(CacheKey, objObject, null, DateTime.MaxValue, Timeout, System.Web.Caching.CacheItemPriority.NotRemovable, null);
- }
- /**//// <summary>
- /// Set up the data cache
- /// </summary>
- public static void SetCache(string CacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration)
- {
- System.Web.Caching.Cache objCache = HttpRuntime.Cache;
- objCache.Insert(CacheKey, objObject, null, absoluteExpiration, slidingExpiration);
- }
- /**//// <summary>
- /// Remove the specified data cache
- /// </summary>
- public static void RemoveAllCache(string CacheKey)
- {
- System.Web.Caching.Cache _cache = HttpRuntime.Cache;
- _cache.Remove(CacheKey);
- }
- /**//// <summary>
- /// Remove all caches
- /// </summary>
- public static void RemoveAllCache()
- {
- System.Web.Caching.Cache _cache = HttpRuntime.Cache;
- IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
- while (CacheEnum.MoveNext())
- ...{
- _cache.Remove(CacheEnum.Key.ToString());
- }
- }
- }
- </strong>
- <strong> void Application_Start(object sender, EventArgs e)
- {
- // code to run when the application starts
- try
- {
- //define the connection string
- string conStr = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
- System.Data.SqlClient.SqlDependency.Start(conStr);//Start the listening service, ps: just start it once
- System.Web.Caching.SqlCacheDependencyAdmin.EnableNotifications(conStr);//Set the database connection for notification, ps: only need to set it once
- string[] str = { "TMS_OptionScoreDetails", "TMS_TeachAssess", "TMS_TeachAssessDetail", "TMS_TeachAssessPublish", "TMS_TeachAssessRecord", "TMS_TeachAssessRecordDetails", "TMS_TeachOption" };
- System.Web.Caching.SqlCacheDependencyAdmin.EnableTableForNotifications(conStr, str);//Set the database connection and table for notification, ps: only need to set it once
- }
- catch
- {
- }
- }
- </strong>
- //cache
- DataSet myDataSet = new DataSet();
- string CacheKey = "SearchDate" + TeachAssessID;
- object objModel = TMSCommonMethod.GetCache(CacheKey); //Get from the cache
- if (objModel == null) //nothing in the cache
- {
- TMSTeachAssessDetailManager myTMSTeachAssessDetailManager = new TMSTeachAssessDetailManager();
- TMSTeachAssessDetailQuery myTMSTeachAssessDetailQuery = new TMSTeachAssessDetailQuery();
- //myTMSTeachAssessDetailQuery.TeachAssessPublishID = TeachAssessPublishID;
- myTMSTeachAssessDetailQuery.TeachAssessID = TeachAssessID;
- myDataSet = myTMSTeachAssessDetailManager.SearchDate(null, myTMSTeachAssessDetailQuery);
- objModel = myDataSet; // store the data in the cache
- if (objModel != null)
- {
- System.Web.Caching.SqlCacheDependency dep = new System.Web.Caching.SqlCacheDependency(ConfigurationManager.AppSettings["CacheDataBaseName"].ToString(), TMSTeachAssessDetail.TABLENAME);
- TMSCommonMethod.SetCache(CacheKey, objModel, dep); //Write to cache
- }
- }
- else
- {
- myDataSet = (DataSet)objModel;
- }