Browse Source

Implementing group read/write support to MemcacheEngine

Jose Lorenzo Rodriguez 14 years ago
parent
commit
411bd85900

+ 30 - 0
lib/Cake/Cache/Engine/MemcacheEngine.php

@@ -235,4 +235,34 @@ class MemcacheEngine extends CacheEngine {
 		return true;
 	}
 
+/**
+ * Returns the `group value` for each of the configured groups
+ * If the group initial value was not found, then it initializes
+ * the group accordingly.
+ *
+ * @return array
+ **/
+	public function groups() {
+		$groups = $this->_Memcache->get($this->settings['groups']);
+		if (count($groups) !== count($this->settings['groups'])) {
+			foreach ($this->settings['groups'] as $group) {
+				if (!isset($groups[$group])) {
+					$this->_Memcache->set($group, 1, false, 0);
+					$groups[$group] = 1;
+				}
+			}
+			ksort($groups);
+		}
+
+		$result = array();
+		foreach ($groups as $group => $value) {
+			$result[] = $group . $value;
+		}
+
+		return $result;
+	}
+
+	public function clearGroup($group) {
+
+	}
 }

+ 33 - 0
lib/Cake/Test/Case/Cache/Engine/MemcacheEngineTest.php

@@ -70,6 +70,8 @@ class MemcacheEngineTest extends CakeTestCase {
 	public function tearDown() {
 		Configure::write('Cache.disable', $this->_cacheDisable);
 		Cache::drop('memcache');
+		Cache::drop('memcache_groups');
+		Cache::drop('memcache_helper');
 		Cache::config('default');
 	}
 
@@ -400,4 +402,35 @@ class MemcacheEngineTest extends CakeTestCase {
 		$memcache->write('key', $value, 50 * DAY);
 	}
 
+/**
+ * Tests that configuring groups for stored keys return the correct values when read/written
+ * Shows that altering the group value is equivalent to deleting all keys under the same
+ * group
+ *
+ * @return void
+ */
+	public function testGroupReadWrite() {
+		Cache::config('memcache_groups', array(
+			'engine' => 'Memcache',
+			'duration' => 3600,
+			'groups' => array('group_a', 'group_b')
+		));
+		Cache::config('memcache_helper', array(
+			'engine' => 'Memcache',
+			'duration' => 3600,
+			'prefix' => ''
+		));
+		$this->assertTrue(Cache::write('test_groups', 'value', 'memcache_groups'));
+		$this->assertEquals('value', Cache::read('test_groups', 'memcache_groups'));
+
+		Cache::increment('group_a', 1, 'memcache_helper');
+		$this->assertFalse(Cache::read('test_groups', 'memcache_groups'));
+		$this->assertTrue(Cache::write('test_groups', 'value2', 'memcache_groups'));
+		$this->assertEquals('value2', Cache::read('test_groups', 'memcache_groups'));
+
+		Cache::increment('group_b', 1, 'memcache_helper');
+		$this->assertFalse(Cache::read('test_groups', 'memcache_groups'));
+		$this->assertTrue(Cache::write('test_groups', 'value3', 'memcache_groups'));
+		$this->assertEquals('value3', Cache::read('test_groups', 'memcache_groups'));
+	}
 }