title: Cache objects don't expire ASP Net C#
body: <p>I am working on a caching feature for my web forms application.  I want to keep track of the number of times a user entered in a username (same or different) for purposes of blocking that IP.</p>
  
  <p>I use an absolute expiration of 1 minute so that the cache object expires after 1 minute from creation regardless of last access.</p>
  
  <p>However, when I run the application (using visual studio development server), the cache does not expire unless I rebuild the project.</p>
  
  <p>I searched online and found a few solutions, such as setting the sliding expiration to  <code>NoSlidingExpiration</code>, and also using a custom counter class to store the data.</p>
  
  <p>I also found a solution which uses the web.config cache element inside the <code>system.web</code> tags:</p>
  
  <pre><code>&lt;caching&gt;
    &lt;cache disableExpiration="false"/&gt;
  &lt;/caching&gt;
  </code></pre>
  
  <p>The cache still does not expire. Any help would be great. Here is the code I am using.</p>
  
  <pre><code>private int CacheCheck()
  {
      // Create the keys used and retrive the values from the cahce
      IncrementingCacheObject cacheUsername = Cache[KeyUsername] as IncrementingCacheObject;
      IncrementingCacheObject cacheDiffAttempts = Cache[KeyDiffAttempts] as IncrementingCacheObject;
      IncrementingCacheObject cacheSameAttempts = Cache[KeySameAttempts] as IncrementingCacheObject;
  
      // Check if the username exists in cache
      if (cacheUsername != null)
      {
          if (cacheUsername.Username.Equals(this.txtUserName.Text, StringComparison.InvariantCultureIgnoreCase))
          {
              if (cacheSameAttempts != null)
              {
                  cacheSameAttempts.Count++;
                  this.Cache[KeySameAttempts] = cacheSameAttempts;
              }
              else
              {
                  cacheSameAttempts = new IncrementingCacheObject { Count = 0, ExpireDate = DateTime.UtcNow.AddMinutes(1), Username = this.txtUserName.Text };
                  Cache.Insert(KeySameAttempts, cacheSameAttempts, null, cacheSameAttempts.ExpireDate, System.Web.Caching.Cache.NoSlidingExpiration);
              }
          }
          else
          {
              if (cacheDiffAttempts != null)
              {
                  cacheDiffAttempts.Count++;
                  this.Cache[KeyDiffAttempts] = cacheDiffAttempts;
              }
              else
              {
                  cacheDiffAttempts = new IncrementingCacheObject { Count = 0, ExpireDate = DateTime.UtcNow.AddMinutes(1), Username = this.txtUserName.Text };
                  Cache.Insert(KeyDiffAttempts, cacheDiffAttempts, null, cacheDiffAttempts.ExpireDate, System.Web.Caching.Cache.NoSlidingExpiration);
              }
          }
  
          // Set the last used username value in the Cache to the value in the text box
          cacheUsername.Username = this.txtUserName.Text;
          this.Cache[KeyUsername] = cacheUsername;
      }
      else
      {
          cacheUsername = new IncrementingCacheObject { Count = 0, ExpireDate = DateTime.UtcNow.AddMinutes(1), Username = this.txtUserName.Text };
          cacheDiffAttempts = new IncrementingCacheObject { Count = 0, ExpireDate = DateTime.UtcNow.AddMinutes(1), Username = this.txtUserName.Text };
          cacheSameAttempts = new IncrementingCacheObject { Count = 0, ExpireDate = DateTime.UtcNow.AddMinutes(1), Username = this.txtUserName.Text };
          Cache.Insert(KeyUsername, cacheUsername, null, cacheUsername.ExpireDate, System.Web.Caching.Cache.NoSlidingExpiration);
          Cache.Insert(KeySameAttempts, cacheSameAttempts, null, cacheSameAttempts.ExpireDate, System.Web.Caching.Cache.NoSlidingExpiration);
          Cache.Insert(KeyDiffAttempts, cacheDiffAttempts, null, cacheDiffAttempts.ExpireDate, System.Web.Caching.Cache.NoSlidingExpiration);
      }
  
      cacheUsername = Cache[KeyUsername] as IncrementingCacheObject;
      cacheDiffAttempts = Cache[KeyDiffAttempts] as IncrementingCacheObject;
      cacheSameAttempts = Cache[KeySameAttempts] as IncrementingCacheObject;
      // Check whether any log in attempts have surpassed the threshold and return a value to indicate what kind of log in was detected
      if (cacheDiffAttempts != null &amp;&amp; cacheDiffAttempts.Count &gt;= MaxLogAttemptsBeforeBan)
      {
          return 0;
      }
  
      if (cacheSameAttempts != null &amp;&amp; cacheSameAttempts.Count &gt;= MaxLogAttemptsBeforeBan)
      {
          return 1;
      }
  
      return -1;
  }
  
      /// &lt;summary&gt;
      /// This will reset the cache to blank/starting values.
      /// &lt;/summary&gt;
      private void ResetCache()
      {
          Cache.Remove(KeyUsername);
          Cache.Remove(KeySameAttempts);
          Cache.Remove(KeyDiffAttempts);
      }
  
      // Class used to store objects in the cache
      private class IncrementingCacheObject
      {
          public string Username;
          public int Count;
          public DateTime ExpireDate;
      }
  </code></pre>
  
  <p>When the user logs in successfully, before the page is redirected, it calls the ResetCache() method.</p>