Permalink
Browse files

Implemented group delete for cache keys in ApcEngine

  • Loading branch information...
1 parent ae34c64 commit 4f131d54f15099b90f8756715023cb5af6cdc7e3 @lorenzo lorenzo committed Mar 26, 2012
View
@@ -454,6 +454,22 @@ public static function clear($check = false, $config = 'default') {
}
/**
+ * Delete all keys from the cache belonging to the same group.
+ *
+ * @param string $group name of the group to be cleared
+ * @param string $config name of the configuration to use. Defaults to 'default'
+ * @return boolean True if the cache group was successfully cleared, false otherwise
+ */
+ public static function clearGroup($group, $config = 'default') {
+ if (!self::isInitialized($config)) {
+ return false;
+ }
+ $success = self::$_engines[$config]->clearGroup($group);
+ self::set(null, $config);
+ return $success;
+ }
+
+/**
* Check if Cache has initialized a working config for the given name.
*
* @param string $config name of the configuration to use. Defaults to 'default'
@@ -121,6 +121,16 @@ public function gc() {
abstract public function clear($check);
/**
+ * Clears all values belonging to a group. Is upt to the implementing engine
+ * to decide whether actually deete the keys or just simulate it to acheive
+ * the same result.
+ *
+ * @param string $groups name of the group to be cleared
+ * @return void
+ **/
+ abstract public function clearGroup($group);
+
+/**
* Does whatever initialization for each group is required
* and returns the `group value` for each of them, this is
* the token representing each group in the cache key
@@ -153,4 +153,16 @@ public function groups() {
}
return $result;
}
+
+/**
+ * Increments the group value to simulate deletion of all keys under a group
+ * old values will remain in sotrage until they expire.
@jrbasso

jrbasso Mar 26, 2012

Member

Little typo in "sotrage".

@lorenzo

lorenzo Mar 26, 2012

Owner

thanks :)

+ *
+ * @return boolean success
+ **/
+ public function clearGroup($group) {
+ apc_inc($group, 1, $success);
+ return $success;
+ }
+
}
@@ -236,4 +236,21 @@ public function testGroupDelete() {
$this->assertFalse(Cache::read('test_groups', 'apc_groups'));
}
+
+/**
+ * Test clearing a cache group
+ *
+ * @return void
+ **/
+ public function testGroupClear() {
+ Cache::config('apc_groups', array('engine' => 'Apc', 'duration' => 0, 'groups' => array('group_a', 'group_b')));
+
+ $this->assertTrue(Cache::write('test_groups', 'value', 'apc_groups'));
+ $this->assertTrue(Cache::clearGroup('group_a', 'apc_groups'));
+ $this->assertFalse(Cache::read('test_groups', 'apc_groups'));
+
+ $this->assertTrue(Cache::write('test_groups', 'value2', 'apc_groups'));
+ $this->assertTrue(Cache::clearGroup('group_b', 'apc_groups'));
+ $this->assertFalse(Cache::read('test_groups', 'apc_groups'));
+ }
}

0 comments on commit 4f131d5

Please sign in to comment.