« Previous Next »

Thread: Http headers when using Output Caching

Last post 07-16-2009 12:43 PM by anilr. 3 replies.

Average Rating Rate It (5)

RSS

Page 1 of 1 (4 items)

Sort Posts:

  • 07-10-2009, 6:09 AM

    • ThierryV
    • Not Ranked
    • Joined on 12-16-2008, 2:33 PM
    • Posts 3

    Http headers when using Output Caching

    Hi,

    I am using the output caching feature from IIS 7 for a website serving mostly runtime-processed images. The output caching works really well; however, it does not remember the HTTP headers of the initial response, particularly the “Expires” one.

    Therefore, any client requesting an image served by the output cache doesn’t receive the HTTP expiration date, and will keep asking for that same image instead of caching it on the client side, thus degrading both the user experience and server performance. Is there a way I can force the output caching to cache the HTTP headers in addition to the actual response content?

    Regards,

    Thierry

  • 07-15-2009, 7:03 PM In reply to

    • anilr
    • Top 10 Contributor
    • Joined on 05-23-2006, 10:13 PM
    • Redmond, WA
    • Posts 2,343

    Re: Http headers when using Output Caching

    What is the exact output caching rule you have configured?  Also, exactly how are you adding the Expires header to the response?

    Anil Ruia
    Senior Software Design Engineer
    IIS Core Server
  • 07-16-2009, 5:24 AM In reply to

    • ThierryV
    • Not Ranked
    • Joined on 12-16-2008, 2:33 PM
    • Posts 3

    Re: Http headers when using Output Caching

    Hi Anil,

    This is what I do in the application code.

    A typical request is http://mysite/get-image.img?path=test.png&width=240&height=320&format=jpg&tag=xyz&expire=365
    Within “get-image.img” code, I have this:

     HttpCachePolicy cachePolicy = HttpContext.Current.Response.Cache;
     cachePolicy.SetCacheability(HttpCacheability.Public);
     cachePolicy.SetExpires(DateTime.Now.AddDays(expireDays));
     cachePolicy.SetValidUntilExpires(true);


    I checked the server response (prior to caching), and I do see the “expire” header being sent:
     Expires: Fri, 16 Jul 2010 09:00:06 GMT

    From the IIS manager / Output Caching, I added a custom extension “.img” (to match my get-image.img HttpHandler), with the following configuration:

    • User-Mode caching : At time intervals (6 hours)
      • Cache different versions of file base on : query string variable : “*” (I tried enumerating all the parameters, but It wasn’t working well with 8 variables)
    • Kernel-Mode caching : At time intervals (6 hours)
      Not sure why the kernel mode doesn’t require to set query string variable, but I tried this with User-Mode alone, or Kernel-Mode alone, and it didn’t fix my problem (another question, by the way, how can both modes be activated at the same time? Does one take precedence over the other?).

    So after a few request of the same URL, the response content gets cached, but when looking at the response, it’s missing the “expire” header. What should I do so that the header is also sent by the output caching?

    Best,

    Thierry

  • 07-16-2009, 12:43 PM In reply to

    • anilr
    • Top 10 Contributor
    • Joined on 05-23-2006, 10:13 PM
    • Redmond, WA
    • Posts 2,343

    Re: Http headers when using Output Caching

    The headers for the HttpCachePolicy are added by asp.net after the caching module runs and so are not part of the response that is cached.  If you want the headers to be cached, just use HttpResponse.SetHeader/AddHeader etc in your code.

    Anil Ruia
    Senior Software Design Engineer
    IIS Core Server
Page 1 of 1 (4 items)
Microsoft Communities