設置
.NET 4.5,.NET MVC 4,IIS 8,C#將.NET用於Web應用程序時,如何在根(應用程序)級別緩存數據?
背景
我一直在使用HttpContext.Current.Cache
多年緩存以提高性能爲單個用戶會話構造的對象。這很好。
現在我需要在根級別緩存構造對象,以便任何瀏覽器中的任何用戶請求一個應該被緩存的信息的頁面,將從緩存中獲取該數據,而不是從數據中提取數據的新構造對象從後端。
因此,我將緩存從HttpContext.Current.Cache
更改爲HttpRuntime.Cache
。下面是添加對象緩存的例子:
/// <summary>
/// Insert value into the cache using
/// appropriate name/value pairs
/// </summary>
/// <typeparam name="T">Type of cached item</typeparam>
/// <param name="o">Item to be cached</param>
/// <param name="key">Name of item</param>
public static void Add<T>(T o, string key)
{
if (!string.IsNullOrEmpty(key))
{
// NOTE: Apply expiration parameters as you see fit.
HttpRuntime.Cache.Insert(
key,
o,
null,
DateTime.Now.AddDays(30),
Cache.NoSlidingExpiration);
}
}
的問題
實施在此之後,我裝我在Chrome桌面上的一個網頁,並期待它作爲第一個請求需要調用加載緩慢數據庫並構造我的對象。
然後,我去了另一臺電腦,並調用相同的頁面,並花了很長時間!在每個不同PC /瀏覽器上發出第一個請求後,它應該會很快照亮。這表明我的對象仍按每個會話進行緩存。
一個可能性
如果沒有人察覺明顯的錯誤我已經作出,如果可能我的問題是由於服務器集羣分發應用程序,我想知道。所以,如果有人對此有所瞭解,我正在使用Rackspace雲託管。