Difference between HttpRuntime.Cache and HttpContext.Current.Cache?

What is the difference between HttpRuntime.Cache and HttpContext.Current.Cache ?

+61
May 14, '09 at
source share
3 answers

I find the following detail from http://theengineroom.provoke.co.nz/archive/2007/04/27/caching-using-httpruntime-cache.aspx

For caching, I studied using HttpContext.Current.Cache, but after reading other blogs, I found that caching was using HttpContext HttpRuntime.Cache to perform caching. The advantage of using HttpRuntime directly is that it is always available, for example, in Console applications and in the block tests.

Using HttpRuntime.Cache is simple. Objects can be cached and indexed by a string. Along with the key and the object for caching another important parameter is the expiration time. This parameter sets the time before deleting an object from the cache.

Here you have a good link.

Another good resource.

+61
May 14 '09 at 2:36 p.m.
source

Caching using HttpContext uses HttpRuntime.Cache for actual caching. The advantage of using HttpRuntime directly is that it is always available in console applications and in unit tests.

+18
May 14 '09 at 2:36 p.m.
source

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.

+1
May 28 '16 at 15:46
source



All Articles