Let's look at the explanations on MSDN:
HttpContext.Current.Cache: Gets the Cache object for the current HTTP request.
Httpruntime.cache: Gets the Cache for the current application.
We'll use the. NET Reflector tool to see the implementation of Httpcontext.cache and Httpruntime.cache:
//Httpcontext.cache and Httpruntime.cache implementations
//System.Web.HttpContext.Cache Property Implementation
PublicSealedclassHttpContext
{
PublicCache Cache
{
Get
{
returnHttpruntime.cache;
}
}
}
//System.Web.HttpRuntime.Cache Property Implementation
PublicSealedclassHttpRuntime
{
PublicStaticCache Cache
{
Get
{
if(Aspinstalldirectoryinternal = =NULL)
{
ThrowNewHttpException (SR. GetString ("aspnet_not_installed",NewObject[] {versioninfo.systemwebversion});
}
Cache cache = _theruntime._cachepublic;
if(Cache = =NULL)
{
Cacheinternal cacheinternal = cacheinternal;
Cachesection cachesection = Runtimeconfig.getappconfig (). Cache;
Cacheinternal.readcacheinternalconfig (cachesection);
_theruntime._cachepublic = Cacheinternal.cachepublic;
cache = _theruntime._cachepublic;
}
returnCache
}
}
}
As we can see from the code above, HttpContext.Current.Cache is implemented by calling Httpruntime.cache, and both points to the same cache object. So is there a difference between the two? Since two points to the same cache object, the difference can only occur on HttpContext and httpruntime. Let's look at the definitions of HttpContext and httpruntime in MSDN.
HttpContext: Encapsulates all HTTP-specific information about an individual HTTP request, HttpContext.Current gets the HttpContext object for the current HTTP request.
HttpRuntime: Provides a set of ASP. NET Runtime services for the current application.
As can be seen from the above definition: Httpruntime.cache equivalent to a cache-specific implementation class, although this class is placed in the System.Web namespace, but not under the Web application can also be used; HttpContext.Current.Cache is the encapsulation of the above cache class, by the is encapsulated in the HttpContext class and is limited to being used only in the knowledge of HttpContext, which can only be used for Web applications.
The following example can be a good illustration of this:
Examples of Httpcontext.cache and Httpruntime.cache
classCachetest
{
StaticvoidMain (string[] args)
{
System.Web.Caching.Cache Httpruntimecache = System.Web.HttpRuntime.Cache;
Httpruntimecache.insert ("Httpruntimecache","I am stored in Httpruntime.cache");
if(Httpruntimecache! =NULL)
{
Console.WriteLine ("Httpruntimecache:"+ httpruntimecache["Httpruntimecache"]);
}
System.Web.HttpContext HttpContext = System.Web.HttpContext.Current;
if(HttpContext = =NULL)
{
Console.WriteLine ("HttpContext object is a null in Console Project");
}
Else
{
System.Web.Caching.Cache Httpcontextcache = Httpcontext.cache;
Httpcontextcache.insert ("Httpcontextcache","I am stored in Httpruntime.cache");
if(Httpcontextcache = =NULL)
{
Console.WriteLine ("Httpcontextcache is null");
}
}
Console.ReadLine ();
}
}
Output result: httpruntimecache:i am StoredinchHttpruntime.cache
HttpContextObject isNULLinchConsole Project
All in all: When using the Cache, we use Httpruntime.cache as much as possible, both to reduce errors and to reduce one function call.
Reference: Httpruntime.cache and HttpContext.Current.Cache's Questions, Httpruntime.cache vs. HttpContext.Current.Cache
The difference between cache HttpContext.Current.Cache and Httpruntime.cache