ASP.NET can be convenient to use cache, for Cache, generally two ways: httpContext.cache and httpruntime.cache. So what is the difference between these two cache?
Let's take a look at the Note on MSDN: httpruntime.cache: Get the Cache of the current application. HttpContext.cache: Get the Cache object for the current HTTP request.
So, is it that HTTPRUNTIME.CACHE is the application level, and HTTPCONTEXT.CACHE is for each user? NO, and in fact, the two calls the same object. Their difference is only different in the call mode (I know).
The fact is better than a else, write an example to confirm (limited to the number of critical code only, the full code see the attachment WebDemo.rar):
/ ** /
///
/// Save Cache by httpruntime.cache
/// summary>
Private
Void
BTNHTTPRUNTIMECACHESAVE_CLICK
Object
Sender, System.EventArgs E)
...
{
Httpruntime.cache.insert (Cachekey, Cachevalue, Null, DateTime.Now.addminutes (3), Timespan.zero;
}
/ ** /
///
/// read cache by 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;
}
/ ** /
///
/// Save Cache by httpcontext.cache
/// summary>
Private
Void
BTnhttpContextCachesave_click
Object
Sender, System.EventArgs E)
...
{
HttpContext.current.cache.insert (cachekey, cachevalue, null, datetime.now.addminutes (3), timeespan.zero;
}
/ ** /
///
/// read cache by 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;
}
This example can be easily proved:
HTTPCONTEXT.CACHE Saved Cache, HttpContext.cache and HttPruntime.cache are read. HttPruntime.cache Saved Cache, HttpContext.cache and HttPruntime.cache are read. No matter which user changes to cache, other users use the cache content read by other users.