Fix clearing files in multiple groups.

When using multiple groups on a cache config, clearing was not working
properly. Clearing un-used cache configs also failed when the
directories did not exist.

Fixes #3930
This commit is contained in:
mark_story 2013-08-02 17:56:11 -04:00
parent 19ac39963a
commit b3860b3992
2 changed files with 41 additions and 16 deletions

View file

@ -228,9 +228,16 @@ class FileEngine extends CacheEngine {
$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);
$directory = new RecursiveDirectoryIterator($this->settings['path']);
$contents = new RecursiveIteratorIterator($directory, RecursiveIteratorIterator::SELF_FIRST);
foreach ($contents as $path) {
if ($path->isFile()) {
continue;
}
$this->_clearDirectory($path->getRealPath() . DS, $now, $threshold);
}
return true;
}
@ -244,35 +251,36 @@ class FileEngine extends CacheEngine {
* @return void
*/
protected function _clearDirectory($path, $now, $threshold) {
$dir = dir($path);
$prefixLength = strlen($this->settings['prefix']);
if (!is_dir($path)) {
return;
}
$dir = dir($path);
while (($entry = $dir->read()) !== false) {
if (substr($entry, 0, $prefixLength) !== $this->settings['prefix']) {
continue;
}
if ($this->_setKey($entry) === false) {
continue;
}
$filePath = $path . $entry;
$file = new SplFileObject($path . $entry, 'r');
if ($threshold) {
$mtime = $this->_File->getMTime();
$mtime = $file->getMTime();
if ($mtime > $threshold) {
continue;
}
$expires = (int)$this->_File->current();
$expires = (int)$file->current();
if ($expires > $now) {
continue;
}
}
$path = $this->_File->getRealPath();
$this->_File = null;
if (file_exists($path)) {
unlink($path);
if ($file->isFile()) {
unlink($file);
}
}
$dir->close();
}
/**

View file

@ -52,7 +52,7 @@ class FileEngineTest extends CakeTestCase {
*/
public function tearDown() {
parent::tearDown();
Cache::clear(false, 'file_test');
// Cache::clear(false, 'file_test');
Cache::drop('file_test');
Cache::drop('file_groups');
Cache::drop('file_groups2');
@ -265,7 +265,7 @@ class FileEngineTest extends CakeTestCase {
$engine->init(array(
'prefix' => 'cake_test_',
'duration' => DAY,
'groups' => array('short')
'groups' => array('short', 'round')
));
$key = 'cake_test_test_key';
$engine->write($key, 'it works', DAY);
@ -273,6 +273,23 @@ class FileEngineTest extends CakeTestCase {
$this->assertFalse($engine->read($key), 'Key should have been removed');
}
/**
* Test that clear() also removes files with group tags.
*
* @return void
*/
public function testClearWithNoKeys() {
$engine = new FileEngine();
$engine->init(array(
'prefix' => 'cake_test_',
'duration' => DAY,
'groups' => array('one', 'two')
));
$key = 'cake_test_test_key';
$engine->clear(false);
$this->assertFalse($engine->read($key), 'No errors should be found');
}
/**
* testKeyPath method
*