Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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

  • Loading branch information...
commit 5682907a88e0a4575f0d2b4e74521fcba6f58c49 1 parent 62186ac
@rchavik rchavik authored
Showing with 109 additions and 0 deletions.
  1. +45 −0 lib/Cake/Cache/Cache.php
  2. +64 −0 lib/Cake/Test/Case/Cache/CacheTest.php
View
45 lib/Cake/Cache/Cache.php
@@ -52,6 +52,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();
*
* @var array
@@ -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])) {
@@ -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));
+ }
+ }
+
}
View
64 lib/Cake/Test/Case/Cache/CacheTest.php
@@ -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']);
}
@@ -238,6 +241,67 @@ public function testWritingWithConfig() {
}
/**
+ * 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
*
Please sign in to comment.
Something went wrong with that request. Please try again.