changeset 68:4dfa3f6a2dc1

Modify CacheTimeCheck to have a common parent, and two different implementations for script caches and file caches. Support for linkedcaches.
author Tom Fredrik "BFG" Klaussen <bfg@blenning.no>
date Thu, 11 Oct 2012 23:16:53 +0200
parents 37dee99c1f8c
children dd4ddedca4c5
files CacheTimeCheck.inc InputParser.inc common-functions.inc index.php
diffstat 4 files changed, 107 insertions(+), 62 deletions(-) [+]
line wrap: on
line diff
--- a/CacheTimeCheck.inc	Thu Oct 11 22:30:07 2012 +0200
+++ b/CacheTimeCheck.inc	Thu Oct 11 23:16:53 2012 +0200
@@ -3,27 +3,24 @@
 
 include_once 'Logger.inc';
 include_once 'common-functions.inc';
-$cache = CacheTimeCheck::instance(__FILE__);
+$cache = ScriptIncludeCache::instance(__FILE__);
 $cache->cache_time("${baseDir}/Logger.inc");
 $cache->cache_time("${baseDir}/common-functions.inc");
 
-/**
- * CacheTimeCheck provides a set of functions to enable generating a
- * correct time for the latest update for a given file.
- *
- * @author Tom Fredrik Blenning Klaussen
- */
-class CacheTimeCheck
-{
-  private $newest;
+class FileCacheSet {
+  private $newest = 0;
   private $files = array();
-  private static $myInstance = 0;
+  private $parentCaches = array();
 
-  private function __construct($filename = False)
+  protected function __construct($parent = null) {
+    if ($parent) {
+      $this->addParent($parent);
+    }
+  }
+
+  protected function addParent($parent)
   {
-    if ($filename)
-      $this->cache_time($filename);
-    $this->cache_time(__FILE__);
+    array_push($this->parentCaches, $parent);
   }
 
   /**
@@ -36,6 +33,9 @@
   function cacheSet($humanReadable = False)
   {
     $retVal = array();
+    foreach ($this->parentCaches as $parent) {
+      $retVal = array_merge($retVal, $parent->cacheSet($humanReadable));
+    }
     foreach($this->files as $file) {
       $mtime = filemtime($file);
       if ($humanReadable)
@@ -47,47 +47,6 @@
   }
 
   /**
-   * Generates a singleton instance of this CacheTimeCheck
-   *
-   * @param $filename an optional file to include in the cacheset
-   *
-   * @return a CacheTimeCheck object
-   */
-  function instance($filename = False)
-  {
-    if (! self::$myInstance)
-      self::$myInstance = new self($filename);
-    elseif ($filename)
-      self::$myInstance->cache_time($filename);
-    return self::$myInstance;
-  }
-
-  /**
-   * 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->newest) . ' 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->newest) {
-	header("HTTP/1.0 304 Not Modified");
-	exit;
-      }
-    }
-
-    header("Last-Modified: $gmdate_mod");
-  }
-
-  /**
    * Include a file in the cacheset
    *
    * @param $path the path of the file
@@ -106,6 +65,45 @@
     }
   }
 
+  function getNewest()
+  {
+    $newest = 0;
+    foreach ($this->parentCaches as $parent) {
+      $newest = max($newest, $parent->getNewest());
+    }
+    $newest = max($newest, $this->newest);
+    return $newest;
+  }
+}
+
+class ScriptIncludeCache extends FileCacheSet
+{
+  private static $myInstance = 0;
+
+  protected function __construct($filename = False)
+  {
+    parent::__construct();
+    if ($filename)
+      $this->cache_time($filename);
+    $this->cache_time(__FILE__);
+  }
+
+  /**
+   * Generates a singleton instance of this CacheTimeCheck
+   *
+   * @param $filename an optional file to include in the cacheset
+   *
+   * @return a CacheTimeCheck object
+   */
+  function instance($filename = False)
+  {
+    if (! self::$myInstance)
+      self::$myInstance = new self($filename);
+    elseif ($filename)
+      self::$myInstance->cache_time($filename);
+    return self::$myInstance;
+  }
+
   /**
    * Convenience function to include a file, and add it to the cacheset.
    *
@@ -119,6 +117,53 @@
     $this->cache_time($path);
     include_once($path);
   }
+}
+
+/**
+ * CacheTimeCheck provides a set of functions to enable generating a
+ * correct time for the latest update for a given file.
+ *
+ * @author Tom Fredrik Blenning Klaussen
+ */
+class CacheTimeCheck extends FileCacheSet
+{
+  function __construct($filename = False)
+  {
+    parent::__construct();
+    if ($filename)
+      $this->cache_time($filename);
+    $this->cache_time(__FILE__);
+  }
+
+  public function addParent($cache)
+  {
+    parent::addParent($cache);
+  }
+
+  /**
+   * 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
--- a/InputParser.inc	Thu Oct 11 22:30:07 2012 +0200
+++ b/InputParser.inc	Thu Oct 11 23:16:53 2012 +0200
@@ -17,8 +17,8 @@
    */
   function __construct($name, $masterCache) {
     $this->master = new DOMDocument();
-    $cache = $masterCache;
-    $cache->cache_time($name);
+    $cache = new CacheTimeCheck($name);
+    $cache->addParent($masterCache);
     $this->master->load($name);
 
     $this->options = new Options($this->master);
@@ -63,11 +63,11 @@
    */
   function genPage()
   {
+    //print_r($this->options->getCache()->cacheSet(1));
+    //exit;
     if (CACHING && $this->options->getCacheable())
       $this->options->getCache()->CheckHttpModified();
-
     print $this->master->saveXml($this->master);
-    //print_r($cache->cacheSet(1));
   }
 
   /**
--- a/common-functions.inc	Thu Oct 11 22:30:07 2012 +0200
+++ b/common-functions.inc	Thu Oct 11 23:16:53 2012 +0200
@@ -8,7 +8,7 @@
 /// @cond
 $baseDir = dirname(__FILE__);
 
-$cache = CacheTimeCheck::instance(__FILE__);
+$cache = ScriptIncludeCache::instance(__FILE__);
 $cache->includeOnce('StatusCodes.inc', $baseDir);
 /// @endcond
 
--- a/index.php	Thu Oct 11 22:30:07 2012 +0200
+++ b/index.php	Thu Oct 11 23:16:53 2012 +0200
@@ -11,7 +11,7 @@
 
 $baseDir = dirname(__FILE__);
 
-$cache = CacheTimeCheck::instance(__FILE__);
+$cache = ScriptIncludeCache::instance(__FILE__);
 $cache->includeOnce('Language.inc', $baseDir);
 $cache->includeOnce('Options.inc', $baseDir);
 $cache->includeOnce('common-functions.inc', $baseDir);