Skip to content

Commit

Permalink
Implemented read/write support for groups in FileEngine
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Mar 26, 2012
1 parent c5c99a7 commit 04ec413
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
33 changes: 32 additions & 1 deletion lib/Cake/Cache/Engine/FileEngine.php
Expand Up @@ -82,6 +82,9 @@ public function init($settings = array()) {
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();
}

Expand Down Expand Up @@ -284,7 +287,17 @@ public function increment($key, $offset = 1) {
* @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;
Expand Down Expand Up @@ -323,4 +336,22 @@ protected function _active() {
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) {

}
}
29 changes: 29 additions & 0 deletions lib/Cake/Test/Case/Cache/Engine/FileEngineTest.php
Expand Up @@ -53,6 +53,7 @@ public function tearDown() {
parent::tearDown();
Cache::clear(false, 'file_test');
Cache::drop('file_test');
Cache::drop('file_groups');
}

/**
Expand Down Expand Up @@ -393,4 +394,32 @@ public function testMaskSetting() {
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'));
}

}

0 comments on commit 04ec413

Please sign in to comment.