Permalink
Browse files

Implemented read/write support for groups in FileEngine

  • Loading branch information...
lorenzo committed Mar 26, 2012
1 parent c5c99a7 commit 04ec41384b84bacfc610251adac24fde9c595f32
Showing with 61 additions and 1 deletion.
  1. +32 −1 lib/Cake/Cache/Engine/FileEngine.php
  2. +29 −0 lib/Cake/Test/Case/Cache/Engine/FileEngineTest.php
@@ -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();
}
@@ -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;
@@ -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) {
+
+ }
}
@@ -53,6 +53,7 @@ public function tearDown() {
parent::tearDown();
Cache::clear(false, 'file_test');
Cache::drop('file_test');
+ Cache::drop('file_groups');
}
/**
@@ -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.