Skip to content
Snippets Groups Projects
Commit b2356002 authored by Christopher Smith's avatar Christopher Smith
Browse files

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
parent 1359eacb
No related branches found
No related tags found
No related merge requests found
......@@ -6,22 +6,22 @@
* Tests if caching can actually be used
*/
class cache_use_test extends DokuWikiTest {
/** @var cache_renderer $cache */
/** @var cache_parser $cache */
private $cache;
function setUp() {
global $ID;
global $ID, $conf;
parent::setUp();
$ID = 'cached';
$file = wikiFN($ID);
$conf['cachetime'] = 0; // ensure the value is not -1, which disables caching
saveWikiText($ID, 'Content', 'Created');
// set the modification time a second in the past in order to ensure that the cache is newer than the page
touch($file, time()-1);
# Create cache. Note that the metadata cache is used as the xhtml cache triggers metadata rendering
$this->cache = new cache_renderer($ID, $file, 'metadata');
$this->cache = new cache_renderer($ID, $file, 'xhtml');
$this->cache->storeCache('Test');
}
......@@ -29,8 +29,52 @@ class cache_use_test extends DokuWikiTest {
$this->assertTrue($this->cache->useCache());
}
/**
* In all the following tests the cache should not be usable
* as such, they are meaningless if test_use didn't pass.
*
* @depends test_use
*/
function test_purge() {
$this->assertFalse($this->cache->useCache(array('purge' => true)));
/* @var Input $INPUT */
global $INPUT;
$INPUT->set('purge',1);
$this->assertFalse($this->cache->useCache());
$this->assertNotEmpty($this->cache->depends['purge']);
}
/**
* @depends test_use
*/
function test_filedependency() {
// give the dependent src file the same mtime as the cache
touch($this->cache->file, filemtime($this->cache->cache));
$this->assertFalse($this->cache->useCache());
}
/**
* @depends test_use
*/
function test_age() {
// need to age both our source file & the cache
$age = 10;
$time = time() - $age - 1; // older than age
touch($this->cache->file, $time - 1);
touch($this->cache->cache, $time);
$this->assertFalse($this->cache->useCache(array('age' => $age)));
}
/**
* @depends test_use
*/
function test_confnocaching() {
global $conf;
$conf['cachetime'] = -1; // disables renderer caching
$this->assertFalse($this->cache->useCache());
$this->assertNotEmpty($this->cache->_nocache);
}
}
\ No newline at end of file
......@@ -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));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment