Differences between HttpContext. Cache and HttpRuntime. Cache

Source: Internet
Author: User

I have read this article HttpContext. Cache and HttpRuntime. Cache

Then, use. NET Reflector to view the source code: HttpRuntime. Cache is returned in the HttpContext class.

public Cache Cache{    get    {        return HttpRuntime.Cache;    }}

Paste the original text for reference:

The Cache can be conveniently used in Asp. Net. For Cache, there are generally two methods to call: HttpContext. Cache and HttpRuntime. Cache. So what are the differences between the two types of Cache?

Let's take a look at the notes on Msdn:
HttpRuntime. Cache: Get the Cache of the current application.
HttpContext. Cache: gets the Cache object for the current HTTP request.

Does it mean that HttpRuntime. Cache is at the application level, while HttpContext. Cache is for each user? NO. In fact, the two call the same object. The difference is that the calling method is different (as far as I know ).

I have won the championship and wrote an example to prove the certificate (only the key code is attached to the Article. For the verification code, see the appendix webdemo.rar ):

/// <Summary> /// use httpruntime. cache method to save cache // </Summary> private void btnhttpruntimecachesave_click (Object sender, system. eventargs e) {httpruntime. cache. insert (cachekey, cachevalue, null, datetime. now. addminutes (3), timespan. zero);} // <summary> // use httpruntime. cache method to read cache // </Summary> private void btnhttpruntimecacheload_click (Object sender, system. eventargs e) {If (httpruntime. cache [cachekey] = NULL) {cachecontent = "no cache";} else {cachecontent = (string) httpruntime. cache [cachekey];} lblcachecontent. TEXT = cachecontent;} // <summary> // use httpcontext. cache method to save cache // </Summary> private void btnhttpcontextcachesave_click (Object sender, system. eventargs e) {httpcontext. current. cache. insert (cachekey, cachevalue, null, datetime. now. addminutes (3), timespan. zero);} // <summary> // use httpcontext. cache method to read cache // </Summary> private void btnhttpcontextcacheload_click (Object sender, system. eventargs e) {If (httpcontext. current. cache [cachekey] = NULL) {cachecontent = "no cache";} else {cachecontent = (string) httpcontext. current. cache [cachekey];} lblcachecontent. TEXT = cachecontent ;}
    Through this example, we can easily prove that:

  1. The Cache saved by HttpContext. Cache can be read by both HttpContext. Cache and HttpRuntime. Cache.
  2. Both HttpContext. Cache and HttpRuntime. Cache can be read.
  3. No matter which user changes the Cache, the content of the Cache read by other users will also change.


 

# Reply: HttpContext. Cache and HttpRuntime. Cache edit 1. HttpRuntime. Cache are equivalent to a specific Cache implementation class, which is placed in the System. Web namespace. However, non-Web applications can also be used.
2. HttpContext. Cache is the encapsulation of the preceding Cache class. Because it is encapsulated in HttpContext, it can only be used in HttpContext, that is, it can only be used for Web applications. 17:08:00 | [anonymous: it migrant workers] # reply: HttpContext. Cache and HttpRuntime. Cache edit this test is nonsense!

This sentence is correct!

The cache is defined in httpcontext for ease of use. In some cases, you can only use httpruntime. cache if httpcontext has not been created. 11:21:00 | [anonymous: Sb] # httpcontext. cache and httpruntime. cache edit httpcontext. cache and httpruntime. cache2007-06-21 23:55:00 | [anonymous: mbskys] # httpruntime. cache and httpcontext. current. the edited Question of the cache is reprinted from. net has already said this topic. Link: httpruntime. cachevs. httpcontext. current. cachehttp: // weblogs. ASP. n... 11:10:00 | [anonymous: E tornado] # httpruntime. cache. httpcontext. current. someone has already mentioned this topic in cache editing. Link: httpruntime. cache v S. httpcontext. current. cachehttp: // weblogs.asp.net/pjohnson/archive/2006/02/06/437559.aspx2006-09-24 05:16:00 | [anonymous: Asp. net Chinese Blogs] # Re: httpcontext. cache and httpruntime. the cache editor adds how to use different sessions to access the same website. You can use IE of different processes, different browsers, or multiple machines. 10:29:00 | [anonymous: Ah] # Re: httpcontext. cache and httpruntime. cache are edited incorrectly. Your demo Code cannot be downloaded or verified.

Let's take a look at the notes on msdn:
Httpruntime. cache: Get the cache of the current application.
Httpcontext. cache: gets the cache object for the current HTTP request.

The annotation on Msdn is correct.

I tested it myself. If different sessions are used to access HttpContext. Cache, the results are different, but HttpRuntime. Cache is the same.

10:26:00 | [anonymous: ah] # re: HttpContext. cache and HttpRuntime. the Cache edits two things. The called object is indeed the same, which is estimated to facilitate the call, but HttpRuntime. cache can be called in Web programs, while HttpContext. the Cache is not necessarily because. net is not a request-level program, and some code is in HttpContext. if Current is empty, HttpRuntime is used. cache 09:22:00 | [anonymous: Jeffrey] # re: HttpContext. cache and HttpRuntime. what is Cache? 21:49:00 | [anonymous: qq] # re: HttpContext. Cache and HttpRuntime. Cache edit ^ _~, Pretty good! Csharpsseeoo2005-05-18 12:53:00 | [anonymous: illumination meter] # re: HttpContext. Cache and HttpRuntime. Cache edit two caches are the same, but the same Cache read method is put in different classes the same
It's like Page. Request and Context. Request.
I think the Cache is an enhanced version of the Application, because it not only has the Application function, but also has the integration of time processing, association, and other methods 10:10:00 | [anonymous: netsoul] # re: HttpContext. cache and HttpRuntime. cache edit ^ _ ^, Pretty Good! 04:54:00 | [anonymous: cellular fragmentation meter] # re: HttpContext. Cache and HttpRuntime. Cache edit ^_^, Pretty Good! 20:10:00 | [anonymous: Milk homogenization machine] # re: HttpContext. Cache and HttpRuntime. Cache how to edit an application to call the Cache of another application?

Create a WebService in another application.
You can obtain Cache.2005-03-31 15:04:00 by calling this Service | [anonymous: LiQiang] # re: HttpContext. cache and HttpRuntime. cache editing. httpContext is called in asax. the Cache cannot get anything. It took me half a day to find out, and I had to add a static variable to the Global class. 10:14:00 | [anonymous: urtracker] # re: HttpContext. Cache and HttpRuntime. Cache edit @ Wang Ting
The URL Length of most browsers and control devices is limited to 255 characters, up to 1 K. 11:12:00 | [anonymous: abin] # re: HttpContext. Cache and HttpRuntime. Cache edit @ Wang Ting

The session is easy to use, but it depends on the browser.

By default, ASP. NET uses cookies to identify which requests belong to a specific session. If the Cookie is unavailable, you can trace the session by adding the session identifier to the URL.

The URL length is limited.

11:03:00 | [anonymous: abin] # re: HttpContext. Cache and HttpRuntime. Cache edit System. Web. HttpContext:

Public Cache get_Cache ()
{
Return HttpRuntime. Cache;
}

...

The cache is defined in HttpContext for ease of use. In some cases, you can only use HttpRuntime. Cache if HttpContext has not been created.

@ Abin

"Suppose that a forum caches some settings for the application login user. If the cache cutoff time is not set, the memory occupied by the Forum will keep increasing ...... "

It is best to store sessions for those things. The developer is responsible for the Code he has written. If you want to troubleshooting memory leak, please contact Microsoft Product Support Service. It is often seen that someone puts hundreds of MB of dataset in the cache ..

@ Lone

"How does one application call the Cache of another application? "

Impossible, unless you use caching application block .. 22:02:00 | [anonymous: Wang Ting] # re: HttpContext. cache and HttpRuntime. cache editing HttpRuntime. cache can also be used for custom class access to Cachem. Today, it takes a lot of time to solve this problem 18:23:00 | [anonymous: A-Nov] # re: HttpContext. cache and HttpRuntime. cache editing a server may not be a Web Appliction2005-01-17 12:39:00 | [anonymous: abin] # re: HttpContext. cache and HttpRuntime. cache editing's personal opinion: compared with the current server memory, the memory occupied by the Cache is not much. The most important thing is how you use it.
18:41:00 | [anonymous: Baoyu] # re: HttpContext. Cache and HttpRuntime. Cache edit Baoyu,
I checked on MSDN that both of these caches belong
System. Web. Caching. Cache.
The results are naturally the same.

But I don't understand why it should be the same and Global. In this way, if no cache time is set, all the variables will be saved until the application is restarted.
Suppose that a forum caches some settings for the application login user. If the cache cutoff time is not set, the memory usage will keep increasing ......

You should set the cache time and offset. 15:28:00 | [anonymous: abin] # re: HttpContext. Cache and HttpRuntime. Cache edit this. I won't :(
I think you just need to get the application's Cache object. 14:54:00 | [anonymous: Baoyu] # re: HttpContext. Cache and HttpRuntime. Cache edit an application. How do I call the Cache of another application?

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.