diff options
Diffstat (limited to 'inc/cache.php')
-rw-r--r-- | inc/cache.php | 126 |
1 files changed, 96 insertions, 30 deletions
diff --git a/inc/cache.php b/inc/cache.php index 5eac94934..7a66049f4 100644 --- a/inc/cache.php +++ b/inc/cache.php @@ -8,16 +8,25 @@ if(!defined('DOKU_INC')) die('meh.'); +/** + * Generic handling of caching + */ class cache { - var $key = ''; // primary identifier for this item - var $ext = ''; // file ext for cache data, secondary identifier for this item - var $cache = ''; // cache file name - var $depends = array(); // array containing cache dependency information, - // used by _useCache to determine cache validity + public $key = ''; // primary identifier for this item + public $ext = ''; // file ext for cache data, secondary identifier for this item + public $cache = ''; // cache file name + public $depends = array(); // array containing cache dependency information, + // used by _useCache to determine cache validity var $_event = ''; // event to be triggered during useCache + var $_time; + var $_nocache = false; // if set to true, cache will not be used or stored - function cache($key,$ext) { + /** + * @param string $key primary identifier + * @param string $ext file extension + */ + public function cache($key,$ext) { $this->key = $key; $this->ext = $ext; $this->cache = getCacheName($key,$ext); @@ -26,7 +35,7 @@ class cache { /** * public method to determine whether the cache can be used * - * to assist in cetralisation of event triggering and calculation of cache statistics, + * to assist in centralisation of event triggering and calculation of cache statistics, * don't override this function override _useCache() * * @param array $depends array of cache dependencies, support dependecies: @@ -36,7 +45,7 @@ class cache { * * @return bool true if cache can be used, false otherwise */ - function useCache($depends=array()) { + public function useCache($depends=array()) { $this->depends = $depends; $this->_addDependencies(); @@ -55,12 +64,15 @@ class cache { * age - expire cache if older than age (seconds) * files - expire cache if any file in this array was updated more recently than the cache * + * Note that this function needs to be public as it is used as callback for the event handler + * * can be overridden * * @return bool see useCache() */ - function _useCache() { + public function _useCache() { + if ($this->_nocache) return false; // caching turned off if (!empty($this->depends['purge'])) return false; // purge requested? if (!($this->_time = @filemtime($this->cache))) return false; // cache exists? @@ -83,7 +95,7 @@ class cache { * it should not remove any existing dependencies and * it should only overwrite a dependency when the new value is more stringent than the old */ - function _addDependencies() { + protected function _addDependencies() { global $INPUT; if ($INPUT->has('purge')) $this->depends['purge'] = true; // purge requested } @@ -94,7 +106,7 @@ class cache { * @param bool $clean true to clean line endings, false to leave line endings alone * @return string cache contents */ - function retrieveCache($clean=true) { + public function retrieveCache($clean=true) { return io_readFile($this->cache, $clean); } @@ -104,14 +116,16 @@ class cache { * @param string $data the data to be cached * @return bool true on success, false otherwise */ - function storeCache($data) { + public function storeCache($data) { + if ($this->_nocache) return false; + return io_savefile($this->cache, $data); } /** * remove any cached data associated with this cache instance */ - function removeCache() { + public function removeCache() { @unlink($this->cache); } @@ -122,7 +136,7 @@ class cache { * @param bool $success result of this cache use attempt * @return bool pass-thru $success value */ - function _stats($success) { + protected function _stats($success) { global $conf; static $stats = null; static $file; @@ -157,14 +171,23 @@ class cache { } } +/** + * Parser caching + */ class cache_parser extends cache { - var $file = ''; // source file for cache - var $mode = ''; // input mode (represents the processing the input file will undergo) + public $file = ''; // source file for cache + public $mode = ''; // input mode (represents the processing the input file will undergo) var $_event = 'PARSER_CACHE_USE'; - function cache_parser($id, $file, $mode) { + /** + * + * @param string $id page id + * @param string $file source file for cache + * @param string $mode input mode + */ + public function cache_parser($id, $file, $mode) { if ($id) $this->page = $id; $this->file = $file; $this->mode = $mode; @@ -172,24 +195,25 @@ class cache_parser extends cache { parent::cache($file.$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT'],'.'.$mode); } - function _useCache() { + /** + * method contains cache use decision logic + * + * @return bool see useCache() + */ + public function _useCache() { if (!@file_exists($this->file)) return false; // source exists? return parent::_useCache(); } - function _addDependencies() { - global $conf, $config_cascade; - - $this->depends['age'] = isset($this->depends['age']) ? - min($this->depends['age'],$conf['cachetime']) : $conf['cachetime']; + protected function _addDependencies() { // parser cache file dependencies ... - $files = array($this->file, // ... source + $files = array($this->file, // ... source DOKU_INC.'inc/parser/parser.php', // ... parser DOKU_INC.'inc/parser/handler.php', // ... handler ); - $files = array_merge($files, getConfigFiles('main')); // ... wiki settings + $files = array_merge($files, getConfigFiles('main')); // ... wiki settings $this->depends['files'] = !empty($this->depends['files']) ? array_merge($files, $this->depends['files']) : $files; parent::_addDependencies(); @@ -197,8 +221,17 @@ class cache_parser extends cache { } +/** + * Caching of data of renderer + */ class cache_renderer extends cache_parser { - function _useCache() { + + /** + * method contains cache use decision logic + * + * @return bool see useCache() + */ + public function _useCache() { global $conf; if (!parent::_useCache()) return false; @@ -231,7 +264,19 @@ class cache_renderer extends cache_parser { return true; } - function _addDependencies() { + protected function _addDependencies() { + global $conf; + + // default renderer cache file 'age' is dependent on 'cachetime' setting, two special values: + // -1 : do not cache (should not be overridden) + // 0 : cache never expires (can be overridden) - no need to set depends['age'] + if ($conf['cachetime'] == -1) { + $this->_nocache = true; + return; + } elseif ($conf['cachetime'] > 0) { + $this->depends['age'] = isset($this->depends['age']) ? + min($this->depends['age'],$conf['cachetime']) : $conf['cachetime']; + } // renderer cache file dependencies ... $files = array( @@ -253,18 +298,39 @@ class cache_renderer extends cache_parser { } } +/** + * Caching of parser instructions + */ class cache_instructions extends cache_parser { - function cache_instructions($id, $file) { + /** + * @param string $id page id + * @param string $file source file for cache + */ + public function cache_instructions($id, $file) { parent::cache_parser($id, $file, 'i'); } - function retrieveCache($clean=true) { + /** + * retrieve the cached data + * + * @param bool $clean true to clean line endings, false to leave line endings alone + * @return string cache contents + */ + public function retrieveCache($clean=true) { $contents = io_readFile($this->cache, false); return !empty($contents) ? unserialize($contents) : array(); } - function storeCache($instructions) { + /** + * cache $instructions + * + * @param string $instructions the instruction to be cached + * @return bool true on success, false otherwise + */ + public function storeCache($instructions) { + if ($this->_nocache) return false; + return io_savefile($this->cache,serialize($instructions)); } } |