Using HttpRuntime.Cache is HttpRuntime.Cache to use than HttpContext.Current.Cache . As already mentioned, objects can be stored in the cache and indexed by a string. Also in unit test and HttpRuntime console HttpRuntime is available.
Here is an example using HttpRuntime.Cache .
public static XmlDocument GetStuff(string sKey) { XmlDocument xmlCodes; xmlCodes = (XmlDocument) HttpRuntime.Cache.Get( sKey ); if (xmlCodes == null) { xmlCodes = SqlHelper.ExecuteXml(new dn("Nodes", "Node"), "Get_Stuff_From_Database", sKey); HttpRuntime.Cache.Add(sKey, xmlCodes, null, DateTime.UtcNow.AddMinutes(1.0), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Normal, null); } return xmlCodes; }
What this example does:
The GetStuff method accepts a string parameter, which is used to retrieve a set of items from the database. First, the method checks to see if the XmlDocument in the cache is indexed by the parameter key. If so, it simply returns this object if it does not query the database. After he retrieved the document from the database, he placed it in the cache. If this method is called again at the appointed time, it will receive the object, and not get into the database.
Neel May 28 '16 at 15:46 2016-05-28 15:46
source share