Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add Cache::groupConfigs() to get group->config map
  • Loading branch information
rchavik committed May 1, 2013
1 parent 62186ac commit 5682907
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
45 changes: 45 additions & 0 deletions lib/Cake/Cache/Cache.php
Expand Up @@ -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();
*
Expand Down Expand Up @@ -130,6 +137,14 @@ public static function config($name = null, $settings = array()) {
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])) {
Expand Down Expand Up @@ -498,4 +513,34 @@ public static function settings($name = 'default') {
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));
}
}

}
64 changes: 64 additions & 0 deletions lib/Cake/Test/Case/Cache/CacheTest.php
Expand Up @@ -48,6 +48,9 @@ public function setUp() {
*/
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']);
}
Expand Down Expand Up @@ -237,6 +240,67 @@ public function testWritingWithConfig() {
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
Expand Down

0 comments on commit 5682907

Please sign in to comment.