我有一個非常標準的MVC3應用程序。我試圖在緩存中存儲應用程序範圍內(而不是用戶範圍)的一些數據(在本例中爲Theme
對象/名稱)。在調試時(在與Visual Studio集成的開發服務器上),如果我調用SwitchTheme
,我會馬上看到新的主題。在IIS7上,任何主題都被緩存,保持緩存;它不更新到新的主題。緩存在IIS7上顯示舊值,而不是在調試服務器上
編輯:一些代碼:
public static Theme CurrentTheme { get {
Theme currentTheme = HttpContext.Current.Cache[CURRENT_THEME] as Theme;
if (currentTheme == null)
{
string themeName = DEFAULT_THEME;
try
{
WebsiteSetting ws = WebsiteSetting.First(w => w.Key == WebsiteSetting.CURRENT_THEME);
if (ws != null && !string.IsNullOrEmpty(ws.Value))
{
themeName = ws.Value;
}
}
catch (Exception e)
{
// DB not inited, or we're installing, or something broke.
// Don't panic, just use the default.
}
// Sets HttpContext.Current.Cache[CURRENT_THEME] = new themeName)
Theme.SwitchTo(themeName);
currentTheme = HttpContext.Current.Cache[CURRENT_THEME] as Theme;
}
return currentTheme;
} }
public static void SwitchTo(string name)
{
HttpContext.Current.Cache.Insert(CURRENT_THEME, new Theme(name), null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(30));
// Persist change to the DB.
// But don't do this if we didn't install the application yet.
try
{
WebsiteSetting themeSetting = WebsiteSetting.First(w => w.Key == WebsiteSetting.CURRENT_THEME);
if (themeSetting != null)
{
themeSetting.Value = name;
themeSetting.Save();
}
// No "else"; if it's not there, we're installing, or Health Check will take care of it.
}
catch (Exception e)
{
// DB not inited or install not complete. No worries, mate.
}
}
我不知道問題出在哪裏。我正在調用相同的方法並更新緩存;但IIS7只是向我展示了舊版本。
我可以在IIS中禁用輸出緩存,但這不是我想要做的。這看起來似乎是一個很好的解決辦法。
您需要張貼代碼示例。 – 2012-01-31 19:44:49
您是否在系統事件日誌中獲取應用程序池重置? (另外,您是否配置了IIS以將應用池重置記錄到事件日誌中)我曾經遇到過這個問題,這是因爲我們配置了虛擬內存限制太低。因此,每個請求都會重置會清空我的靜態對象的應用程序池。 – 2012-01-31 19:57:14
@Ek0nomik什麼樣的代碼?我可以展示很多東西。 web.config文件? – ashes999 2012-01-31 19:59:37