In. net, cache can be called in two ways.

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 cache of the current application.

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
{
Return httpruntime. 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 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 above code, we can see that httpcontext. Current. cache is implemented by calling httpruntime. cache, and 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 preceding definition shows 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.

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/avon520/archive/2009/11/25/4872704.aspx

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.