国产老熟女高潮毛片A片仙踪林,欧美喂奶吃大乳,狠狠爱无码一区二区三区,女神的私人医生动漫免费阅读

新聞建站cms系統、政府cms系統定制開發

廣州網站建設公司-閱速公司

asp.net新聞發布系統、報紙數字報系統方案
/
http://www.tjsimaide.com/
廣州網站建設公司
您當前位置:首頁>ASP.NET MVC

ASP.NET MVC

HttpContext.Current.Cache 和HttpRuntime.Cache的區別

發布時間:2009/6/29 11:02:03  作者:  閱讀:1410  

廣告:

.NET中Cache有兩種調用方式:HttpContext.Current.Cache 和 HttpRuntime.Cache,這兩種方式有什么區別呢?我們先看MSDN上的解釋:

1. 先來看看Msdn上的注釋:
HttpRuntime.Cache:獲取當前應用程序的 Cache。
HttpContext.Cache:為當前 HTTP 請求獲取 Cache 對象。

2. 在HttpContext里定義cache只是為了使用方便。在某些情況下,HttpContext還沒被創建出來,就只能用HttpRuntime.Cache

3.兩個東西調用的對象確實是同一個,這個估計是為了方便調用的,但是HttpRuntime.Cache在Web程序里是去對能調用的,而HttpContext.Cache就不一定了,因為.net不是請求級別的程序,有些代碼是在HttpContext.Current為空的情況下執行,這個時候就只能用HttpRuntime.Cache了

4.這兩個Cache都屬于System.Web.Caching.Cache。 結果自然就一樣了

調用:Cache cache = System.Web.HttpContext.Current.Cache;

我們再用.NET Reflector工具看看HttpContext.Cache和HttpRuntime.Cache的實現:

HttpContext.Cache和HttpRuntime.Cache實現
//System.Web.HttpContext.Cache屬性實現
public sealed class HttpContext
{
public Cache Cache
{
get
{
return HttpRuntime.Cache;
}
}
}


//System.Web.HttpRuntime.Cache屬性實現
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;
}
}
}

通過上面的代碼我們可以看出:HttpContext.Current.Cache是調用HttpRuntime.Cache實現的,兩者指向同一Cache對象。那么兩者到底有沒有區別的?既然兩個指向的是同一Cache對象,兩者的差別只能出現在HttpContext和HttpRuntime上了。我們再來看看MSDN中HttpContext和HttpRuntime的定義。
HttpContext:封裝有關個別HTTP請求的所有HTTP特定的信息,HttpContext.Current為當前的HTTP請求獲取HttpContext對象。
HttpRuntime:為當前應用程序提供一組ASP.NET運行時服務。

由上面的定義可以看出:HttpRuntime.Cache相當于就是一個緩存具體實現類,這個類雖然被放在了System.Web命名空間下,但是非Web應用下也是可以使用;HttpContext.Current.Cache是對上述緩存類的封裝,由于封裝到了HttpContext類中,局限于只能在知道HttpContext下使用,即只能用于Web應用。

下面的例子可以很好的說明這一點:

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();
}
}

輸出結果:httpRuntimeCache:I am stored in HttpRuntime.Cache
HttpContext object is null in Console Project

綜上:我們在使用Cache時,盡量使用HttpRuntime.Cache,既能減少出錯,也減少了一次函數調用。

參考資料:HttpRuntime.Cache 與HttpContext.Current.Cache的疑問,HttpRuntime.Cache vs. HttpContext.Current.Cache

廣告:

相關文章
HttpContext.Current.Cache 和HttpRuntime.Cache的區別
cms新聞系統購買咨詢
掃描關注 廣州閱速軟件科技有限公司
掃描關注 廣州閱速科技
主站蜘蛛池模板: 平泉县| 西乌珠穆沁旗| 武冈市| 武平县| 闽清县| 双柏县| 枣强县| 桓台县| 鹤岗市| 儋州市| 开阳县| 漯河市| 富平县| 马鞍山市| 景泰县| 东乡县| 宜兰市| 霍林郭勒市| 葵青区| 贵港市| 凤冈县| 乐至县| 曲周县| 本溪市| 汶上县| 治县。| 宜良县| 成安县| 永昌县| 长顺县| 策勒县| 峨边| 景谷| 壶关县| 二连浩特市| 怀远县| 安溪县| 安远县| 横峰县| 东明县| 溧水县|