From 5682907a88e0a4575f0d2b4e74521fcba6f58c49 Mon Sep 17 00:00:00 2001 From: Rachman Chavik Date: Tue, 30 Apr 2013 10:42:56 +0700 Subject: [PATCH] Add Cache::groupConfigs() to get group->config map --- lib/Cake/Cache/Cache.php | 45 ++++++++++++++++++ lib/Cake/Test/Case/Cache/CacheTest.php | 64 ++++++++++++++++++++++++++ 2 files changed, 109 insertions(+) diff --git a/lib/Cake/Cache/Cache.php b/lib/Cake/Cache/Cache.php index e36375b47..858bfe4f0 100644 --- a/lib/Cake/Cache/Cache.php +++ b/lib/Cake/Cache/Cache.php @@ -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)); + } + } + } diff --git a/lib/Cake/Test/Case/Cache/CacheTest.php b/lib/Cake/Test/Case/Cache/CacheTest.php index 43cdddd24..9018bc4e7 100644 --- a/lib/Cake/Test/Case/Cache/CacheTest.php +++ b/lib/Cake/Test/Case/Cache/CacheTest.php @@ -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