HttpContext.Current.Cache 和HttpRuntime.Cache的區(qū)別
廣告:
.NET中Cache有兩種調(diào)用方式:HttpContext.Current.Cache 和 HttpRuntime.Cache,這兩種方式有什么區(qū)別呢?我們先看MSDN上的解釋:
1. 先來(lái)看看Msdn上的注釋:
HttpRuntime.Cache:獲取當(dāng)前應(yīng)用程序的 Cache。
HttpContext.Cache:為當(dāng)前 HTTP 請(qǐng)求獲取 Cache 對(duì)象。
2. 在HttpContext里定義cache只是為了使用方便。在某些情況下,HttpContext還沒(méi)被創(chuàng)建出來(lái),就只能用HttpRuntime.Cache
3.兩個(gè)東西調(diào)用的對(duì)象確實(shí)是同一個(gè),這個(gè)估計(jì)是為了方便調(diào)用的,但是HttpRuntime.Cache在Web程序里是去對(duì)能調(diào)用的,而HttpContext.Cache就不一定了,因?yàn)?net不是請(qǐng)求級(jí)別的程序,有些代碼是在HttpContext.Current為空的情況下執(zhí)行,這個(gè)時(shí)候就只能用HttpRuntime.Cache了
4.這兩個(gè)Cache都屬于System.Web.Caching.Cache。 結(jié)果自然就一樣了
調(diào)用:Cache cache = System.Web.HttpContext.Current.Cache;
我們?cè)儆?NET Reflector工具看看HttpContext.Cache和HttpRuntime.Cache的實(shí)現(xiàn):
HttpContext.Cache和HttpRuntime.Cache實(shí)現(xiàn)
//System.Web.HttpContext.Cache屬性實(shí)現(xiàn)
public sealed class HttpContext
{
public Cache Cache
{
get
{
return HttpRuntime.Cache;
}
}
}
//System.Web.HttpRuntime.Cache屬性實(shí)現(xiàn)
public sealed class HttpRuntime
{
public static Cache Cache
{
get
{
if (AspInstallDirectoryInternal == null)
{
throw new HttpException(SR.GetString("Aspnet_not_installed", new object[] { 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;
}
return cache;
}
}
}
通過(guò)上面的代碼我們可以看出:HttpContext.Current.Cache是調(diào)用HttpRuntime.Cache實(shí)現(xiàn)的,兩者指向同一Cache對(duì)象。那么兩者到底有沒(méi)有區(qū)別的?既然兩個(gè)指向的是同一Cache對(duì)象,兩者的差別只能出現(xiàn)在HttpContext和HttpRuntime上了。我們?cè)賮?lái)看看MSDN中HttpContext和HttpRuntime的定義。
HttpContext:封裝有關(guān)個(gè)別HTTP請(qǐng)求的所有HTTP特定的信息,HttpContext.Current為當(dāng)前的HTTP請(qǐng)求獲取HttpContext對(duì)象。
HttpRuntime:為當(dāng)前應(yīng)用程序提供一組ASP.NET運(yùn)行時(shí)服務(wù)。
由上面的定義可以看出:HttpRuntime.Cache相當(dāng)于就是一個(gè)緩存具體實(shí)現(xiàn)類,這個(gè)類雖然被放在了System.Web命名空間下,但是非Web應(yīng)用下也是可以使用;HttpContext.Current.Cache是對(duì)上述緩存類的封裝,由于封裝到了HttpContext類中,局限于只能在知道HttpContext下使用,即只能用于Web應(yīng)用。
下面的例子可以很好的說(shuō)明這一點(diǎn):
HttpContext.Cache和HttpRuntime.Cache的示例
class CacheTest
{
static void Main(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 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();
}
}
輸出結(jié)果:httpRuntimeCache:I am stored in HttpRuntime.Cache
HttpContext object is null in Console Project
綜上:我們?cè)谑褂肅ache時(shí),盡量使用HttpRuntime.Cache,既能減少出錯(cuò),也減少了一次函數(shù)調(diào)用。
參考資料:HttpRuntime.Cache 與HttpContext.Current.Cache的疑問(wèn),HttpRuntime.Cache vs. HttpContext.Current.Cache
廣告: