Implementing group read/write support to MemcacheEngine

This commit is contained in:
Jose Lorenzo Rodriguez 2012-03-25 20:08:37 -04:30
parent 4f131d54f1
commit 411bd85900
2 changed files with 63 additions and 0 deletions

View file

@ -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) {
}
}

View file

@ -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'));
}
}