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

View file

@ -52,7 +52,7 @@ class FileEngineTest extends CakeTestCase {
*/ */
public function tearDown() { public function tearDown() {
parent::tearDown(); parent::tearDown();
Cache::clear(false, 'file_test'); // Cache::clear(false, 'file_test');
Cache::drop('file_test'); Cache::drop('file_test');
Cache::drop('file_groups'); Cache::drop('file_groups');
Cache::drop('file_groups2'); Cache::drop('file_groups2');
@ -265,7 +265,7 @@ class FileEngineTest extends CakeTestCase {
$engine->init(array( $engine->init(array(
'prefix' => 'cake_test_', 'prefix' => 'cake_test_',
'duration' => DAY, 'duration' => DAY,
'groups' => array('short') 'groups' => array('short', 'round')
)); ));
$key = 'cake_test_test_key'; $key = 'cake_test_test_key';
$engine->write($key, 'it works', DAY); $engine->write($key, 'it works', DAY);
@ -273,6 +273,23 @@ class FileEngineTest extends CakeTestCase {
$this->assertFalse($engine->read($key), 'Key should have been removed'); $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 * testKeyPath method
* *