Skip to content

Commit 5682907

Browse files
committed
Add Cache::groupConfigs() to get group->config map
1 parent 62186ac commit 5682907

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

lib/Cake/Cache/Cache.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ class Cache {
5151
*/
5252
protected static $_config = array();
5353

54+
/**
55+
* Group to Config mapping
56+
*
57+
* @var array
58+
*/
59+
protected static $_groups = array();
60+
5461
/**
5562
* Whether to reset the settings with the next call to Cache::set();
5663
*
@@ -130,6 +137,14 @@ public static function config($name = null, $settings = array()) {
130137
return false;
131138
}
132139

140+
if (!empty(self::$_config[$name]['groups'])) {
141+
foreach (self::$_config[$name]['groups'] as $group) {
142+
self::$_groups[$group][] = $name;
143+
sort(self::$_groups[$group]);
144+
self::$_groups[$group] = array_unique(self::$_groups[$group]);
145+
}
146+
}
147+
133148
$engine = self::$_config[$name]['engine'];
134149

135150
if (!isset(self::$_engines[$name])) {
@@ -498,4 +513,34 @@ public static function settings($name = 'default') {
498513
return array();
499514
}
500515

516+
/**
517+
* Retrieve group names to config mapping.
518+
*
519+
* {{{
520+
* Cache::config('daily', array(
521+
* 'duration' => '1 day', 'groups' => array('posts')
522+
* ));
523+
* Cache::config('weekly', array(
524+
* 'duration' => '1 week', 'groups' => array('posts', 'archive')
525+
* ));
526+
* $configs = Cache::groupConfigs('posts');
527+
* }}}
528+
*
529+
* $config will equal to `array('posts' => array('daily', 'weekly'))`
530+
*
531+
* @param string $group group name or null to retrieve all group mappings
532+
* @return array map of group and all configuration that has the same group
533+
* @throws CacheException
534+
*/
535+
public static function groupConfigs($group = null) {
536+
if ($group == null) {
537+
return self::$_groups;
538+
}
539+
if (isset(self::$_groups[$group])) {
540+
return array($group => self::$_groups[$group]);
541+
} else {
542+
throw new CacheException(__d('cake_dev', 'Invalid cache group %s', $group));
543+
}
544+
}
545+
501546
}

lib/Cake/Test/Case/Cache/CacheTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ public function setUp() {
4848
*/
4949
public function tearDown() {
5050
parent::tearDown();
51+
Cache::drop('latest');
52+
Cache::drop('page');
53+
Cache::drop('archive');
5154
Configure::write('Cache.disable', $this->_cacheDisable);
5255
Cache::config('default', $this->_defaultCacheConfig['settings']);
5356
}
@@ -237,6 +240,67 @@ public function testWritingWithConfig() {
237240
Cache::config('sessions', $_cacheConfigSessions['settings']);
238241
}
239242

243+
/**
244+
* testGroupConfigs method
245+
*/
246+
public function testGroupConfigs() {
247+
Cache::config('latest', array(
248+
'duration' => 300,
249+
'engine' => 'File',
250+
'groups' => array(
251+
'posts', 'comments',
252+
),
253+
));
254+
255+
$expected = array(
256+
'posts' => array('latest'),
257+
'comments' => array('latest'),
258+
);
259+
$result = Cache::groupConfigs();
260+
$this->assertEquals($expected, $result);
261+
262+
$result = Cache::groupConfigs('posts');
263+
$this->assertEquals(array('posts' => array('latest')), $result);
264+
265+
Cache::config('page', array(
266+
'duration' => 86400,
267+
'engine' => 'File',
268+
'groups' => array(
269+
'posts', 'archive'
270+
),
271+
));
272+
273+
$result = Cache::groupConfigs();
274+
$expected = array(
275+
'posts' => array('latest', 'page'),
276+
'comments' => array('latest'),
277+
'archive' => array('page'),
278+
);
279+
$this->assertEquals($expected, $result);
280+
281+
$result = Cache::groupConfigs('archive');
282+
$this->assertEquals(array('archive' => array('page')), $result);
283+
284+
Cache::config('archive', array(
285+
'duration' => 86400 * 30,
286+
'engine' => 'File',
287+
'groups' => array(
288+
'posts', 'archive', 'comments',
289+
),
290+
));
291+
292+
$result = Cache::groupConfigs('archive');
293+
$this->assertEquals(array('archive' => array('archive', 'page')), $result);
294+
}
295+
296+
/**
297+
* testGroupConfigsThrowsException method
298+
* @expectedException CacheException
299+
*/
300+
public function testGroupConfigsThrowsException() {
301+
Cache::groupConfigs('bogus');
302+
}
303+
240304
/**
241305
* test that configured returns an array of the currently configured cache
242306
* settings

0 commit comments

Comments
 (0)