From 945925bdb999142a62809b5c3381ecf1bc4658a0 Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Sun, 25 Mar 2012 23:33:37 -0430 Subject: [PATCH] Implemented group cache delete in FileEngine --- lib/Cake/Cache/Engine/FileEngine.php | 15 +++++++++- .../Test/Case/Cache/Engine/FileEngineTest.php | 30 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/lib/Cake/Cache/Engine/FileEngine.php b/lib/Cake/Cache/Engine/FileEngine.php index 54d3a929f..b79f9e658 100644 --- a/lib/Cake/Cache/Engine/FileEngine.php +++ b/lib/Cake/Cache/Engine/FileEngine.php @@ -351,7 +351,20 @@ class FileEngine extends CacheEngine { return $key; } +/** + * Recursively deletes all files under any directory named as $group + * + * @return boolean success + **/ public function clearGroup($group) { - + $directoryIterator = new RecursiveDirectoryIterator($this->settings['path']); + $contents = new RecursiveIteratorIterator($directoryIterator, RecursiveIteratorIterator::CHILD_FIRST); + foreach ($contents as $object) { + $containsGroup = strpos($object->getPathName(), DS . $group . DS) !== false; + if ($object->isFile() && $containsGroup) { + unlink($object->getPathName()); + } + } + return true; } } diff --git a/lib/Cake/Test/Case/Cache/Engine/FileEngineTest.php b/lib/Cake/Test/Case/Cache/Engine/FileEngineTest.php index 4d719fb0e..f7b54f3b1 100644 --- a/lib/Cake/Test/Case/Cache/Engine/FileEngineTest.php +++ b/lib/Cake/Test/Case/Cache/Engine/FileEngineTest.php @@ -54,6 +54,8 @@ class FileEngineTest extends CakeTestCase { Cache::clear(false, 'file_test'); Cache::drop('file_test'); Cache::drop('file_groups'); + Cache::drop('file_groups2'); + Cache::drop('file_groups3'); } /** @@ -422,4 +424,32 @@ class FileEngineTest extends CakeTestCase { $this->assertFalse(Cache::read('test_groups', 'file_groups')); } +/** + * Test clearing a cache group + * + * @return void + **/ + public function testGroupClear() { + Cache::config('file_groups', array('engine' => 'File', 'duration' => 3600, 'groups' => array('group_a', 'group_b'))); + Cache::config('file_groups2', array('engine' => 'File', 'duration' => 3600, 'groups' => array('group_b'))); + Cache::config('file_groups3', array('engine' => 'File', 'duration' => 3600, 'groups' => array('group_a'))); + + $this->assertTrue(Cache::write('test_groups', 'value', 'file_groups')); + $this->assertTrue(Cache::write('test_groups2', 'value', 'file_groups2')); + $this->assertTrue(Cache::write('test_groups3', 'value', 'file_groups3')); + + $this->assertTrue(Cache::clearGroup('group_a', 'file_groups')); + $this->assertFalse(Cache::read('test_groups', 'file_groups')); + $this->assertEquals('value', Cache::read('test_groups2', 'file_groups2')); + $this->assertFalse(Cache::read('test_groups3', 'file_groups3')); + + $this->assertTrue(Cache::write('test_groups4', 'value', 'file_groups')); + $this->assertTrue(Cache::write('test_groups5', 'value', 'file_groups2')); + $this->assertTrue(Cache::write('test_groups6', 'value', 'file_groups3')); + + $this->assertTrue(Cache::clearGroup('group_b', 'file_groups')); + $this->assertFalse(Cache::read('test_groups4', 'file_groups')); + $this->assertFalse(Cache::read('test_groups5', 'file_groups2')); + $this->assertEquals('value', Cache::read('test_groups6', 'file_groups3')); + } }