Created
September 10, 2010 12:46
-
-
Save DavyLandman/573574 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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