Skip to content

Commit

Permalink
Only delete files that have a matching group + prefix.
Browse files Browse the repository at this point in the history
Fixes #3873
  • Loading branch information
markstory committed Jun 9, 2013
1 parent 0f38dbd commit e5b1182
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
3 changes: 2 additions & 1 deletion lib/Cake/Cache/Engine/FileEngine.php
Expand Up @@ -365,7 +365,8 @@ public function clearGroup($group) {
$contents = new RecursiveIteratorIterator($directoryIterator, RecursiveIteratorIterator::CHILD_FIRST); $contents = new RecursiveIteratorIterator($directoryIterator, RecursiveIteratorIterator::CHILD_FIRST);
foreach ($contents as $object) { foreach ($contents as $object) {
$containsGroup = strpos($object->getPathName(), DS . $group . DS) !== false; $containsGroup = strpos($object->getPathName(), DS . $group . DS) !== false;
if ($object->isFile() && $containsGroup) { $hasPrefix = strpos($object->getBaseName(), $this->settings['prefix']) === 0;
if ($object->isFile() && $containsGroup && $hasPrefix) {
unlink($object->getPathName()); unlink($object->getPathName());
} }
} }
Expand Down
29 changes: 19 additions & 10 deletions lib/Cake/Test/Case/Cache/Engine/FileEngineTest.php
Expand Up @@ -443,7 +443,11 @@ public function testClearingWithRepeatWrites() {
* @return void * @return void
*/ */
public function testGroupDelete() { public function testGroupDelete() {
Cache::config('file_groups', array('engine' => 'File', 'duration' => 3600, 'groups' => array('group_a', 'group_b'))); Cache::config('file_groups', array(
'engine' => 'File',
'duration' => 3600,
'groups' => array('group_a', 'group_b')
));
$this->assertTrue(Cache::write('test_groups', 'value', 'file_groups')); $this->assertTrue(Cache::write('test_groups', 'value', 'file_groups'));
$this->assertEquals('value', Cache::read('test_groups', 'file_groups')); $this->assertEquals('value', Cache::read('test_groups', 'file_groups'));
$this->assertTrue(Cache::delete('test_groups', 'file_groups')); $this->assertTrue(Cache::delete('test_groups', 'file_groups'));
Expand All @@ -459,24 +463,29 @@ public function testGroupDelete() {
public function testGroupClear() { public function testGroupClear() {
Cache::config('file_groups', array('engine' => 'File', 'duration' => 3600, 'groups' => array('group_a', 'group_b'))); 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_groups2', array('engine' => 'File', 'duration' => 3600, 'groups' => array('group_b')));
Cache::config('file_groups3', array('engine' => 'File', 'duration' => 3600, 'groups' => array('group_a'))); Cache::config('file_groups3', array(
'engine' => 'File',
'duration' => 3600,
'groups' => array('group_b'),
'prefix' => 'leading_',
));


$this->assertTrue(Cache::write('test_groups', 'value', 'file_groups')); $this->assertTrue(Cache::write('test_groups', 'value', 'file_groups'));
$this->assertTrue(Cache::write('test_groups2', 'value', 'file_groups2')); $this->assertTrue(Cache::write('test_groups2', 'value 2', 'file_groups2'));
$this->assertTrue(Cache::write('test_groups3', 'value', 'file_groups3')); $this->assertTrue(Cache::write('test_groups3', 'value 3', 'file_groups3'));


$this->assertTrue(Cache::clearGroup('group_a', 'file_groups')); $this->assertTrue(Cache::clearGroup('group_b', 'file_groups'));
$this->assertFalse(Cache::read('test_groups', 'file_groups')); $this->assertFalse(Cache::read('test_groups', 'file_groups'));
$this->assertEquals('value', Cache::read('test_groups2', 'file_groups2')); $this->assertFalse(Cache::read('test_groups2', 'file_groups2'));
$this->assertFalse(Cache::read('test_groups3', 'file_groups3')); $this->assertEquals('value 3', Cache::read('test_groups3', 'file_groups3'));


$this->assertTrue(Cache::write('test_groups4', 'value', 'file_groups')); $this->assertTrue(Cache::write('test_groups4', 'value', 'file_groups'));
$this->assertTrue(Cache::write('test_groups5', 'value', 'file_groups2')); $this->assertTrue(Cache::write('test_groups5', 'value 2', 'file_groups2'));
$this->assertTrue(Cache::write('test_groups6', 'value', 'file_groups3')); $this->assertTrue(Cache::write('test_groups6', 'value 3', 'file_groups3'));


$this->assertTrue(Cache::clearGroup('group_b', 'file_groups')); $this->assertTrue(Cache::clearGroup('group_b', 'file_groups'));
$this->assertFalse(Cache::read('test_groups4', 'file_groups')); $this->assertFalse(Cache::read('test_groups4', 'file_groups'));
$this->assertFalse(Cache::read('test_groups5', 'file_groups2')); $this->assertFalse(Cache::read('test_groups5', 'file_groups2'));
$this->assertEquals('value', Cache::read('test_groups6', 'file_groups3')); $this->assertEquals('value 3', Cache::read('test_groups6', 'file_groups3'));
} }
} }

0 comments on commit e5b1182

Please sign in to comment.