title: ASP.NET: HttpRuntime.Cache getting flushed when saving WebConfigurationManager -- Is this a bug?
body: <p>Recently, I've been experimenting with caching objects with <code>HttpRuntime.Cache</code> and I was generally under the impression that if I "added" something to the cache like this:</p>
  
  <pre><code>HttpRuntime.Cache.Insert("Test", "This is a test!", null, 
       Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, 
       CacheItemPriority.NotRemovable, new CacheItemRemovedCallback(FileChanged));
  </code></pre>
  
  <p>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!</p>
  
  <p>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.</p>
  
  <p>Eventually, I commented out the line:</p>
  
  <pre><code>WebConfigurationManager.OpenWebConfiguration("~").Save;
  </code></pre>
  
  <p>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?</p>
  
  <p>Is this a bug? Or are the two caches somehow linked in a way that I should be made aware of?</p>
  
  <p><strong>EDIT:</strong> </p>
  
  <p>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 <code>HomeController</code> over whatever is already there:</p>
  
  <pre><code>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();
          }
      }
  }
  </code></pre>
  
  <p>Then follow these steps:</p>
  
  <ol>
  <li>Start-up the app in debug mode.</li>
  <li>Click on the "About" link. This loads a string into the Cache.</li>
  <li>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.</li>
  <li>Click on the "Home" link again. String <em>should</em> be pulled from the cache, but it's not.</li>
  <li>Stop program. Comment out the 3 lines that start with "Config". Restart the program.</li>
  <li>Try steps 1 thru 4 again. Notice how the HttpRuntime.Cache has not been emptied.</li>
  </ol>
  
  <p>Weird, huh?</p>