From d7fd4c3e04fcbbf463c35763e008527d3c9ad59f Mon Sep 17 00:00:00 2001 From: Gerrit Uitslag Date: Mon, 17 Feb 2014 19:12:41 +0100 Subject: Add dynamic declared _time attribute to cache object --- inc/cache.php | 1 + 1 file changed, 1 insertion(+) (limited to 'inc/cache.php') diff --git a/inc/cache.php b/inc/cache.php index 5eac94934..8453fe3e9 100644 --- a/inc/cache.php +++ b/inc/cache.php @@ -16,6 +16,7 @@ class cache { // used by _useCache to determine cache validity var $_event = ''; // event to be triggered during useCache + var $_time; function cache($key,$ext) { $this->key = $key; -- cgit v1.2.3 From c59b3e001d1e8258b1d118909257b70516c8a6b1 Mon Sep 17 00:00:00 2001 From: Gerrit Uitslag Date: Mon, 17 Feb 2014 23:16:59 +0100 Subject: add visibility keywords and PHPDocs for cache --- inc/cache.php | 87 ++++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 62 insertions(+), 25 deletions(-) (limited to 'inc/cache.php') diff --git a/inc/cache.php b/inc/cache.php index 8453fe3e9..8c23bd09b 100644 --- a/inc/cache.php +++ b/inc/cache.php @@ -9,16 +9,20 @@ if(!defined('DOKU_INC')) die('meh.'); 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, + 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; - 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); @@ -37,7 +41,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(); @@ -60,7 +64,7 @@ class cache { * * @return bool see useCache() */ - function _useCache() { + protected function _useCache() { if (!empty($this->depends['purge'])) return false; // purge requested? if (!($this->_time = @filemtime($this->cache))) return false; // cache exists? @@ -84,7 +88,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 } @@ -95,7 +99,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); } @@ -105,14 +109,14 @@ class cache { * @param string $data the data to be cached * @return bool true on success, false otherwise */ - function storeCache($data) { + public function storeCache($data) { return io_savefile($this->cache, $data); } /** * remove any cached data associated with this cache instance */ - function removeCache() { + public function removeCache() { @unlink($this->cache); } @@ -123,7 +127,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; @@ -160,12 +164,18 @@ class cache { 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; @@ -173,24 +183,29 @@ 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() + */ + protected function _useCache() { if (!@file_exists($this->file)) return false; // source exists? return parent::_useCache(); } - function _addDependencies() { - global $conf, $config_cascade; + protected function _addDependencies() { + global $conf; $this->depends['age'] = isset($this->depends['age']) ? min($this->depends['age'],$conf['cachetime']) : $conf['cachetime']; // 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(); @@ -199,7 +214,13 @@ class cache_parser extends cache { } class cache_renderer extends cache_parser { - function _useCache() { + + /** + * method contains cache use decision logic + * + * @return bool see useCache() + */ + protected function _useCache() { global $conf; if (!parent::_useCache()) return false; @@ -232,7 +253,7 @@ class cache_renderer extends cache_parser { return true; } - function _addDependencies() { + protected function _addDependencies() { // renderer cache file dependencies ... $files = array( @@ -256,16 +277,32 @@ class cache_renderer extends cache_parser { 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) { return io_savefile($this->cache,serialize($instructions)); } } -- cgit v1.2.3 From cefd14cbc4f6dabfb2fb7b7ffb6b68d4501afd4f Mon Sep 17 00:00:00 2001 From: Gerrit Uitslag Date: Tue, 18 Feb 2014 00:54:21 +0100 Subject: PHPDocs of cache classes --- inc/cache.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'inc/cache.php') diff --git a/inc/cache.php b/inc/cache.php index 8c23bd09b..5f54a34a9 100644 --- a/inc/cache.php +++ b/inc/cache.php @@ -8,6 +8,9 @@ if(!defined('DOKU_INC')) die('meh.'); +/** + * Generic handling of caching + */ class cache { public $key = ''; // primary identifier for this item public $ext = ''; // file ext for cache data, secondary identifier for this item @@ -162,6 +165,9 @@ class cache { } } +/** + * Parser caching + */ class cache_parser extends cache { public $file = ''; // source file for cache @@ -213,6 +219,9 @@ class cache_parser extends cache { } +/** + * Caching of data of renderer + */ class cache_renderer extends cache_parser { /** @@ -275,6 +284,9 @@ class cache_renderer extends cache_parser { } } +/** + * Caching of parser instructions + */ class cache_instructions extends cache_parser { /** -- cgit v1.2.3 From a8795974051a91137b01ff88dbf5586a647b24ce Mon Sep 17 00:00:00 2001 From: Michael Hamann Date: Fri, 14 Mar 2014 00:24:21 +0100 Subject: Fix caching (make the event callback public again) Caching had been completely broken (disabled) for caches with events because the default event handler (cache::_useCache()) was protected and thus couldn't be executed by the event handler. This was broken in c59b3e001d1e8258b1d118909257b70516c8a6b1 --- inc/cache.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'inc/cache.php') diff --git a/inc/cache.php b/inc/cache.php index 5f54a34a9..56c5b65f2 100644 --- a/inc/cache.php +++ b/inc/cache.php @@ -63,11 +63,13 @@ 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() */ - protected function _useCache() { + public function _useCache() { if (!empty($this->depends['purge'])) return false; // purge requested? if (!($this->_time = @filemtime($this->cache))) return false; // cache exists? @@ -194,7 +196,7 @@ class cache_parser extends cache { * * @return bool see useCache() */ - protected function _useCache() { + public function _useCache() { if (!@file_exists($this->file)) return false; // source exists? return parent::_useCache(); @@ -229,7 +231,7 @@ class cache_renderer extends cache_parser { * * @return bool see useCache() */ - protected function _useCache() { + public function _useCache() { global $conf; if (!parent::_useCache()) return false; -- cgit v1.2.3 From b23560022e3a99310a04296b3c385325e7abb747 Mon Sep 17 00:00:00 2001 From: Christopher Smith Date: Sun, 16 Mar 2014 18:32:07 +0000 Subject: Fix longstanding issue with cache class & cachetime setting 1. cachetime setting should only be applied to the cache_renderer class. Previously it was applied to cache_parser (and by extension cache_handler). 2. two special cachetime values, -1 & 0, weren't handled, per FS#2183 To handle the cachetime setting, -1, disable caching, a new property _nocache is added. When that property is true, any cache file must not be used and storecache() should not store any values --- inc/cache.php | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) (limited to 'inc/cache.php') diff --git a/inc/cache.php b/inc/cache.php index 56c5b65f2..7a66049f4 100644 --- a/inc/cache.php +++ b/inc/cache.php @@ -16,10 +16,11 @@ class cache { 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 + // 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 /** * @param string $key primary identifier @@ -34,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: @@ -71,6 +72,7 @@ class cache { */ 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? @@ -115,6 +117,8 @@ class cache { * @return bool true on success, false otherwise */ public function storeCache($data) { + if ($this->_nocache) return false; + return io_savefile($this->cache, $data); } @@ -203,10 +207,6 @@ class cache_parser extends cache { } protected function _addDependencies() { - global $conf; - - $this->depends['age'] = isset($this->depends['age']) ? - min($this->depends['age'],$conf['cachetime']) : $conf['cachetime']; // parser cache file dependencies ... $files = array($this->file, // ... source @@ -265,6 +265,18 @@ class cache_renderer extends cache_parser { } 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( @@ -317,6 +329,8 @@ class cache_instructions extends cache_parser { * @return bool true on success, false otherwise */ public function storeCache($instructions) { + if ($this->_nocache) return false; + return io_savefile($this->cache,serialize($instructions)); } } -- cgit v1.2.3