Permalink
Browse files

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
  • Loading branch information...
1 parent 19ac399 commit b3860b399221d5b2c5b9fac85a8881cb4ca9e146 @markstory markstory committed Aug 2, 2013
Showing with 41 additions and 16 deletions.
  1. +22 −14 lib/Cake/Cache/Engine/FileEngine.php
  2. +19 −2 lib/Cake/Test/Case/Cache/Engine/FileEngineTest.php
View
36 lib/Cake/Cache/Engine/FileEngine.php
@@ -228,9 +228,16 @@ public function clear($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);
+
+ $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 @@ public function clear($check) {
* @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
21 lib/Cake/Test/Case/Cache/Engine/FileEngineTest.php
@@ -52,7 +52,7 @@ public function setUp() {
*/
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 @@ public function testClearWithGroups() {
$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);
@@ -274,6 +274,23 @@ public function testClearWithGroups() {
}
/**
+ * 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
*
* @return void

0 comments on commit b3860b3

Please sign in to comment.