Use Httpruntime.cache over HttpContext.Current.Cache

Source: Internet
Author: User
Tags httpcontext

Caching is often used in asp.net development and in the process of using Httpruntime.cache and HttpContext.Current.Cache. And in the course of use, it is often felt that both of them seem to be in the same line, can achieve the purpose of caching data. So what's the difference between the two cache? At what time is it better to use which one? Here to talk about some of my understanding and view it.

Similarities and differences between the two

Let's look at the MSDN explanation.

HttpContext.Cache:Gets the ASP.net Cache object for the current request

HttpContext.Current:Gets the HttpContext object for the current request

From this explanation, the resulting cache object is the cache object of the current request. Personally, this explanation is a bit misleading and can make people think that the cache object is just for the current request. And after a little thought, you will find that this understanding is wrong. If the contents of the cache are only useful for the current request, why cache it?

Since MSDN has to explain no, then look directly at the source code, ^_^, with reflector open Httpcontext.cache, you can see

Public Cache Cache 

{

get 

{return 

httpruntime.cache; 

} 

}

As you can see from this code, both are returned in the same way.

Is the two really exactly the same? Let's look at HttpContext.Current.

public static HttpContext current 

{ 

get 

{ 

contextbase.current as Httpconte      XT); 

} 

}

Contextbase's source code:

Internal static object current 

{ 

get 

{return 

callcontext.hostcontext; 

} 

}

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.