Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Improved caching behaviour #42

MattWilcox opened this Issue Apr 8, 2012 · 3 comments


None yet
4 participants

MattWilcox commented Apr 8, 2012

Send different caching responses depending on whether the user had a cookie or not.

No cookie = cache, but also ensure the browser asks the server if there's a newer version on every load.
Cookie = cache with far-future expires and don't allow the browser to request if an updated version exists.

Right now AI always does the latter, It'd be good to do as planned once I get a more advanced Retina handling process working. This will solve an issue where cookie-race conditions caused a retina display to download a non-retina image and cache it, but may on the second request now have a cookie and benefit from a re-request.

@ghost ghost assigned MattWilcox Apr 8, 2012

rol423 commented Jun 25, 2013

These lines at the beginning of the sendImage function in addition to setting $browser_cache to zero should make the browser request if the file has changed:

$lastModified = filemtime($filename);
$headers =  getallheaders();
$refresh =  TRUE;
if (isset($headers["If-Modified-Since"])) {
    $arraySince =   explode(";", $headers["If-Modified-Since"]);
    $since =    strtotime($arraySince[0]);
    if($since >= $lastModified) {
        $refresh =  FALSE;
if (isset($headers["If-None-Match"])) {
    $refresh =  (strcmp($headers["If-None-Match"], "\"".md5($filename.$lastModified)."\"") <> 0) ?  TRUE :  FALSE;
header("ETag: \"".md5($filename.$lastModified)."\"");
header("Last-Modified: ".gmdate("D, d M Y H:i:s", $lastModified)." GMT");
if ($refresh == FALSE) {
    header("HTTP/1.1 304 Not changed");

The suggested optimization sending status 304 seems to work.

staabm commented Feb 28, 2014

@MattWilcox With If-None-Matchand ETag we could also enable public caching instead of the current private-only caching. With the ETag provided a public proxy can distinguish different images in different resolutions because of the provided http metadata (the etag).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment