Mercurial > SimpleWebPresenter
comparison CacheTimeCheck.inc @ 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 | 7b87ec8b0842 |
| children | dd4ddedca4c5 |
comparison
equal
deleted
inserted
replaced
| 67:37dee99c1f8c | 68:4dfa3f6a2dc1 |
|---|---|
| 1 <?php | 1 <?php |
| 2 $baseDir = dirname(__FILE__); | 2 $baseDir = dirname(__FILE__); |
| 3 | 3 |
| 4 include_once 'Logger.inc'; | 4 include_once 'Logger.inc'; |
| 5 include_once 'common-functions.inc'; | 5 include_once 'common-functions.inc'; |
| 6 $cache = CacheTimeCheck::instance(__FILE__); | 6 $cache = ScriptIncludeCache::instance(__FILE__); |
| 7 $cache->cache_time("${baseDir}/Logger.inc"); | 7 $cache->cache_time("${baseDir}/Logger.inc"); |
| 8 $cache->cache_time("${baseDir}/common-functions.inc"); | 8 $cache->cache_time("${baseDir}/common-functions.inc"); |
| 9 | 9 |
| 10 /** | 10 class FileCacheSet { |
| 11 * CacheTimeCheck provides a set of functions to enable generating a | 11 private $newest = 0; |
| 12 * correct time for the latest update for a given file. | |
| 13 * | |
| 14 * @author Tom Fredrik Blenning Klaussen | |
| 15 */ | |
| 16 class CacheTimeCheck | |
| 17 { | |
| 18 private $newest; | |
| 19 private $files = array(); | 12 private $files = array(); |
| 20 private static $myInstance = 0; | 13 private $parentCaches = array(); |
| 21 | 14 |
| 22 private function __construct($filename = False) | 15 protected function __construct($parent = null) { |
| 16 if ($parent) { | |
| 17 $this->addParent($parent); | |
| 18 } | |
| 19 } | |
| 20 | |
| 21 protected function addParent($parent) | |
| 23 { | 22 { |
| 24 if ($filename) | 23 array_push($this->parentCaches, $parent); |
| 25 $this->cache_time($filename); | |
| 26 $this->cache_time(__FILE__); | |
| 27 } | 24 } |
| 28 | 25 |
| 29 /** | 26 /** |
| 30 * List a set of files which contributes to this pages cacheset. | 27 * List a set of files which contributes to this pages cacheset. |
| 31 * | 28 * |
| 34 * @return an associative array of file, and timestamp | 31 * @return an associative array of file, and timestamp |
| 35 */ | 32 */ |
| 36 function cacheSet($humanReadable = False) | 33 function cacheSet($humanReadable = False) |
| 37 { | 34 { |
| 38 $retVal = array(); | 35 $retVal = array(); |
| 36 foreach ($this->parentCaches as $parent) { | |
| 37 $retVal = array_merge($retVal, $parent->cacheSet($humanReadable)); | |
| 38 } | |
| 39 foreach($this->files as $file) { | 39 foreach($this->files as $file) { |
| 40 $mtime = filemtime($file); | 40 $mtime = filemtime($file); |
| 41 if ($humanReadable) | 41 if ($humanReadable) |
| 42 $mtime = gmdate('D, d M Y H:i:s', $mtime) . ' GMT'; | 42 $mtime = gmdate('D, d M Y H:i:s', $mtime) . ' GMT'; |
| 43 | 43 |
| 44 $retVal[$file] = $mtime; | 44 $retVal[$file] = $mtime; |
| 45 } | 45 } |
| 46 return $retVal; | 46 return $retVal; |
| 47 } | |
| 48 | |
| 49 /** | |
| 50 * Generates a singleton instance of this CacheTimeCheck | |
| 51 * | |
| 52 * @param $filename an optional file to include in the cacheset | |
| 53 * | |
| 54 * @return a CacheTimeCheck object | |
| 55 */ | |
| 56 function instance($filename = False) | |
| 57 { | |
| 58 if (! self::$myInstance) | |
| 59 self::$myInstance = new self($filename); | |
| 60 elseif ($filename) | |
| 61 self::$myInstance->cache_time($filename); | |
| 62 return self::$myInstance; | |
| 63 } | |
| 64 | |
| 65 /** | |
| 66 * Checks if a HTTP_IF_MODIFIED_SINCE header is set, if this file is | |
| 67 * newer, set a Last-Modified header, otherwise abort with an 304 | |
| 68 * status code | |
| 69 */ | |
| 70 function CheckHttpModified() | |
| 71 { | |
| 72 if (DEBUG_LEVEL >= VERBOSITY_DEBUG) | |
| 73 var_dump($_SERVER); | |
| 74 | |
| 75 $gmdate_mod = gmdate('D, d M Y H:i:s', $this->newest) . ' GMT'; | |
| 76 | |
| 77 if(array_key_exists('HTTP_IF_MODIFIED_SINCE', $_SERVER)) { | |
| 78 $HTTP_IF_MODIFIED_SINCE = $_SERVER['HTTP_IF_MODIFIED_SINCE']; | |
| 79 $if_modified_since = preg_replace('/;.*$/', '', $HTTP_IF_MODIFIED_SINCE); | |
| 80 | |
| 81 if (strtotime($if_modified_since) >= $this->newest) { | |
| 82 header("HTTP/1.0 304 Not Modified"); | |
| 83 exit; | |
| 84 } | |
| 85 } | |
| 86 | |
| 87 header("Last-Modified: $gmdate_mod"); | |
| 88 } | 47 } |
| 89 | 48 |
| 90 /** | 49 /** |
| 91 * Include a file in the cacheset | 50 * Include a file in the cacheset |
| 92 * | 51 * |
| 104 if ($mtime > $this->newest) { | 63 if ($mtime > $this->newest) { |
| 105 $this->newest = $mtime; | 64 $this->newest = $mtime; |
| 106 } | 65 } |
| 107 } | 66 } |
| 108 | 67 |
| 68 function getNewest() | |
| 69 { | |
| 70 $newest = 0; | |
| 71 foreach ($this->parentCaches as $parent) { | |
| 72 $newest = max($newest, $parent->getNewest()); | |
| 73 } | |
| 74 $newest = max($newest, $this->newest); | |
| 75 return $newest; | |
| 76 } | |
| 77 } | |
| 78 | |
| 79 class ScriptIncludeCache extends FileCacheSet | |
| 80 { | |
| 81 private static $myInstance = 0; | |
| 82 | |
| 83 protected function __construct($filename = False) | |
| 84 { | |
| 85 parent::__construct(); | |
| 86 if ($filename) | |
| 87 $this->cache_time($filename); | |
| 88 $this->cache_time(__FILE__); | |
| 89 } | |
| 90 | |
| 91 /** | |
| 92 * Generates a singleton instance of this CacheTimeCheck | |
| 93 * | |
| 94 * @param $filename an optional file to include in the cacheset | |
| 95 * | |
| 96 * @return a CacheTimeCheck object | |
| 97 */ | |
| 98 function instance($filename = False) | |
| 99 { | |
| 100 if (! self::$myInstance) | |
| 101 self::$myInstance = new self($filename); | |
| 102 elseif ($filename) | |
| 103 self::$myInstance->cache_time($filename); | |
| 104 return self::$myInstance; | |
| 105 } | |
| 106 | |
| 109 /** | 107 /** |
| 110 * Convenience function to include a file, and add it to the cacheset. | 108 * Convenience function to include a file, and add it to the cacheset. |
| 111 * | 109 * |
| 112 * @param $path path of the file | 110 * @param $path path of the file |
| 113 * @param $basedir a directory to prepend to the path | 111 * @param $basedir a directory to prepend to the path |
| 116 { | 114 { |
| 117 if ($basedir) | 115 if ($basedir) |
| 118 $path = $basedir . "/" . $path; | 116 $path = $basedir . "/" . $path; |
| 119 $this->cache_time($path); | 117 $this->cache_time($path); |
| 120 include_once($path); | 118 include_once($path); |
| 119 } | |
| 120 } | |
| 121 | |
| 122 /** | |
| 123 * CacheTimeCheck provides a set of functions to enable generating a | |
| 124 * correct time for the latest update for a given file. | |
| 125 * | |
| 126 * @author Tom Fredrik Blenning Klaussen | |
| 127 */ | |
| 128 class CacheTimeCheck extends FileCacheSet | |
| 129 { | |
| 130 function __construct($filename = False) | |
| 131 { | |
| 132 parent::__construct(); | |
| 133 if ($filename) | |
| 134 $this->cache_time($filename); | |
| 135 $this->cache_time(__FILE__); | |
| 136 } | |
| 137 | |
| 138 public function addParent($cache) | |
| 139 { | |
| 140 parent::addParent($cache); | |
| 141 } | |
| 142 | |
| 143 /** | |
| 144 * Checks if a HTTP_IF_MODIFIED_SINCE header is set, if this file is | |
| 145 * newer, set a Last-Modified header, otherwise abort with an 304 | |
| 146 * status code | |
| 147 */ | |
| 148 function CheckHttpModified() | |
| 149 { | |
| 150 if (DEBUG_LEVEL >= VERBOSITY_DEBUG) | |
| 151 var_dump($_SERVER); | |
| 152 | |
| 153 $gmdate_mod = gmdate('D, d M Y H:i:s', $this->getNewest()) . ' GMT'; | |
| 154 | |
| 155 if(array_key_exists('HTTP_IF_MODIFIED_SINCE', $_SERVER)) { | |
| 156 $HTTP_IF_MODIFIED_SINCE = $_SERVER['HTTP_IF_MODIFIED_SINCE']; | |
| 157 $if_modified_since = preg_replace('/;.*$/', '', $HTTP_IF_MODIFIED_SINCE); | |
| 158 | |
| 159 if (strtotime($if_modified_since) >= $this->getNewest()) { | |
| 160 header("HTTP/1.0 304 Not Modified"); | |
| 161 exit; | |
| 162 } | |
| 163 } | |
| 164 | |
| 165 header("Last-Modified: $gmdate_mod"); | |
| 121 } | 166 } |
| 122 | 167 |
| 123 /** | 168 /** |
| 124 * Convenience function to load a file, and add it to the cacheset | 169 * Convenience function to load a file, and add it to the cacheset |
| 125 * | 170 * |
