USE HttpRuntime.Cache OVER HttpContext.Current.Cache

來源:互聯網
上載者:User

緩衝是在ASP.NET開發中經常需要用到在技術,在使用過程中,通常會用到HttpRuntime.Cache和 HttpContext.Current.Cache。而且在使用過程中,通常會覺得這兩個似乎用哪一個都行,都能達到緩衝 資料的目的。那麼這兩個Cache到底有什麼不同呢?在什麼時候用哪一個比較好呢?這裡談談我的一些了 解和看法吧。

兩者的異同

先來看看msdn的解釋

HttpContext.Cache : Gets the ASP.NET Cache object for the current request

HttpContext.Current : Gets the HttpContext object for the current request

從這個解釋看,得到的Cache對象是當前請求的Cache對象。個人認為,這個解釋有點誤導性,會讓 人誤以為這時的Cache對象只是對於當前的Request的。而稍微想一下之後,就會發現這種理解是錯誤的。 如果Cache的內容只對當前的Request有用,那有何須緩衝呢?

既然msdn得解釋不行,那就直接看原始碼吧,^_^,用Reflector開啟HttpContext.Cache, 可以看到

public Cache Cache {       get       {             return HttpRuntime.Cache;       } }

從這代碼可以看出,兩者返回的都是是一樣的。

難道這兩者真的就完全一樣嗎?再來看看HttpContext.Current

public static HttpContext Current {       get       {             return (ContextBase.Current as HttpContext);       } }

ContextBase的原始碼:

internal static object Current {       get       {             return CallContext.HostContext;       } }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.