Skip to content

Commit

Permalink
Implemented group cache delete in FileEngine
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Mar 26, 2012
1 parent 04ec413 commit 945925b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/Cake/Cache/Engine/FileEngine.php
Expand Up @@ -351,7 +351,20 @@ public function key($key) {
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;
}
}
30 changes: 30 additions & 0 deletions lib/Cake/Test/Case/Cache/Engine/FileEngineTest.php
Expand Up @@ -54,6 +54,8 @@ public function tearDown() {
Cache::clear(false, 'file_test');
Cache::drop('file_test');
Cache::drop('file_groups');
Cache::drop('file_groups2');
Cache::drop('file_groups3');
}

/**
Expand Down Expand Up @@ -422,4 +424,32 @@ public function testGroupDelete() {
$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'));
}
}

0 comments on commit 945925b

Please sign in to comment.