diff --git a/cake/libs/view/view.php b/cake/libs/view/view.php index c3343af5fde..6487997a453 100644 --- a/cake/libs/view/view.php +++ b/cake/libs/view/view.php @@ -325,23 +325,22 @@ public function element($name, $params = array(), $callbacks = false) { } if (isset($params['cache'])) { - $expires = '+1 day'; - if (is_array($params['cache'])) { - $expires = $params['cache']['time']; - $key = Inflector::slug($params['cache']['key']); - } elseif ($params['cache'] !== true) { - $expires = $params['cache']; - $key = implode('_', array_keys($params)); + $defaults = array( + 'config' => 'default', + 'key' => '', + ); + $caching = array_merge($defaults, $params['cache']); + } else { + $caching = array( + 'config' => 'default', + 'key' => implode('_', array_keys($params)) + ); } - - if ($expires) { - $cacheFile = 'element_' . $key . '_' . $plugin . Inflector::slug($name); - $cache = cache('views' . DS . $cacheFile, null, $expires); - - if (is_string($cache)) { - return $cache; - } + $key = 'element_' . $caching['key'] . '_' . $plugin . Inflector::slug($name); + $contents = Cache::read($key, $caching['config']); + if ($contents !== false) { + return $contents; } } $paths = $this->_paths($plugin); @@ -364,8 +363,8 @@ public function element($name, $params = array(), $callbacks = false) { if ($callbacks) { $this->Helpers->trigger('afterRender', array($file, $element)); } - if (isset($params['cache']) && isset($cacheFile) && isset($expires)) { - cache('views' . DS . $cacheFile, $element, $expires); + if (isset($params['cache'])) { + Cache::write($key, $element, $caching['config']); } return $element; } diff --git a/cake/tests/cases/libs/view/view.test.php b/cake/tests/cases/libs/view/view.test.php index 965aa02518f..a8fdc6948fd 100644 --- a/cake/tests/cases/libs/view/view.test.php +++ b/cake/tests/cases/libs/view/view.test.php @@ -511,6 +511,22 @@ function testElementCacheHelperNoCache() { * @return void */ function testElementCache() { + Cache::drop('test_view'); + Cache::config('test_view', array( + 'engine' => 'File', + 'duration' => '+1 day', + 'path' => CACHE . 'views' . DS + )); + + $View = new TestView($this->PostsController); + $result = $View->element('test_element', array('cache' => array('config' => 'test_view'))); + $expected = 'this is the test element'; + $this->assertEquals($expected, $result); + + $result = Cache::read('element__test_element', 'test_view'); + $this->assertEquals($expected, $result); + +/* $writable = is_writable(CACHE . 'views' . DS); if ($this->skipIf(!$writable, 'CACHE/views dir is not writable, cannot test elementCache. %s')) { return; @@ -553,7 +569,7 @@ function testElementCache() { } $this->assertTrue($cached); $this->assertEqual($result, $expected); - + */ } /**