亚洲3P视频,日韩BBW无码,亚洲制服麻豆网站,88re伊人,九草精品视频在线观看,国产精品久久夜,色青青狠狠色,无码熟女一区二区三区,日本一区二区成人网站

新聞建站cms系統(tǒng)、政府cms系統(tǒng)定制開發(fā)

廣州網(wǎng)站建設(shè)公司-閱速公司

asp.net新聞發(fā)布系統(tǒng)、報紙數(shù)字報系統(tǒng)方案
/
http://m.duxiu2008.cn/
廣州網(wǎng)站建設(shè)公司
您當(dāng)前位置:首頁>ASP.NET MVC

ASP.NET MVC

HttpContext.Current.Cache 和HttpRuntime.Cache的區(qū)別

發(fā)布時間:2009/6/29 11:02:03  作者:  閱讀:1454  

廣告:

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

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

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

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

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

調(diào)用:Cache cache = System.Web.HttpContext.Current.Cache;

我們再用.NET Reflector工具看看HttpContext.Cache和HttpRuntime.Cache的實現(xiàn):

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


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

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

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

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

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

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

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

廣告:

相關(guān)文章
HttpContext.Current.Cache 和HttpRuntime.Cache的區(qū)別
cms新聞系統(tǒng)購買咨詢
掃描關(guān)注 廣州閱速軟件科技有限公司
掃描關(guān)注 廣州閱速科技
靖边县| 宜阳县| 类乌齐县| 蕉岭县| 保亭| 潞西市| 辛集市| 马公市| 五华县| 孝感市| 沙雅县| 麟游县| 昌江| 武川县| 郸城县| 海兴县| 敦煌市| 乐平市| 黄陵县| 新宁县| 尉氏县| 雅江县| 东乡族自治县| 兰州市| 北海市| 桐庐县| 团风县| 秭归县| 丰顺县| 绥化市| 东方市| 上栗县| 陕西省| 南城县| 廊坊市| 固阳县| 门头沟区| 堆龙德庆县| 东山县| 凤台县| 苏尼特左旗|