Added Not-Modified support to fetcher; made caching more aggressive
authorDan
Thu, 29 Jan 2009 02:03:47 -0500
changeset 30 689b095d02cf
parent 29 205d0534a0f2
child 31 3e8d2acd7388
Added Not-Modified support to fetcher; made caching more aggressive
plugins/gallery/fetcher.php
--- a/plugins/gallery/fetcher.php	Thu Jan 29 01:30:09 2009 -0500
+++ b/plugins/gallery/fetcher.php	Thu Jan 29 02:03:47 2009 -0500
@@ -136,10 +136,25 @@
     die('Can\'t retrieve image file ' . $filename);
   
   $contents = file_get_contents($filename);
+  // expire images 30 days from now
+  $expiry = time() + ( 30 * 86400 );
   
   header('Content-type: '   . $mimetype);
   header('Content-length: ' . strlen($contents));
   header('Last-Modified: '  . date('r', $row['img_time_mod']));
+  header('Expires: ' . date('r', $expiry));
+  
+  // check for not-modified condition
+  if ( isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) )
+  {
+    $time = @strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']);
+    if ( ( !empty($time) && intval($row['img_time_mod']) <= $time ) || date('r', $row['img_time_mod']) === $_SERVER['HTTP_IF_MODIFIED_SINCE'] )
+    {
+      header('HTTP/1.1 304 Not Modified');
+      $db->close();
+      exit;
+    }
+  }
   
   if ( isset($_GET['download']) )
   {