ASP.NET:HttpRuntime.Cache节省WebConfigurationManager得到时刷新 - 这是一个错误? [英] ASP.NET: HttpRuntime.Cache getting flushed when saving WebConfigurationManager -- Is this a bug?
问题描述
最近,我一直在尝试使用缓存的对象与 HttpRuntime.Cache
我一般是IM pression下,如果我补充说:一些东西到缓存是这样的:
Recently, I've been experimenting with caching objects with HttpRuntime.Cache
and I was generally under the impression that if I "added" something to the cache like this:
HttpRuntime.Cache.Insert("Test", "This is a test!", null,
Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration,
CacheItemPriority.NotRemovable, new CacheItemRemovedCallback(FileChanged));
这在NotRemovable和NoExpiration标志将保持在存储器中的对象的应用程序的持续时间。但被发现在某些页面请求的最后,HttpRuntime.Cache会完全空的!
that the "NotRemovable" and "NoExpiration" flags would keep the object in-memory for the duration of the application. But was finding that at the end of certain page requests, the HttpRuntime.Cache would be completely empty!
跟踪直通和设置我的回调中的断点FileChanged我看得出来,确实让人感到删除我的目标(和高速缓存中所有其他对象),但我想不出为什么。所以最终,我开始禁用,我认为可能会影响这个子系统其他的东西。
Tracing thru and setting a breakpoint inside my Callback "FileChanged" I could see that indeed something was "removing" my object (and every other object in the cache), but I couldn't figure out why. So eventually, I started disabling other things that I thought might affect this subsystem.
最后,我注释掉行:
WebConfigurationManager.OpenWebConfiguration("~").Save;
,我已大多来自于的AppSettings区域的web.config检索数据,但偶尔写回的AppSettings和使用上述命令保存变更。我从阅读的web.config文件的缓存,但救了我变回它知道不应该刷新所有HttpRuntime.Cache的,对吧?
I had been mostly retrieving data from "web.config" in the AppSettings region, but occasionally writing back to AppSettings and saving the changes using the above command. I knew from reading that the "web.config" is cached, but saving my changes back to it shouldn't flush all of HttpRuntime.Cache, right?
这是一个错误吗?或者,这两个缓存不知何故,我应该知道的一种方式联系?
Is this a bug? Or are the two caches somehow linked in a way that I should be made aware of?
编辑:
好吧,我做了,如果有人想试试这个自己的机器上这个超级重复的。 (我运行VS2008 Pro的瓦特/ MVC2针对.NET 3.5)只启动一个新项目MVC2并粘贴在任何已经存在以下到的HomeController
:
Ok, I've made this super reproducible if anyone wants to try this on their own machine. (I'm running VS2008 Pro w/ MVC2 targeting .NET 3.5) Just start up a new MVC2 project and paste the following into the HomeController
over whatever is already there:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Configuration;
using System.Configuration;
namespace BackButtonTest.Controllers
{
[HandleError]
public class HomeController : Controller
{
private string MESSAGE = "Message";
public ActionResult Index()
{
ViewData[MESSAGE] = HttpRuntime.Cache.Get(MESSAGE);
Configuration Config = WebConfigurationManager.OpenWebConfiguration("~");
Config.AppSettings.Settings.Add("SomeKey", "SomeValue");
Config.Save();
return View();
}
public ActionResult About()
{
HttpRuntime.Cache[MESSAGE] = "This is pulled from the HttpRuntime.Cache";
return View();
}
}
}
然后按照下列步骤操作:
Then follow these steps:
- 开机调试模式下的应用程序。
- 点击关于链接。这将加载一个字符串到缓存中。
- 单击上的主页链接。字符串是从缓存拉和卡在ViewMessage字典。一些键/值对被写入到web.config文件并保存。从缓存中字符串应该出现在主页上。
- 再次点击主页链接。字符串的应的从缓存中拉出来,但事实并非如此。
- 停止程序。注释掉的3条线与配置开始。重新启动程序。
- 重新尝试步骤1 THRU 4。请注意如何HttpRuntime.Cache尚未清空。
- Start-up the app in debug mode.
- Click on the "About" link. This loads a string into the Cache.
- Click on the "Home" link. String is pulled from the Cache and stuck in ViewMessage dictionary. Some key/value pair is written to web.config and saved. String from cache should appear on the Home page.
- Click on the "Home" link again. String should be pulled from the cache, but it's not.
- Stop program. Comment out the 3 lines that start with "Config". Restart the program.
- Try steps 1 thru 4 again. Notice how the HttpRuntime.Cache has not been emptied.
奇怪的,是吧?
推荐答案
写你的web.config强制应用程序重新启动,而且,最有可能的,事件清空缓存,进程内的sessionState等将熄灭。试着写下一些日志在Global.asax中Application_End(或任何方法的名称是:))
Writing to your web.config will force the application to restart, and, most likely, events for emptying the cache, in-proc sessionstate etc. will go off. Try and write down some log in global.asax Application_End (or whatever the name of the method is :) )
这篇关于ASP.NET:HttpRuntime.Cache节省WebConfigurationManager得到时刷新 - 这是一个错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!