From b3860b399221d5b2c5b9fac85a8881cb4ca9e146 Mon Sep 17 00:00:00 2001 From: mark_story Date: Fri, 2 Aug 2013 17:56:11 -0400 Subject: [PATCH] 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 --- lib/Cake/Cache/Engine/FileEngine.php | 36 +++++++++++-------- .../Test/Case/Cache/Engine/FileEngineTest.php | 21 +++++++++-- 2 files changed, 41 insertions(+), 16 deletions(-) diff --git a/lib/Cake/Cache/Engine/FileEngine.php b/lib/Cake/Cache/Engine/FileEngine.php index 27b4dba1e..6e5c9b1c3 100644 --- a/lib/Cake/Cache/Engine/FileEngine.php +++ b/lib/Cake/Cache/Engine/FileEngine.php @@ -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(); } /** diff --git a/lib/Cake/Test/Case/Cache/Engine/FileEngineTest.php b/lib/Cake/Test/Case/Cache/Engine/FileEngineTest.php index d82b9d607..e19c80dee 100644 --- a/lib/Cake/Test/Case/Cache/Engine/FileEngineTest.php +++ b/lib/Cake/Test/Case/Cache/Engine/FileEngineTest.php @@ -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 *