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.
They'll login, but the menu won't update because it's still cached in the unauthenticated state... and vice versa.
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).
Update
Here's how I set client-side, browser caching in my asp.net mvc 2 app:
public class CacheFilterAttribute : ActionFilterAttribute {
/// <summary>
/// Gets or sets the cache duration in seconds. The default is 10 seconds.
/// </summary>
/// <value>The cache duration in seconds.</value>
public int Duration { get; set; }
public CacheFilterAttribute() { Duration = 10; }
public override void OnActionExecuted(ActionExecutedContext filterContext) {
if (Duration <= 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");
}
}
And then apply a [CachFilter(Duration = 60)] to my actions. (note, I got the code above from Kazi Manzur Rashid's Blog