HttpContext.Cache和HttpRuntime.Cache的區別

來源:互聯網
上載者:User

看了這篇文章HttpContext.Cache和HttpRuntime.Cache

然後用.NET Reflector查看了源碼:HttpContext類中返回的就是HttpRuntime.Cache

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

把原文粘貼一下,以便參考:

Asp.Net中可以方便的使用緩衝,對於Cache,一般有兩種方式調用:HttpContext.Cache和HttpRuntime.Cache。那麼這兩種Cache有什麼區別呢?

先來看看Msdn上的注釋:
HttpRuntime.Cache:擷取當前應用程式的 Cache。
HttpContext.Cache:為當前 HTTP 要求擷取 Cache 對象。

那麼是不是說對於HttpRuntime.Cache就是應用程式級,而HttpContext.Cache則是針對每個使用者的呢?NO,而實際上,兩者調用的是同一個對象。他們的區別僅僅在於調用方式不一樣(就我所知)。

事實勝過雄辯,寫個例子來證實一下(限於篇幅僅貼出關鍵代碼,完整代碼見附件WebDemo.rar):

/// <summary>/// 通過HttpRuntime.Cache的方式來儲存Cache/// </summary>private void btnHttpRuntimeCacheSave_Click(object sender, System.EventArgs e){            HttpRuntime.Cache.Insert(cacheKey, cacheValue, null, DateTime.Now.AddMinutes(3), TimeSpan.Zero);        }/// <summary>/// 通過HttpRuntime.Cache的方式來讀取Cache/// </summary>private void btnHttpRuntimeCacheLoad_Click(object sender, System.EventArgs e){if (HttpRuntime.Cache[cacheKey] == null){                cacheContent = "No Cache";            }else{                cacheContent = (string)HttpRuntime.Cache[cacheKey];            }            lblCacheContent.Text = cacheContent;        }/// <summary>/// 通過HttpContext.Cache的方式來儲存Cache/// </summary>private void btnHttpContextCacheSave_Click(object sender, System.EventArgs e){            HttpContext.Current.Cache.Insert(cacheKey, cacheValue, null, DateTime.Now.AddMinutes(3), TimeSpan.Zero);        }/// <summary>/// 通過HttpContext.Cache的方式來讀取Cache/// </summary>private void btnHttpContextCacheLoad_Click(object sender, System.EventArgs e){if (HttpContext.Current.Cache[cacheKey] == null){                cacheContent = "No Cache";            }else{                cacheContent = (string)HttpContext.Current.Cache[cacheKey];            }            lblCacheContent.Text = cacheContent;        }
    通過這個例子可以很容易證明:

  1. HttpContext.Cache儲存的Cache,HttpContext.Cache和HttpRuntime.Cache都可以讀取。
  2. HttpRuntime.Cache儲存的Cache,HttpContext.Cache和HttpRuntime.Cache都可以讀取。
  3. 無論是哪個使用者通過什麼方式對Cache的改變,其他使用者無論用什麼方式讀取的Cache內容也會隨之變。


 

#回複: HttpContext.Cache和HttpRuntime.Cache 編輯1、HttpRuntime.Cache 相當於就是一個緩衝具體實作類別,這個類雖然被放在了 System.Web 命名空間下了。但是非 Web 應用程式也是可以拿來用的。 
2、HttpContext.Cache 是對上述緩衝類的封裝,由於封裝到了 HttpContext ,局限於只能在知道 HttpContext 下使用,即只能用於 Web 應用程式。2007-12-29 17:08:00 | [匿名:it民工]#回複: HttpContext.Cache和HttpRuntime.Cache 編輯這個測試等於廢話! 

還是這句話說得對! 

在HttpContext裡定義cache只是為了使用方便。在某些情況下,HttpContext還沒被建立出來,就只能用HttpRuntime.Cache。 2007-08-20 11:21:00 | [匿名:sb]#HttpContext.Cache和HttpRuntime.Cache 編輯HttpContext.Cache和HttpRuntime.Cache2007-06-21 23:55:00 | [匿名:mbskys]#HttpRuntime.Cache 與HttpContext.Current.Cache的疑問 編輯轉載自蟈蟈俊.net 已經有人說過這個話題,相關連結: HttpRuntime.Cachevs.HttpContext.Current.Cachehttp://weblogs.asp.n...2006-12-31 11:10:00 | [匿名:e旋風]#HttpRuntime.Cache vs. HttpContext.Current.Cache 編輯已經有人說過這個話題,相關連結: HttpRuntime.Cache vs. HttpContext.Current.Cachehttp://weblogs.asp.net/pjohnson/archive/2006/02/06/437559.aspx2006-09-24 05:16:00 | [匿名:ASP.NET Chinese Blogs]#re: HttpContext.Cache和HttpRuntime.Cache 編輯補充一下,怎樣使用不同的Session訪問同一個網站。可以使用不同進程的IE,或者不同的瀏覽器,或者多個機器。2006-07-25 10:29:00 | [匿名:ah]#re: HttpContext.Cache和HttpRuntime.Cache 編輯好像摟主說的不對。你的Demo代碼無法下載,我也無法驗證。 

先來看看Msdn上的注釋: 
HttpRuntime.Cache:擷取當前應用程式的 Cache。 
HttpContext.Cache:為當前 HTTP 要求擷取 Cache 對象。 

Msdn上的注釋 是對的。 

我自己測試過,如果使用不同的Session訪問HttpContext.Cache,結果不一樣;但是HttpRuntime.Cache 是一樣的。 

2006-07-25 10:26:00 | [匿名:ah]#re: HttpContext.Cache和HttpRuntime.Cache 編輯兩個東西調用的對象確實是同一個,這個估計是為了方便調用的,但是HttpRuntime.Cache在Web程式裡是去對能調用的,而HttpContext.Cache就不一定了,因為.net不是請求層級的程式,有些代碼是在HttpContext.Current為空白的情況下執行,這個時候就只能用HttpRuntime.Cache了2006-01-12 09:22:00 | [匿名:Jeffrey]#re: HttpContext.Cache和HttpRuntime.Cache 編輯什麼是cache 啊?2005-09-02 21:49:00 | [匿名:qq]#re:HttpContext.Cache和HttpRuntime.Cache 編輯^_~,pretty good!csharpsseeoo2005-05-18 12:53:00 | [匿名:照度計]#re: HttpContext.Cache和HttpRuntime.Cache 編輯兩個Cache是一樣的,只是同一個Cache的讀取方法放在不同類中一樣 
就好像Page.Request 和Context.Request一樣 
我覺得Cache是Application的增強版,因為它除了具有Application的功能外,還有時間處理,關聯等方法的整合2005-04-19 10:10:00 | [匿名:netsoul]#re:HttpContext.Cache和HttpRuntime.Cache 編輯^_^,Pretty Good!2005-04-16 04:54:00 | [匿名:細胞破碎儀]#re:HttpContext.Cache和HttpRuntime.Cache 編輯^_^,Pretty Good!2005-04-10 20:10:00 | [匿名:乳勻機]#re: HttpContext.Cache和HttpRuntime.Cache 編輯一個應用程式如何調用另外一個應用程式的Cache? 

在另一個應用程式中建立一個WebService. 
通過調用該Service可以獲得Cache.2005-03-31 15:04:00 | [匿名:LiQiang]#re: HttpContext.Cache和HttpRuntime.Cache 編輯是啊,在Global.asax裡面調用HttpContext.Cache是取不到東西的。這個也費了我半天才查出來,到最後不得不在Global類裡加了個靜態變數。2005-03-07 10:14:00 | [匿名:urtracker]#re: HttpContext.Cache和HttpRuntime.Cache 編輯@Wang Ting 
多數瀏覽器和控制項裝置的URL 長度限制為255 個字元 ,最多也就1K。2005-01-18 11:12:00 | [匿名:abin]#re: HttpContext.Cache和HttpRuntime.Cache 編輯@Wang Ting 

session好用,但是依賴瀏覽器。 

預設情況下,ASP.NET 使用 Cookie 來標識哪些請求屬於特定的會話。如果 Cookie 不可用,則可以通過將工作階段識別項添加到 URL 來跟蹤會話。 

URL的長度是有限制的。

2005-01-18 11:03:00 | [匿名:abin]#re: HttpContext.Cache和HttpRuntime.Cache 編輯System.Web.HttpContext: 

public Cache get_Cache() 

return HttpRuntime.Cache; 

... 

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

@abin 

“假設一下:如果一個論壇為應用登入使用者緩衝一些設定,如果沒有設定緩衝截止的時間,那麼它佔用的記憶體會一直變大…… ” 

那些東西最好存session。developer要對自己寫的代碼負責,如果想troubleshooting memory leak,歡迎聯絡Microsoft Product Support Service..經常看到有人在cache裡放幾百m的dataset.. 

@lone 

“一個應用程式如何調用另外一個應用程式的Cache? ” 

impossible,除非你用caching application block.. 2005-01-17 22:02:00 | [匿名:Wang Ting]#re: HttpContext.Cache和HttpRuntime.Cache 編輯HttpRuntime.Cache 還可用於自訂類訪問Cachem,今天為瞭解決這個問題花了好多時間 2005-01-17 18:23:00 | [匿名:阿不]#re: HttpContext.Cache和HttpRuntime.Cache 編輯一台伺服器上可不是一個Web Appliction2005-01-17 12:39:00 | [匿名:abin]#re: HttpContext.Cache和HttpRuntime.Cache 編輯個人看法:相對於現在的伺服器記憶體來說,緩衝佔用的記憶體並不算多,最重要看你是怎麼用的。 
2005-01-16 18:41:00 | [匿名:寶玉]#re: HttpContext.Cache和HttpRuntime.Cache 編輯寶玉, 
在MSDN上查了,這兩個Cache都屬於 
System.Web.Caching.Cache。 
結果自然就一樣了。 

但我有一點不明白,為什麼要弄成一樣呢,而且還是全域的,這樣如果不設定緩衝時間的話所有的變數都會存起來,直到運用程式被重新啟動。 
假設一下:如果一個論壇為應用登入使用者緩衝一些設定,如果沒有設定緩衝截止的時間,那麼它佔用的記憶體會一直變大…… 

看了要好好設定一下緩衝時間和位移量。 2005-01-16 15:28:00 | [匿名:abin]#re: HttpContext.Cache和HttpRuntime.Cache 編輯這個我不會:( 
我想只要你取到應用程式的Cache對象即可。2005-01-15 14:54:00 | [匿名:寶玉]#re: HttpContext.Cache和HttpRuntime.Cache 編輯一個應用程式如何調用另外一個應用程式的Cache? 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.