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 current applicationProgram.
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 ).
Facts are better than words. Write an example to prove it.Code, Refer to the appendix webdemo.rar for the authorization code ):
/**/ /// <Summary>///Use httpruntime. cache to save the 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 to read the 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 to save the 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 to read the 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:
- The cache saved by httpcontext. cache can be read by both httpcontext. cache and httpruntime. cache.
- Both httpcontext. cache and httpruntime. cache can be read.
- 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. c Ache. 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. the cache editing server may not have a web appliction 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?