changeset 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 f2d52fed708c
children 0a33803ee026
files CacheTimeCheck.inc FileCacheSet.inc Flag.inc InputParser.inc Options.inc Page.inc Sitemap.inc
diffstat 7 files changed, 113 insertions(+), 39 deletions(-) [+]
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);
   }
+
 }
 ?>
--- a/FileCacheSet.inc	Fri Oct 19 01:36:22 2012 +0200
+++ b/FileCacheSet.inc	Sun Sep 14 21:11:27 2014 +0200
@@ -5,10 +5,23 @@
 $cache = ScriptIncludeCache::instance(__FILE__);
 $cache->cache_time("${baseDir}/Logger.inc");
 
+class Cache {
+  private $max_age=0;
+  function setMaxAge($seconds)
+  {
+    $this->max_age=$seconds;
+  }
+
+  function cacheControl() {
+    return array("expires" => toGMTime(time()+$this->max_age), "cache-control" => "public, max-age=$this->max_age");
+  }
+
+};
+
 /**
  * Caches a set of file with timestamps
  */
-class FileCacheSet {
+class FileCacheSet extends Cache {
   private $newest = 0;
   private $files = array();
   private $parentCaches = array();
--- a/Flag.inc	Fri Oct 19 01:36:22 2012 +0200
+++ b/Flag.inc	Sun Sep 14 21:11:27 2014 +0200
@@ -51,14 +51,14 @@
     $this->name .= ".png";
 
     $cache = new CacheTimeCheck($this->name);
+    $cache->setMaxAge(86400);
     $cache->addParent($masterCache);
     $this->setCache($cache);
   }
 
   function cacheCheck()
   {
-    $this->getCache()->cache_time($this->name);
-    return $true;
+    return Cacheable::YES;
   }
 
   function mayCompress()
--- a/InputParser.inc	Fri Oct 19 01:36:22 2012 +0200
+++ b/InputParser.inc	Sun Sep 14 21:11:27 2014 +0200
@@ -61,7 +61,7 @@
       }
     }
     $this->master = self::getFiles($this->master, $this->options);
-
+    $cache->setMaxAge(20);
   }
 
   function cacheCheck()
--- a/Options.inc	Fri Oct 19 01:36:22 2012 +0200
+++ b/Options.inc	Sun Sep 14 21:11:27 2014 +0200
@@ -1,5 +1,13 @@
 <?php
 /**
+ */
+abstract class Cacheable {
+  const NO = 0;
+  const YES = 1;
+  const UNDETERMINED = -1;
+}
+
+/**
  * Contains alle configurable parameters, and "globals"
  *
  * @author Tom Fredrik Blenning Klaussen
@@ -16,7 +24,7 @@
   private $basePath;
   private $flagUrl;
   private $baseUrl;
-  private $cacheable = True;
+  private $cacheable = Cacheable::YES;
 
   /**
    * Gets the default language
@@ -41,7 +49,7 @@
   /**
    *  Sets wether or not this page may be cached
    *
-   * @param $cacheable bool
+   * @param $cacheable Cacheable
    */
   function setCacheable($cacheable)
   {
@@ -51,7 +59,7 @@
   /**
    * Gets wether or not this page may be cached
    *
-   * @return bool, default is True
+   * @return Cacheable, default is YES
    */
   function getCacheable()
   {
--- a/Page.inc	Fri Oct 19 01:36:22 2012 +0200
+++ b/Page.inc	Sun Sep 14 21:11:27 2014 +0200
@@ -152,14 +152,24 @@
   /**
    * Generates an appropriate response to the request.
    *
-   * Eg. 302 NOT CHANGED, error message or the actual content
+   * Eg. 304 NOT CHANGED, error message or the actual content
    */
   function genPage()
   {
-    if ($this->cacheCheck()) {
-      $this->cache->CheckHttpModified();
+    $cacheable = $this->cacheCheck();
+    if ($cacheable == Cacheable::YES) {
+      if (!CheckHttpModified($this->cache))
+	return false;
     }
     $res = $this->generateContent();
+    if ($cacheable == Cacheable::UNDETERMINED) {
+      $cacheable = $this->cacheCheck();
+      if ($cacheable == Cacheable::YES) {
+	if (!CheckHttpModified($this->cache))
+	  return false;
+      }
+    }
+
     if ($this->mayValidate()) {
       /*
       $request = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
@@ -190,10 +200,12 @@
   function display()
   {
     $res = $this->genPage();
-    foreach ($res->headers as $header => $value) {
-      header("${header}: ${value}");
+    if ($res) {
+      foreach ($res->headers as $header => $value) {
+	header("${header}: ${value}");
+      }
+      print $res;
     }
-    print $res;
   }
 
 
--- a/Sitemap.inc	Fri Oct 19 01:36:22 2012 +0200
+++ b/Sitemap.inc	Sun Sep 14 21:11:27 2014 +0200
@@ -7,8 +7,20 @@
 $cache->includeOnce('Http.inc', $baseDir);
 $cache->includeOnce('Page.inc', $baseDir);
 $cache->includeOnce('common-functions.inc', $baseDir);
+$cache->includeOnce('CacheTimeCheck.inc', $baseDir);
 /// @endcond
 
+class SimpleCache extends Cache {
+  private $time;
+
+  function __construct($time = 0) {
+    $this->time = $time;
+  }
+  function getNewest() {
+    return $this->time;
+  }
+};
+
 function cmp_length_lex($a, $b)
 {
   if ($a == $b) {
@@ -41,11 +53,15 @@
     $this->master->load($path);
 
     $this->options = new Options($this->master);
+    $this->lastmod=0;
   }
 
   function cacheCheck()
   {
-    return false;
+    if ($this->lastmod == 0)
+      return Cacheable::UNDETERMINED;
+    else
+      return Cacheable::YES;
   }
 
   function mayValidate()
@@ -125,6 +141,9 @@
 
       if ($n == StatusCodes::HTTP_OK) {
 	$lastmod = strtotime($lastmod);
+	if ($lastmod > $this->lastmod) {
+	  $this->lastmod = $lastmod;
+	}
 	$lastmod = date(DateTime::W3C, $lastmod);
 
 	$out .= "<url>\n";
@@ -137,6 +156,9 @@
     $out .= '</urlset>';
 
     $res = new PageContent($out);
+    $cache=new SimpleCache($this->lastmod);
+    $cache->setMaxAge(86400);
+    $this->setCache($cache);
     $res->setHeader('Content-type', 'application/xml');
     return $res;
   }