Caching problem

I have an aspx page, but all the content is created by hand (yes, I know I need to make a handler, I have another question)

I want to cache the output in the client browser. The problem is that it is only cached for one request.

        public static void ProceedCaching(string etag, string lastModify, string response, HttpResponse Response,
                                      HttpRequest Request)
    {
        Response.AddHeader("ETag", "\"" + etag + "\"");
        Response.AddHeader("Last-Modified", lastModify);
        Response.AppendHeader("Cache-Control", "Public");
        Response.AppendHeader("Expires",
                              DateTime.Now.AddMinutes(1).ToUniversalTime().ToString("r",DateTimeFormatInfo.InvariantInfo));

        string ifModified = Request.Headers["If-Modified-Since"];

        if (!string.IsNullOrEmpty(ifModified))
        {
            if (ifModified.Contains(";"))
                ifModified = ifModified.Remove(ifModified.IndexOf(';'));
        }

        string incomingEtag = Request.Headers["If-None-Match"];

        if (String.Compare(incomingEtag, etag) == 0 || string.Compare(ifModified, lastModify) == 0)
        {
            Response.StatusCode = 304;
            Response.End();
        }

        Response.Write(response);
        Response.End();
    }

      

it becomes preati dirty. As I said, it is cached only once. After receiving HTTP 304, the browser will send a clean request without caching information (etag, lastmodified). Any ideas?

0


source to share


2 answers


Found this answer here

In general, these are the most common rules that follow (don't worry if you don't understand the details, this will be explained below):



  • If the response headers tell the cache not to keep it, it won't.
  • If the request is authenticated or secure, it will not be cached.
  • If a validator (ETag or Last-Modified header) is present in the response and has no obvious freshness, it will be considered undecidable.
  • The cache view is considered fresh (that is, it can be sent to the client without being verified by the origin server) if:
    • The expiration time or other age control is set and is still in the period.
    • If the browser cache has already seen the view and was set to check after session.
    • If the proxy cache saw the last view and it was changed relatively long ago. Fresh submissions are served directly from the cache, without checking with the origin server.
  • If the view is out of date, the origin server will be prompted to check it or tell the cache whether it still has a good copy.

And Microsoft has a good article if you don't want to cache it.

0


source


Your code snippet works great for me if I remove the quotes you add around the etag on the first line. But I am guessing this is just a bug in the snippet and not the real problem you are facing.

Firefox 3 dosnt even bothers to hit the server after it got the first 304. IE7 keeps running, but it sends the lastmod / etag headers correctly and gets 304 every time.



Are you sure this is not because you changed the caching settings in your browser? I would try it in another browser to be sure.

To make it cleaner, you can use methods in Response.Caching rather than setting headers directly.

0


source







All Articles