Implemented read/write support for groups in FileEngine

This commit is contained in:
Jose Lorenzo Rodriguez 2012-03-25 23:00:28 -04:30
parent c5c99a7c29
commit 04ec41384b
2 changed files with 61 additions and 1 deletions

View file

@ -82,6 +82,9 @@ class FileEngine extends CacheEngine {
if (substr($this->settings['path'], -1) !== DS) {
$this->settings['path'] .= DS;
}
if (!empty($this->groupPrefix)) {
$this->groupPrefix = str_replace('_', DS, $this->groupPrefix);
}
return $this->_active();
}
@ -284,7 +287,17 @@ class FileEngine extends CacheEngine {
* @return boolean true if the cache key could be set, false otherwise
*/
protected function _setKey($key, $createKey = false) {
$path = new SplFileInfo($this->settings['path'] . $key);
$groups = null;
if (!empty($this->groupPrefix)) {
$groups = vsprintf($this->groupPrefix, $this->groups());
}
$dir = $this->settings['path'] . $groups;
if (!is_dir($dir)) {
mkdir($dir, 0777, true);
}
$path = new SplFileInfo($dir . $key);
if (!$createKey && !$path->isFile()) {
return false;
@ -323,4 +336,22 @@ class FileEngine extends CacheEngine {
return true;
}
/**
* Generates a safe key for use with cache engine storage engines.
*
* @param string $key the key passed over
* @return mixed string $key or false
*/
public function key($key) {
if (empty($key)) {
return false;
}
$key = Inflector::underscore(str_replace(array(DS, '/', '.'), '_', strval($key)));
return $key;
}
public function clearGroup($group) {
}
}

View file

@ -53,6 +53,7 @@ class FileEngineTest extends CakeTestCase {
parent::tearDown();
Cache::clear(false, 'file_test');
Cache::drop('file_test');
Cache::drop('file_groups');
}
/**
@ -393,4 +394,32 @@ class FileEngineTest extends CakeTestCase {
Cache::drop('mask_test');
}
/**
* Tests that configuring groups for stored keys return the correct values when read/written
*
* @return void
*/
public function testGroupsReadWrite() {
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->assertEquals('value', Cache::read('test_groups', 'file_groups'));
$this->assertTrue(Cache::write('test_groups2', 'value2', 'file_groups'));
$this->assertTrue(Cache::write('test_groups3', 'value3', 'file_groups'));
}
/**
* Tests that deleteing from a groups-enabled config is possible
*
* @return void
*/
public function testGroupDelete() {
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->assertEquals('value', Cache::read('test_groups', 'file_groups'));
$this->assertTrue(Cache::delete('test_groups', 'file_groups'));
$this->assertFalse(Cache::read('test_groups', 'file_groups'));
}
}