Skip to content

Commit

Permalink
Very initial pass at getting elements cached with Cache. Refs #1268
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Nov 8, 2010
1 parent a9a9bc0 commit dfefc2d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
33 changes: 16 additions & 17 deletions cake/libs/view/view.php
Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down
18 changes: 17 additions & 1 deletion cake/tests/cases/libs/view/view.test.php
Expand Up @@ -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;
Expand Down Expand Up @@ -553,7 +569,7 @@ function testElementCache() {
}
$this->assertTrue($cached);
$this->assertEqual($result, $expected);

*/
}

/**
Expand Down

0 comments on commit dfefc2d

Please sign in to comment.