Permalink
Browse files

Implementing group support for XcacheEngine

  • Loading branch information...
1 parent 6f9d2c0 commit 9c733b44208ac599d131b9a6ab98d193ae1679cc @lorenzo lorenzo committed Mar 26, 2012
Showing with 86 additions and 1 deletion.
  1. +30 −1 lib/Cake/Cache/Engine/XcacheEngine.php
  2. +56 −0 lib/Cake/Test/Case/Cache/Engine/XcacheEngineTest.php
@@ -138,6 +138,36 @@ public function clear($check) {
}
/**
+ * 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() {
+ $result = array();
+ foreach ($this->settings['groups'] as $group) {
+ $value = xcache_get($group);
+ if (!$value) {
+ $value = 1;
+ xcache_set($group, $value, 0);
+ }
+ $result[] = $group . $value;
+ }
+ return $result;
+ }
+
+/**
+ * Increments the group value to simulate deletion of all keys under a group
+ * old values will remain in sotrage until they expire.
+ *
+ * @return boolean success
+ **/
+ public function clearGroup($group) {
+ return (bool) xcache_inc($group, 1);
+ }
+
+/**
* Populates and reverses $_SERVER authentication values
* Makes necessary changes (and reverting them back) in $_SERVER
*
@@ -173,5 +203,4 @@ protected function _auth($reverse = false) {
}
}
}
-
}
@@ -47,6 +47,8 @@ public function setUp() {
*/
public function tearDown() {
Configure::write('Cache.disable', $this->_cacheDisable);
+ Cache::drop('xcache');
+ Cache::drop('xcache_groups');
Cache::config('default');
}
@@ -196,4 +198,58 @@ public function testIncrement() {
$result = Cache::read('test_increment');
$this->assertEquals(8, $result);
}
+
+/**
+ * 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 testGroupsReadWrite() {
+ Cache::config('xcache_groups', array('engine' => 'Xcache', 'duration' => 0, 'groups' => array('group_a', 'group_b')));
+ $this->assertTrue(Cache::write('test_groups', 'value', 'xcache_groups'));
+ $this->assertEquals('value', Cache::read('test_groups', 'xcache_groups'));
+
+ xcache_inc('group_a', 1);
+ $this->assertFalse(Cache::read('test_groups', 'xcache_groups'));
+ $this->assertTrue(Cache::write('test_groups', 'value2', 'xcache_groups'));
+ $this->assertEquals('value2', Cache::read('test_groups', 'xcache_groups'));
+
+ xcache_inc('group_b', 1);
+ $this->assertFalse(Cache::read('test_groups', 'xcache_groups'));
+ $this->assertTrue(Cache::write('test_groups', 'value3', 'xcache_groups'));
+ $this->assertEquals('value3', Cache::read('test_groups', 'xcache_groups'));
+ }
+
+/**
+ * Tests that deleteing from a groups-enabled config is possible
+ *
+ * @return void
+ */
+ public function testGroupDelete() {
+ Cache::config('xcache_groups', array('engine' => 'Xcache', 'duration' => 0, 'groups' => array('group_a', 'group_b')));
+ $this->assertTrue(Cache::write('test_groups', 'value', 'xcache_groups'));
+ $this->assertEquals('value', Cache::read('test_groups', 'xcache_groups'));
+ $this->assertTrue(Cache::delete('test_groups', 'xcache_groups'));
+
+ $this->assertFalse(Cache::read('test_groups', 'xcache_groups'));
+ }
+
+/**
+ * Test clearing a cache group
+ *
+ * @return void
+ **/
+ public function testGroupClear() {
+ Cache::config('xcache_groups', array('engine' => 'Xcache', 'duration' => 0, 'groups' => array('group_a', 'group_b')));
+
+ $this->assertTrue(Cache::write('test_groups', 'value', 'xcache_groups'));
+ $this->assertTrue(Cache::clearGroup('group_a', 'xcache_groups'));
+ $this->assertFalse(Cache::read('test_groups', 'xcache_groups'));
+
+ $this->assertTrue(Cache::write('test_groups', 'value2', 'xcache_groups'));
+ $this->assertTrue(Cache::clearGroup('group_b', 'xcache_groups'));
+ $this->assertFalse(Cache::read('test_groups', 'xcache_groups'));
+ }
}

0 comments on commit 9c733b4

Please sign in to comment.