Skip to content

Instantly share code, notes, and snippets.

@DavyLandman
Created September 10, 2010 12:46
Show Gist options
  • Save DavyLandman/573574 to your computer and use it in GitHub Desktop.
Save DavyLandman/573574 to your computer and use it in GitHub Desktop.
public class BrowserCacheAttribute : 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 bool PreventBrowserCaching
{
get;
set;
}
public BrowserCacheAttribute()
{
Duration = 10;
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
if (Duration < 0) return;
HttpCachePolicyBase cache = filterContext.HttpContext.Response.Cache;
if (PreventBrowserCaching)
{
cache.SetCacheability(HttpCacheability.NoCache);
Duration = 0;
}
else
{
cache.SetCacheability(HttpCacheability.Public);
}
TimeSpan cacheDuration = TimeSpan.FromSeconds(Duration);
cache.SetExpires(DateTime.Now.Add(cacheDuration));
cache.SetMaxAge(cacheDuration);
cache.AppendCacheExtension("must-revalidate,proxy-revalidate");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment