diff --git a/lib/Cake/Cache/Engine/FileEngine.php b/lib/Cake/Cache/Engine/FileEngine.php index 9213a5f2a..54d3a929f 100644 --- a/lib/Cake/Cache/Engine/FileEngine.php +++ b/lib/Cake/Cache/Engine/FileEngine.php @@ -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) { + + } } diff --git a/lib/Cake/Test/Case/Cache/Engine/FileEngineTest.php b/lib/Cake/Test/Case/Cache/Engine/FileEngineTest.php index b59b3029a..4d719fb0e 100644 --- a/lib/Cake/Test/Case/Cache/Engine/FileEngineTest.php +++ b/lib/Cake/Test/Case/Cache/Engine/FileEngineTest.php @@ -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')); + } + }