Add Cache::groupConfigs() to get group->config map

This commit is contained in:
Rachman Chavik 2013-04-30 10:42:56 +07:00
parent 62186ac8da
commit 5682907a88
2 changed files with 109 additions and 0 deletions

View file

@ -51,6 +51,13 @@ class Cache {
*/
protected static $_config = array();
/**
* Group to Config mapping
*
* @var array
*/
protected static $_groups = array();
/**
* Whether to reset the settings with the next call to Cache::set();
*
@ -130,6 +137,14 @@ class Cache {
return false;
}
if (!empty(self::$_config[$name]['groups'])) {
foreach (self::$_config[$name]['groups'] as $group) {
self::$_groups[$group][] = $name;
sort(self::$_groups[$group]);
self::$_groups[$group] = array_unique(self::$_groups[$group]);
}
}
$engine = self::$_config[$name]['engine'];
if (!isset(self::$_engines[$name])) {
@ -498,4 +513,34 @@ class Cache {
return array();
}
/**
* Retrieve group names to config mapping.
*
* {{{
* Cache::config('daily', array(
* 'duration' => '1 day', 'groups' => array('posts')
* ));
* Cache::config('weekly', array(
* 'duration' => '1 week', 'groups' => array('posts', 'archive')
* ));
* $configs = Cache::groupConfigs('posts');
* }}}
*
* $config will equal to `array('posts' => array('daily', 'weekly'))`
*
* @param string $group group name or null to retrieve all group mappings
* @return array map of group and all configuration that has the same group
* @throws CacheException
*/
public static function groupConfigs($group = null) {
if ($group == null) {
return self::$_groups;
}
if (isset(self::$_groups[$group])) {
return array($group => self::$_groups[$group]);
} else {
throw new CacheException(__d('cake_dev', 'Invalid cache group %s', $group));
}
}
}

View file

@ -48,6 +48,9 @@ class CacheTest extends CakeTestCase {
*/
public function tearDown() {
parent::tearDown();
Cache::drop('latest');
Cache::drop('page');
Cache::drop('archive');
Configure::write('Cache.disable', $this->_cacheDisable);
Cache::config('default', $this->_defaultCacheConfig['settings']);
}
@ -237,6 +240,67 @@ class CacheTest extends CakeTestCase {
Cache::config('sessions', $_cacheConfigSessions['settings']);
}
/**
* testGroupConfigs method
*/
public function testGroupConfigs() {
Cache::config('latest', array(
'duration' => 300,
'engine' => 'File',
'groups' => array(
'posts', 'comments',
),
));
$expected = array(
'posts' => array('latest'),
'comments' => array('latest'),
);
$result = Cache::groupConfigs();
$this->assertEquals($expected, $result);
$result = Cache::groupConfigs('posts');
$this->assertEquals(array('posts' => array('latest')), $result);
Cache::config('page', array(
'duration' => 86400,
'engine' => 'File',
'groups' => array(
'posts', 'archive'
),
));
$result = Cache::groupConfigs();
$expected = array(
'posts' => array('latest', 'page'),
'comments' => array('latest'),
'archive' => array('page'),
);
$this->assertEquals($expected, $result);
$result = Cache::groupConfigs('archive');
$this->assertEquals(array('archive' => array('page')), $result);
Cache::config('archive', array(
'duration' => 86400 * 30,
'engine' => 'File',
'groups' => array(
'posts', 'archive', 'comments',
),
));
$result = Cache::groupConfigs('archive');
$this->assertEquals(array('archive' => array('archive', 'page')), $result);
}
/**
* testGroupConfigsThrowsException method
* @expectedException CacheException
*/
public function testGroupConfigsThrowsException() {
Cache::groupConfigs('bogus');
}
/**
* test that configured returns an array of the currently configured cache
* settings