0

How do I cache the images properly, I think asked this somewhere before, but it hasn't affected me until gameing site went out of beta.

It's HTTP headers or something isn't


Ok I used George's answer but frankly the performance is awful, asking the server for the image everytime (even when it doesn't download the image) creates a small delay of about 1/2 a second but because of the huge number of SE sites, the 1/2s add up.

Please, please consider adding a hash of the image to the stackauth.com/sites

1 Answer 1

1

Yes.

Here is an example HTTP request and the associated response received from retrieving this file.

GET /gaming/img/apple-touch-icon.png HTTP/1.1
Host: sstatic.net
User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.10) Gecko/20100915 Ubuntu/10.04 (lucid) Firefox/3.6.10
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
If-Modified-Since: Wed, 06 Oct 2010 19:18:55 GMT
If-None-Match: "145871518b65cb1:0"
Cache-Control: max-age=0

...and the response...


HTTP/1.1 200 OK
Cache-Control: max-age=604800
Content-Type: image/png
Last-Modified: Wed, 06 Oct 2010 19:14:02 GMT
Accept-Ranges: bytes
Etag: "9b7cfa28a65cb1:0"
X-Powered-By: ASP.NET
Access-Control-Allow-Origin: *
Date: Thu, 07 Oct 2010 21:41:32 GMT
Content-Length: 4012

Here is the relevant information:

  • In the request headers, notice the If-Modified-Since header. This tells the server to return the full content only if it has been modified since the given date.
  • In the response headers, notice the Cache-Control: max-age=604800 line. This tells the client that if the file in the browser's cache is older than this time period, then it should issue a new request.
  • Lastly, notice the Last-Modified header in the response. This indicates the date and time the file was last modified. This information is typically retrieved from the filesystem.
5
  • Would you say that the values in the request are called HTTP Header Fields? Thanks for your answer :)
    – Jonathan.
    Oct 8, 2010 at 7:36
  • Also no 100% sure what you mean about the Cache-Control.
    – Jonathan.
    Oct 8, 2010 at 15:51
  • @jonathan: Yup, you could call them HTTP header fields. Oct 8, 2010 at 17:31
  • I get Bad Request in return. Gee would this just be easier with a hash of the image provided in stackauth.com/sites.
    – Jonathan.
    Oct 8, 2010 at 20:26
  • @jonathan: I know :) That's the way they provide it, though. Nothing anyone can do about it. Oct 8, 2010 at 22:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .