diff CacheTimeCheck.inc @ 99:d98e315308cd

Improved caching of flag and sitemap.
author Tom Fredrik "BFG" Klaussen <bfg@blenning.no>
date Sun, 14 Sep 2014 21:11:27 +0200
parents dd4ddedca4c5
children adf7b11921f4
line wrap: on
line diff
--- a/CacheTimeCheck.inc	Fri Oct 19 01:36:22 2012 +0200
+++ b/CacheTimeCheck.inc	Sun Sep 14 21:11:27 2014 +0200
@@ -6,6 +6,48 @@
 $cache = ScriptIncludeCache::instance(__FILE__);
 $cache->cache_time("${baseDir}/FileCacheSet.inc");
 
+function toGMTime($time) {
+  return gmdate('D, d M Y H:i:s', $time) . ' GMT';
+}
+
+/**
+ * Checks if a HTTP_IF_MODIFIED_SINCE header is set, if this file is
+ * newer, set a Last-Modified header, otherwise abort with an 304
+ * status code
+ */
+function CheckHttpModified($cache)
+{
+  if (DEBUG_LEVEL >= VERBOSITY_DEBUG)
+    var_dump($_SERVER);
+
+  $gmdate_mod = toGMTime($cache->getNewest());
+
+  if ($_SERVER['REDIRECT_URL'] == '/sitemap.xml') {
+    //print_r($gmdate_mod);
+  }
+
+  if(array_key_exists('HTTP_IF_MODIFIED_SINCE', $_SERVER)) {
+    $HTTP_IF_MODIFIED_SINCE = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
+    $if_modified_since = preg_replace('/;.*$/', '', $HTTP_IF_MODIFIED_SINCE);
+
+    if (strtotime($if_modified_since) >= $cache->getNewest()) {
+      header("HTTP/1.0 304 Not Modified");
+      foreach($cache->cacheControl() as $header => $value) {
+	header ("${header}: ${value}");
+      }
+      return false;
+    }
+  }
+
+  foreach($cache->cacheControl() as $header => $value) {
+    header ("${header}: ${value}");
+  }
+  header("Last-Modified: $gmdate_mod");
+  return true;
+}
+
+
+
 /**
  * CacheTimeCheck provides a set of functions to enable generating a
  * correct time for the latest update for a given file.
@@ -20,6 +62,7 @@
     if ($filename)
       $this->cache_time($filename);
     $this->cache_time(__FILE__);
+    $this->max_age=0;
   }
 
   public function addParent($cache)
@@ -28,31 +71,6 @@
   }
 
   /**
-   * Checks if a HTTP_IF_MODIFIED_SINCE header is set, if this file is
-   * newer, set a Last-Modified header, otherwise abort with an 304
-   * status code
-   */
-  function CheckHttpModified()
-  {
-    if (DEBUG_LEVEL >= VERBOSITY_DEBUG)
-      var_dump($_SERVER);
-
-    $gmdate_mod = gmdate('D, d M Y H:i:s', $this->getNewest()) . ' GMT';
-
-    if(array_key_exists('HTTP_IF_MODIFIED_SINCE', $_SERVER)) {
-      $HTTP_IF_MODIFIED_SINCE = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
-      $if_modified_since = preg_replace('/;.*$/', '', $HTTP_IF_MODIFIED_SINCE);
-
-      if (strtotime($if_modified_since) >= $this->getNewest()) {
-	header("HTTP/1.0 304 Not Modified");
-	exit;
-      }
-    }
-
-    header("Last-Modified: $gmdate_mod");
-  }
-
-  /**
    * Convenience function to load a file, and add it to the cacheset
    *
    * @param $path path of the file
@@ -63,5 +81,6 @@
     $this->cache_time($path);
     return loadFile($path);
   }
+
 }
 ?>