The difference between cache HttpContext.Current.Cache and Httpruntime.cache

Source: Internet
Author: User
Tags httpcontext

Let's look at the explanations on MSDN:
HttpContext.Current.Cache: Gets the Cache object for the current HTTP request.
Httpruntime.cache: Gets the Cache for the current application.
We'll use the. NET Reflector tool to see the implementation of Httpcontext.cache and Httpruntime.cache:
//Httpcontext.cache and Httpruntime.cache implementations
//System.Web.HttpContext.Cache Property Implementation
PublicSealedclassHttpContext
{
PublicCache Cache
{
Get
{
returnHttpruntime.cache;
}
}
}


//System.Web.HttpRuntime.Cache Property Implementation
PublicSealedclassHttpRuntime
{
PublicStaticCache Cache
{
Get
{
if(Aspinstalldirectoryinternal = =NULL)
{
ThrowNewHttpException (SR. GetString ("aspnet_not_installed",NewObject[] {versioninfo.systemwebversion});
}
Cache cache = _theruntime._cachepublic;
if(Cache = =NULL)
{
Cacheinternal cacheinternal = cacheinternal;
Cachesection cachesection = Runtimeconfig.getappconfig (). Cache;
Cacheinternal.readcacheinternalconfig (cachesection);
_theruntime._cachepublic = Cacheinternal.cachepublic;
cache = _theruntime._cachepublic;
}
returnCache
}
}
}

As we can see from the code above, HttpContext.Current.Cache is implemented by calling Httpruntime.cache, and both points to the same cache object. So is there a difference between the two? Since two points to the same cache object, the difference can only occur on HttpContext and httpruntime. Let's look at the definitions of HttpContext and httpruntime in MSDN.
HttpContext: Encapsulates all HTTP-specific information about an individual HTTP request, HttpContext.Current gets the HttpContext object for the current HTTP request.
HttpRuntime: Provides a set of ASP. NET Runtime services for the current application.

As can be seen from the above definition: Httpruntime.cache equivalent to a cache-specific implementation class, although this class is placed in the System.Web namespace, but not under the Web application can also be used; HttpContext.Current.Cache is the encapsulation of the above cache class, by the is encapsulated in the HttpContext class and is limited to being used only in the knowledge of HttpContext, which can only be used for Web applications.

The following example can be a good illustration of this:

Examples of Httpcontext.cache and Httpruntime.cache
classCachetest
{
StaticvoidMain (string[] args)
{
System.Web.Caching.Cache Httpruntimecache = System.Web.HttpRuntime.Cache;
Httpruntimecache.insert ("Httpruntimecache","I am stored in Httpruntime.cache");

if(Httpruntimecache! =NULL)
{
Console.WriteLine ("Httpruntimecache:"+ httpruntimecache["Httpruntimecache"]);
}

System.Web.HttpContext HttpContext = System.Web.HttpContext.Current;
if(HttpContext = =NULL)
{
Console.WriteLine ("HttpContext object is a null in Console Project");
}
Else
{
System.Web.Caching.Cache Httpcontextcache = Httpcontext.cache;
Httpcontextcache.insert ("Httpcontextcache","I am stored in Httpruntime.cache");
if(Httpcontextcache = =NULL)
{
Console.WriteLine ("Httpcontextcache is null");
}
}

Console.ReadLine ();
}
}

Output result: httpruntimecache:i am StoredinchHttpruntime.cache
HttpContextObject isNULLinchConsole Project

All in all: When using the Cache, we use Httpruntime.cache as much as possible, both to reduce errors and to reduce one function call.

Reference: Httpruntime.cache and HttpContext.Current.Cache's Questions, Httpruntime.cache vs. HttpContext.Current.Cache

The difference between cache HttpContext.Current.Cache and Httpruntime.cache

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.