Mercurial > SimpleWebPresenter
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/FileCacheSet.inc.php Sun Jan 22 19:22:00 2023 +0100 @@ -0,0 +1,160 @@ +<?php +$baseDir = dirname(__FILE__); + +include_once 'Logger.inc.php'; +$cache = ScriptIncludeCache::instance(__FILE__); +$cache->cache_time("${baseDir}/Logger.inc.php"); + +class Cache { + private $max_age=0; + function setMaxAge($seconds) + { + $this->max_age=$seconds; + } + + function cacheControl() { + if ($this->max_age > 0) { + return array("expires" => toGMTime(time()+$this->max_age), "cache-control" => "public, max-age=$this->max_age"); + } + else { + return array("expires" => 0, "cache-control" => "public, max-age=0, no-cache"); + } + } + +}; + +/** + * Caches a set of file with timestamps + */ +class FileCacheSet extends Cache { + private $newest = 0; + private $files = array(); + private $parentCaches = array(); + + /** + * Constructs a FileCacheSet object + * + * @param $parent optional linked parent cache to sync with + */ + protected function __construct($parent = null) { + if ($parent) { + $this->addParent($parent); + } + } + + /** + * Links up a parent cache + * + * @param $parent cache we should sync with + */ + protected function addParent($parent) + { + array_push($this->parentCaches, $parent); + } + + /** + * List a set of files which contributes to this pages cacheset. + * + * @param $humanReadable If the timestamp should be human readable. + * + * @return an associative array of file, and timestamp + */ + 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) + $mtime = gmdate('D, d M Y H:i:s', $mtime) . ' GMT'; + + $retVal[$file] = $mtime; + } + return $retVal; + } + + /** + * Include a file in the cacheset + * + * @param $path the path of the file + */ + function cache_time($path) + { + if (!file_exists($path)) { + Logger::warn("${path} does not exist"); + errorPage("Resource is not available", 404); + } + + array_push($this->files, $path); + $mtime = filemtime($path); + if ($mtime > $this->newest) { + $this->newest = $mtime; + } + } + + /** + * Find the newest member of the combined cacheset + * + * @return timestamp of newest member + */ + function getNewest() + { + $newest = 0; + foreach ($this->parentCaches as $parent) { + $newest = max($newest, $parent->getNewest()); + } + $newest = max($newest, $this->newest); + return $newest; + } +} + +/** + * Singleton class, keeps track of all scriptfile includes + * + * This class is typically used as a parent class of another cache + */ +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 + */ + public static 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. + * + * @param $path path of the file + * @param $basedir a directory to prepend to the path + */ + function includeOnce($path, $basedir = false) + { + if ($basedir) + $path = $basedir . "/" . $path; + $this->cache_time($path); + include_once($path); + } +} +?> \ No newline at end of file
