我有一个非常标准的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