Httpcontext. Current. cache vs. httpruntime. Cache

Source: Internet
Author: User
In. net, cache has two calling methods: httpcontext. Current. cache and httpruntime. cache. What are the differences between the two methods? Let's take a look at the explanations on msdn:
Httpcontext. Current. cache: gets the cache object for the current HTTP request.
Httpruntime. cache: Get the current application Program .

We can use the. NET reflector tool to check the implementation of httpcontext. cache and httpruntime. cache:

Httpcontext. cache and httpruntime. cache implementation
// System. Web. httpcontext. cache attribute implementation
Public   Sealed   Class Httpcontext
{
Public Cache
{
Get
{
ReturnHttpruntime. cache;
}
}
}


// System. Web. httpruntime. cache attribute implementation
Public   Sealed   Class Httpruntime
{
Public   Static Cache
{
Get
{
If (Aspinstalldirectoryinternal =   Null )
{
Throw   New Httpexception (Sr. getstring ( " Aspnet_not_installed " , New   Object [] {Versioninfo. systemwebversion} ));
}
Cache = _ Theruntime. _ cachepublic;
If (Cache =   Null )
{
Cacheinternal = Cacheinternal;
Cachesection = Runtimeconfig. getappconfig (). cache;
Cacheinternal. readcacheinternalconfig (cachesection );
_ Theruntime. _ cachepublic = Cacheinternal. cachepublic;
Cache = _ Theruntime. _ cachepublic;
}
Return Cache;
}
}
}

Through the aboveCodeWe can see that:Httpcontext. Current. cache is implemented by calling httpruntime. cache. The two point to the same cache object. Are there any differences between the two? Since the two point to the same cache object, the difference between the two can only appear in httpcontext and httpruntime.Let's take a look at the definitions of httpcontext and httpruntime in msdn.
Httpcontext: encapsulate all http-specific information about individual HTTP requests. httpcontext. Current is the current HTTP request to obtain the httpcontext object.
Httpruntime: provides a set of ASP. NET runtime services for the current application.

The above definition shows that: Httpruntime. the cache is equivalent to a specific implementation class of the cache, although this class is placed in the system. httpcontext. current. cache is the encapsulation of the preceding cache class. Because it is encapsulated into the httpcontext class, it can only be used in the httpcontext class, that is, it can only be used for Web applications.

The following example demonstrates this:Example of httpcontext. cache and httpruntime. Cache
Class Cachetest
{
Static   Void Main ( 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 = System. Web. httpcontext. Current;
If (Httpcontext =   Null )
{
Console. writeline ("Httpcontext object is 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 stored in httpruntime. Cache
Httpcontext object is null in console Project

To sum up, we try to use httpruntime. cache when using the cache, which reduces both errors and callback function calls.

References: questions about httpruntime. cache and httpcontext. Current. cache, httpruntime. cache vs. httpcontext. Current. 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.