Skip to content

Commit

Permalink
Fix FileEngine not clearing keys when groups are used.
Browse files Browse the repository at this point in the history
Fixes #3930
  • Loading branch information
markstory committed Jul 28, 2013
1 parent 17b2538 commit 8a81903
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
22 changes: 19 additions & 3 deletions lib/Cake/Cache/Engine/FileEngine.php
Expand Up @@ -223,11 +223,28 @@ public function clear($check) {
if (!$this->_init) {
return false;
}
$dir = dir($this->settings['path']);
$threshold = $now = false;
if ($check) {
$now = time();
$threshold = $now - $this->settings['duration'];
}
$this->_clearDirectory($this->settings['path'], $now, $threshold);
foreach ($this->settings['groups'] as $group) {
$this->_clearDirectory($this->settings['path'] . $group . DS, $now, $threshold);
}
return true;
}

/**
* Used to clear a directory of matching files.
*
* @param string $path The path to search.
* @param integer $now The current timestamp
* @param integer $threshold Any file not modified after this value will be deleted.
* @return void
*/
protected function _clearDirectory($path, $now, $threshold) {
$dir = dir($path);
$prefixLength = strlen($this->settings['prefix']);
while (($entry = $dir->read()) !== false) {
if (substr($entry, 0, $prefixLength) !== $this->settings['prefix']) {
Expand All @@ -236,7 +253,7 @@ public function clear($check) {
if ($this->_setKey($entry) === false) {
continue;
}
if ($check) {
if ($threshold) {
$mtime = $this->_File->getMTime();

if ($mtime > $threshold) {
Expand All @@ -256,7 +273,6 @@ public function clear($check) {
}
}
$dir->close();
return true;
}

/**
Expand Down
5 changes: 3 additions & 2 deletions lib/Cake/Test/Case/Cache/Engine/FileEngineTest.php
Expand Up @@ -267,9 +267,10 @@ public function testClearWithGroups() {
'duration' => DAY,
'groups' => array('short')
));
$engine->write('test_key', 'it works', DAY);
$key = 'cake_test_test_key';
$engine->write($key, 'it works', DAY);
$engine->clear(false);
$this->assertFalse($engine->read('test_key'), 'Key should have been removed');
$this->assertFalse($engine->read($key), 'Key should have been removed');
}

/**
Expand Down

0 comments on commit 8a81903

Please sign in to comment.