日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HttpContext.Current.Cache vs. HttpRuntime.Cache

發布時間:2025/3/20 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HttpContext.Current.Cache vs. HttpRuntime.Cache 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
.NET中Cache有兩種調用方式:HttpContext.Current.Cache 和 HttpRuntime.Cache,這兩種方式有什么區別呢?我們先看MSDN上的解釋:
??????HttpContext.Current.Cache:為當前 HTTP 請求獲取Cache對象。
??????HttpRuntime.Cache:獲取當前應用程序的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 vs. HttpRuntime.Cache的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。