locked
Struggling with Headers: If-Modified-Since vs Last-Modified vs browser cache RRS feed

  • Question

  • User1118199527 posted

     I have a news site that is output by an .ashx page. Before my admins login, they go to the page (which stores the non-admin version in their browser cache), then they login and the ashx page returns slightly different admin html.  The problem is, they have to do a shift-refresh to clear their browser cache in order to see the different html output of the .ashx page.  I was wondering if there is a better way of caching this page, and also eliminating the need for the shift-refresh.

    In the code-behind on the ashx page, I make this method call before doing anything else, where I pass in the context, the lastModified DateTime of the page, and a bool indicating if the page should bypass the cache.  Of course, the problem is when the page is cached, the browser doesn't even make it back to this code-behind.

    public static void CheckIfUpdated(HttpContext context, DateTime lastModified, bool refresh)
    {
    if (!refresh)
    {
    context.Response.Cache.SetCacheability(HttpCacheability.Public);
    context.Response.Cache.SetExpires(DateTime.Now.AddMinutes(30));

    string clientLastModified = context.Request.Headers["Last-Modified"];

    if (clientLastModified != null)
    {
    DateTime dtClientLastModified = Convert.ToDateTime(clientLastModified);

    if (lastModified <= dtClientLastModified) // The file has not changed
    {
    context.Response.StatusCode = 304;
    context.Response.SuppressContent = true;
    context.Response.End();
    }
    }

    context.Response.Cache.SetLastModified(lastModified); // otherwise, the file has changed, so update the client's Last-Modified header
    }

    context.Response.StatusCode = 200; // this is set here because if any header cache is being set, then some sort of legitament content must have been found by the code that called this method.
    }
     

     Thanks for any help!

    Thursday, January 8, 2009 9:19 AM

Answers

  • User-1136466523 posted

    Hi,

    Based on your first post, you mentioned that you return your contents from your httphandler directly, here, do you mean you return the html markup to browser? If so, you can't use OUTPUTCACHE in that scenario. I think you need to create an aspx page, and display authenticated and non-authenticated contents in different panels, you can add the OUTPUTCACHE declaration in that page. And just add the Response.redirect or Server.Transfer method in your httphandler to redirect the request to that page.

    Thanks.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 13, 2009 11:24 PM

All replies

  • User-1136466523 posted

    Hi,

    From your description, it seems that you are using caching mechanism in your system. As you are asking if there's a better way to cache the page, i suggest that you can use OUTPUTCache instead.

    In your scenario, i think you can use the VaryByCustom property in OUTPUT Cache. Meanwhile add a value which indicates authenticated or not authenticated to the user's cookie. And then, override the GetVaryByCustomString event in Global.asax, so if the user logs in, the value in cookie will get changing, so it can remove the cache of the page you request.

    Thanks.

    Monday, January 12, 2009 10:56 PM
  • User1118199527 posted

     Thanks for your response. I am using ashx pages, insteadof aspx pages, and it doesn't appear that I can add something like:

    <%@ OutputCache Duration="60" VaryByParam="None" %>

    Is there another way of using OutputCache in a handler?

    Tuesday, January 13, 2009 8:34 AM
  • User-1136466523 posted

    Hi,

    Based on your first post, you mentioned that you return your contents from your httphandler directly, here, do you mean you return the html markup to browser? If so, you can't use OUTPUTCACHE in that scenario. I think you need to create an aspx page, and display authenticated and non-authenticated contents in different panels, you can add the OUTPUTCACHE declaration in that page. And just add the Response.redirect or Server.Transfer method in your httphandler to redirect the request to that page.

    Thanks.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 13, 2009 11:24 PM
  • User1118199527 posted

     I ended up scapping the browser cache, since the page changes very frequently each day.  Now I also don't have to worry about the admin scenario.  Thanks for your help!

    Wednesday, January 14, 2009 8:29 AM