Mercurial > SimpleWebPresenter
comparison FileCacheSet.inc.php @ 134:b6b4a58c7625
Using .inc.php rather than just .inc for include files.
| author | Tom Fredrik Blenning <bfg@bfgconsult.no> |
|---|---|
| date | Sun, 22 Jan 2023 19:22:00 +0100 |
| parents | FileCacheSet.inc@adf7b11921f4 |
| children |
comparison
equal
deleted
inserted
replaced
| 133:00255ca89459 | 134:b6b4a58c7625 |
|---|---|
| 1 <?php | |
| 2 $baseDir = dirname(__FILE__); | |
| 3 | |
| 4 include_once 'Logger.inc.php'; | |
| 5 $cache = ScriptIncludeCache::instance(__FILE__); | |
| 6 $cache->cache_time("${baseDir}/Logger.inc.php"); | |
| 7 | |
| 8 class Cache { | |
| 9 private $max_age=0; | |
| 10 function setMaxAge($seconds) | |
| 11 { | |
| 12 $this->max_age=$seconds; | |
| 13 } | |
| 14 | |
| 15 function cacheControl() { | |
| 16 if ($this->max_age > 0) { | |
| 17 return array("expires" => toGMTime(time()+$this->max_age), "cache-control" => "public, max-age=$this->max_age"); | |
| 18 } | |
| 19 else { | |
| 20 return array("expires" => 0, "cache-control" => "public, max-age=0, no-cache"); | |
| 21 } | |
| 22 } | |
| 23 | |
| 24 }; | |
| 25 | |
| 26 /** | |
| 27 * Caches a set of file with timestamps | |
| 28 */ | |
| 29 class FileCacheSet extends Cache { | |
| 30 private $newest = 0; | |
| 31 private $files = array(); | |
| 32 private $parentCaches = array(); | |
| 33 | |
| 34 /** | |
| 35 * Constructs a FileCacheSet object | |
| 36 * | |
| 37 * @param $parent optional linked parent cache to sync with | |
| 38 */ | |
| 39 protected function __construct($parent = null) { | |
| 40 if ($parent) { | |
| 41 $this->addParent($parent); | |
| 42 } | |
| 43 } | |
| 44 | |
| 45 /** | |
| 46 * Links up a parent cache | |
| 47 * | |
| 48 * @param $parent cache we should sync with | |
| 49 */ | |
| 50 protected function addParent($parent) | |
| 51 { | |
| 52 array_push($this->parentCaches, $parent); | |
| 53 } | |
| 54 | |
| 55 /** | |
| 56 * List a set of files which contributes to this pages cacheset. | |
| 57 * | |
| 58 * @param $humanReadable If the timestamp should be human readable. | |
| 59 * | |
| 60 * @return an associative array of file, and timestamp | |
| 61 */ | |
| 62 function cacheSet($humanReadable = False) | |
| 63 { | |
| 64 $retVal = array(); | |
| 65 foreach ($this->parentCaches as $parent) { | |
| 66 $retVal = array_merge($retVal, $parent->cacheSet($humanReadable)); | |
| 67 } | |
| 68 foreach($this->files as $file) { | |
| 69 $mtime = filemtime($file); | |
| 70 if ($humanReadable) | |
| 71 $mtime = gmdate('D, d M Y H:i:s', $mtime) . ' GMT'; | |
| 72 | |
| 73 $retVal[$file] = $mtime; | |
| 74 } | |
| 75 return $retVal; | |
| 76 } | |
| 77 | |
| 78 /** | |
| 79 * Include a file in the cacheset | |
| 80 * | |
| 81 * @param $path the path of the file | |
| 82 */ | |
| 83 function cache_time($path) | |
| 84 { | |
| 85 if (!file_exists($path)) { | |
| 86 Logger::warn("${path} does not exist"); | |
| 87 errorPage("Resource is not available", 404); | |
| 88 } | |
| 89 | |
| 90 array_push($this->files, $path); | |
| 91 $mtime = filemtime($path); | |
| 92 if ($mtime > $this->newest) { | |
| 93 $this->newest = $mtime; | |
| 94 } | |
| 95 } | |
| 96 | |
| 97 /** | |
| 98 * Find the newest member of the combined cacheset | |
| 99 * | |
| 100 * @return timestamp of newest member | |
| 101 */ | |
| 102 function getNewest() | |
| 103 { | |
| 104 $newest = 0; | |
| 105 foreach ($this->parentCaches as $parent) { | |
| 106 $newest = max($newest, $parent->getNewest()); | |
| 107 } | |
| 108 $newest = max($newest, $this->newest); | |
| 109 return $newest; | |
| 110 } | |
| 111 } | |
| 112 | |
| 113 /** | |
| 114 * Singleton class, keeps track of all scriptfile includes | |
| 115 * | |
| 116 * This class is typically used as a parent class of another cache | |
| 117 */ | |
| 118 class ScriptIncludeCache extends FileCacheSet | |
| 119 { | |
| 120 private static $myInstance = 0; | |
| 121 | |
| 122 protected function __construct($filename = False) | |
| 123 { | |
| 124 parent::__construct(); | |
| 125 if ($filename) | |
| 126 $this->cache_time($filename); | |
| 127 $this->cache_time(__FILE__); | |
| 128 } | |
| 129 | |
| 130 /** | |
| 131 * Generates a singleton instance of this CacheTimeCheck | |
| 132 * | |
| 133 * @param $filename an optional file to include in the cacheset | |
| 134 * | |
| 135 * @return a CacheTimeCheck object | |
| 136 */ | |
| 137 public static function instance($filename = False) | |
| 138 { | |
| 139 if (! self::$myInstance) | |
| 140 self::$myInstance = new self($filename); | |
| 141 elseif ($filename) | |
| 142 self::$myInstance->cache_time($filename); | |
| 143 return self::$myInstance; | |
| 144 } | |
| 145 | |
| 146 /** | |
| 147 * Convenience function to include a file, and add it to the cacheset. | |
| 148 * | |
| 149 * @param $path path of the file | |
| 150 * @param $basedir a directory to prepend to the path | |
| 151 */ | |
| 152 function includeOnce($path, $basedir = false) | |
| 153 { | |
| 154 if ($basedir) | |
| 155 $path = $basedir . "/" . $path; | |
| 156 $this->cache_time($path); | |
| 157 include_once($path); | |
| 158 } | |
| 159 } | |
| 160 ?> |
