What are the differences between System. Web. HttpContext. Current. Cache and System. Web. HttpRuntime. Cache?

Source: Internet
Author: User
Tags server memory

System. Web. HttpContext. Current. Cache attribute -----> is a "property value"

System. Web. httpruntime. cache attribute --------------->Is a "property value"

 System. Web. caching. CacheWebCache = ***--------> Is a "class"

For example, protected static volatile System. Web. Caching. Cache webCache = System. Web. HttpRuntime. Cache;

========================================================== ============

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. Current. 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 ):

 

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>
/// Read the Cache through HttpRuntime. 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>
/// Read the Cache through HttpContext. 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. Current. cache, httpcontext. Current. cache, and httpruntime. cache can both be read.
  2. Both httpcontext. Current. 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.

Transferred from:Http://blog.joycode.com/dotey/archive/2005/01/15/43091.aspx

 

Message feedback:

========================================================== ==============

1. HttpRuntime. Cache is equivalent to a specific implementation class of Cache, which is placed in the System. Web namespace. However, non-Web applications can also be used.
2. HttpContext. Current. 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.

========================================================== ==============

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.

========================================================== ==============

Someone has already mentioned this topic. Link: HttpRuntime. Cache vs. HttpContext. Current. Cache
Http://weblogs.asp.net/pjohnson/archive/2006/02/06/437559.aspx

========================================================== ==============

It seems that I am not talking about it. 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. Current. Cache, the results are different, but HttpRuntime. Cache is the same.

========================================================== ==============

The objects called by two things are 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

========================================================== ==============

The two caches are the same, but the reading methods of the same cache are the same in different classes.
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.

========================================================== ==============

How does one application call the cache of another application?

Create a WebService in another application.
You can obtain the cache by calling this service.

========================================================== ==============

Yes, calling httpcontext. cache in global. asax won't get anything. It took me half a day to find out, and I had to add a static variable to the global class.

========================================================== ==============

The URL Length of most browsers and control devices is limited to 255 characters, up to 1 K.

========================================================== ==============

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.

========================================================== ==============

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 ..

========================================================== ==============

HttpRuntime. Cache can also be used for custom class access to Cachem. Today it takes a lot of time to solve this problem.

========================================================== ==============

One server may not be a Web Appliction

========================================================== ==============

My 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.

========================================================== ==============

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.

========================================================== ==============

......

========================================================== ==============

 

 

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.