mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Implemented group delete for cache keys in ApcEngine
This commit is contained in:
parent
ae34c64fa3
commit
4f131d54f1
4 changed files with 55 additions and 0 deletions
|
@ -453,6 +453,22 @@ class Cache {
|
|||
return $success;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
|
|
|
@ -120,6 +120,16 @@ abstract class CacheEngine {
|
|||
*/
|
||||
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
|
||||
|
|
|
@ -153,4 +153,16 @@ class ApcEngine extends CacheEngine {
|
|||
}
|
||||
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) {
|
||||
apc_inc($group, 1, $success);
|
||||
return $success;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -236,4 +236,21 @@ class ApcEngineTest extends CakeTestCase {
|
|||
|
||||
$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'));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue