title: Is browser cache clearable via code?
body: <p>I have a menu on my site that changes depending on whether the user is logged in or not. With browser caching, the menu "gets stuck" in either state and is confusing to users.</p>
  
  <p>They'll login, but the menu won't update because it's still cached in the unauthenticated state... and vice versa.</p>
  
  <p>How is this typically handled? Can we refresh the user's browser cache from our code? Or do I just not allow browser caching? (would rather use it, very nice bump in speed).</p>
  
  <p>Update</p>
  
  <p>Here's how I set client-side, browser caching in my asp.net mvc 2 app:</p>
  
  <pre><code>public class CacheFilterAttribute : ActionFilterAttribute {
      /// &lt;summary&gt;
      /// Gets or sets the cache duration in seconds. The default is 10 seconds.
      /// &lt;/summary&gt;
      /// &lt;value&gt;The cache duration in seconds.&lt;/value&gt;
      public int Duration { get; set; }
  
      public CacheFilterAttribute() { Duration = 10; }
  
      public override void OnActionExecuted(ActionExecutedContext filterContext) {
          if (Duration &lt;= 0) return;
  
          var cache = filterContext.HttpContext.Response.Cache;
          var cacheDuration = TimeSpan.FromSeconds(Duration);
  
          cache.SetCacheability(HttpCacheability.Public);
          cache.SetExpires(DateTime.Now.Add(cacheDuration));
          cache.SetMaxAge(cacheDuration);
          cache.AppendCacheExtension("must-revalidate, proxy-revalidate");
      }
  }
  </code></pre>
  
  <p>And then apply a [CachFilter(Duration = 60)] to my actions. (note, I got the code above from <a href="http://weblogs.asp.net/rashid/archive/2008/03/28/asp-net-mvc-action-filter-caching-and-compression.aspx" rel="nofollow noreferrer">Kazi Manzur Rashid's Blog</a> </p>