title: MemoryCache empty just after adding an object
body: <p>I'm facing a strange problem with ASP.NET MemoryCaching in a MVC 3 ASP.NET application.</p>
  
  <p>Each time an action is executed, I check if its LoginInfo are actually stored in the MemoryCache (code has been simplified, but core is as follow):</p>
  
  <pre><code>[NonAction]
  protected override void OnAuthorization(AuthorizationContext filterContext) {
    Boolean autorizzato = false;
    LoginInfo me = CacheUtils.GetLoginData(User.Identity.Name);
    if (me == null)
    {
      me = LoginData.UserLogin(User.Identity.Name);
      CacheUtils.SetLoginInfo(User.Identity.Name, me);
    }
    // Test if the object is really in the memory cache
    if (CacheUtils.GetLoginData(User.Identity.Name) == null) {
       throw new Exception("IMPOSSIBLE");
    } 
  }
  </code></pre>
  
  <p>The GetLoginInfo is:</p>
  
  <pre><code> public static LoginInfo GetLoginData(String Username)
          {
              LoginInfo local = null;
              ObjectCache cache = MemoryCache.Default;
              if (cache.Contains(Username.ToUpper()))
              {
                  local = (LoginInfo)cache.Get(Username.ToUpper());
              }
              else
              {
                  log.Warn("User " + Username + " not found  in cache");
              }
              return local;
          }
  </code></pre>
  
  <p>The SetLoginInfo is:</p>
  
  <pre><code>        public static void SetLoginInfo (String Username, LoginInfo Info)
          {
              ObjectCache cache = MemoryCache.Default;
              if ((Username != null) &amp;&amp; (Info != null))
              {
                  if (cache.Contains(Username.ToUpper()))
                  {
                      cache.Remove(Username.ToUpper());
                  }
                  cache.Add(Username.ToUpper(), Info, new CacheItemPolicy());
              }
              else
              {
                  log.Error("NotFound...");
              }
         }
  </code></pre>
  
  <p>The code is pretty straightforward, but sometimes (totally randomly), just after adding the LoginInfo to the MemoryCache, this results empty, the just added Object is not present, therefore I got the Exception.</p>
  
  <p>I'm testing this both on Cassini and IIS 7, it seems not related to AppPool reusability (enabled in IIS 7), I've tested with several Caching policies, but cannot make it work</p>
  
  <p>What Am I missing/Failing ?</p>
  
  <p>PS: forgive me for my bad english</p>