Implemented group cache delete in FileEngine

This commit is contained in:
Jose Lorenzo Rodriguez 2012-03-25 23:33:37 -04:30
parent 04ec41384b
commit 945925bdb9
2 changed files with 44 additions and 1 deletions

View file

@ -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;
}
}

View file

@ -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'));
}
}