Static Content Global Caching Policy

  • Updated

Some customers look for a way to set global caching policy for static content.  This article provides information on a way to move forward implementing that option.

In these cases we recommend using a global asax to perform additional measures as sampled below. This code would be added to a global asax in the site root.

void Application_PreRequestHandlerExecute(object sender, EventArgs e)
    {
        Dictionary<String, HttpCookie> cookieCollection = new Dictionary<string, HttpCookie>();
        for (int i = HttpContext.Current.Request.Cookies.Count - 1; i >= 0; i--)
        {
            HttpCookie c = HttpContext.Current.Request.Cookies.Get(i);
            if (!cookieCollection.ContainsKey(c.Name))
            {
                cookieCollection.Add(c.Name, c);
            }
        }
        HttpContext.Current.Items.Add("RequestCookieCollection", cookieCollection);
    }
void Application_PostRequestHandlerExecute(object sender, EventArgs e)
    {
        Dictionary<String, HttpCookie> cookieCollection = new Dictionary<string, HttpCookie>();
        for (int i = HttpContext.Current.Response.Cookies.Count - 1; i >= 0; i--)
        {
            HttpCookie c = HttpContext.Current.Response.Cookies.Get(i);
            if (!cookieCollection.ContainsKey(c.Name))
            {
                cookieCollection.Add(c.Name, c);
            }
        }
        HttpContext.Current.Items.Add("ResponseCookieCollection", cookieCollection);
        Response.Cookies.Clear();
    }
 void Application_PreSendRequestHeaders(object sender, EventArgs e)
    {
        System.Web.HttpCacheability cachelevel = System.Web.HttpCacheability.Private;
        if (Request.FilePath.ToLower().StartsWith("/uploadedimages")) cachelevel = System.Web.HttpCacheability.Public;
        //if (Request.FilePath.ToLower().StartsWith("/uploadedassets")) cachelevel = System.Web.HttpCacheability.Public;
        if (Request.FilePath.ToLower().EndsWith(".js")) cachelevel = System.Web.HttpCacheability.Public;
        if (Request.FilePath.ToLower().EndsWith(".css")) cachelevel = System.Web.HttpCacheability.Public;
        if (Request.FilePath.ToLower().EndsWith(".gif")) cachelevel = System.Web.HttpCacheability.Public;
        if (Request.FilePath.ToLower().EndsWith(".png")) cachelevel = System.Web.HttpCacheability.Public;
        if (Request.FilePath.ToLower().EndsWith(".jpg")) cachelevel = System.Web.HttpCacheability.Public;

        if (HttpContext.Current.Items.Contains("ResponseCookieCollection"))
        {
            Dictionary<String, HttpCookie> ResponseCookieCollection = (Dictionary<String, HttpCookie>)HttpContext.Current.Items["ResponseCookieCollection"];
            if (HttpContext.Current.Items.Contains("RequestCookieCollection"))
            {
                Dictionary<String, HttpCookie> RequestCookieCollection = (Dictionary<String, HttpCookie>)HttpContext.Current.Items["RequestCookieCollection"];
                foreach (HttpCookie c in ResponseCookieCollection.Values)
                {
                    if (!RequestCookieCollection.ContainsKey(c.Name) || RequestCookieCollection[c.Name].Value != c.Value)
                    {
                        HttpContext.Current.Response.Cookies.Add(c);
                        cachelevel = System.Web.HttpCacheability.Private;
                    }
                }
            }
        }
        if (cachelevel == System.Web.HttpCacheability.Public) Response.Cache.SetCacheability(cachelevel);
    }


Additional options for max-age code can be found here.

This code segment in particular has relevance. Setting max-age in PreSendRequestHeaders worked.

var staticExtensions = new List<string> { ".js", ".css", ".jpg", ".gif", ".png" };


 if(staticExtensions.Any(ext => Request.FilePath.ToLower().EndsWith(ext))) 
{ 
Response.Cache.SetMaxAge(TimeSpan.FromDays(7)); 
}