2012-09-04 29 views
1

我做了很多測試,我的MemoryCache實例在一段時間後被釋放,並且在我調用Get方法時總是返回null。我認爲這是在PollingInterval之後。MemoryCache進入處置狀態神奇

爲了模擬,你可以開始一個新的Web應用程序和編寫代碼:

private static MemoryCache mem = new MemoryCache("MyCache"); 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     string key = "MyKey"; 
     mem.Set(key, "MyValue", new CacheItemPolicy() {AbsoluteExpiration = DateTimeOffset.Now.AddDays(1), Priority = CacheItemPriority.NotRemovable}); 
     LtrValue.Text = mem.Get(key) as string; 
    } 

我配置給pollingInterval 30小時似乎工作。

我想可能是AppDomain卸載,但經過一番調查我不認爲這就是它:

1)MSDN說,使用的MemoryCache爲ASP.NET:http://msdn.microsoft.com/en-us/library/ff477235(v=vs.100).aspx 並說,ASP.NET 4高速緩存使用ObjectCache構建:

在ASP.NET 4中,使用ObjectCache類實現高速緩存。

2-)AppDomain卸載將重置靜態變量,創建新的MemoryCache實例。

3-)我附加了一個事件到AppDomain卸載像MemoryCache一樣(我看到了反編譯的源碼)在我的測試中,它沒有被調用,並且memorycache被丟棄。

參考文獻:

同一職位在MSDN論壇:http://social.msdn.microsoft.com/Forums/en/netfxbcl/thread/1233ffb3-6480-431b-94ca-1190f96cf5f6

http://social.msdn.microsoft.com/Forums/en/netfxbcl/thread/0a666d5e-9bda-47bd-8dd3-98d32eb5fe60

MemoryCache Empty : Returns null after being set

System.Runtime.Caching.MemoryCache strange behavior?

MemoryCache empty just after adding an object

MemoryCache does not obey memory limits in configuration

+0

[在Web應用程序中的MemoryCache棄置]的可能重複( http://stackoverflow.com/questions/7422859/memorycache-disposed-in-web-application) –

+0

Scott Hanselman報告.Net4中的一個bug可能會影響到你:http://stackoverflow.com/questions/7422859/memorycache -disposed功能於Web的應用程序 –

回答