Skip to content

Commit dfefc2d

Browse files
committed
Very initial pass at getting elements cached with Cache. Refs #1268
1 parent a9a9bc0 commit dfefc2d

File tree

2 files changed

+33
-18
lines changed

2 files changed

+33
-18
lines changed

cake/libs/view/view.php

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -325,23 +325,22 @@ public function element($name, $params = array(), $callbacks = false) {
325325
}
326326

327327
if (isset($params['cache'])) {
328-
$expires = '+1 day';
329-
330328
if (is_array($params['cache'])) {
331-
$expires = $params['cache']['time'];
332-
$key = Inflector::slug($params['cache']['key']);
333-
} elseif ($params['cache'] !== true) {
334-
$expires = $params['cache'];
335-
$key = implode('_', array_keys($params));
329+
$defaults = array(
330+
'config' => 'default',
331+
'key' => '',
332+
);
333+
$caching = array_merge($defaults, $params['cache']);
334+
} else {
335+
$caching = array(
336+
'config' => 'default',
337+
'key' => implode('_', array_keys($params))
338+
);
336339
}
337-
338-
if ($expires) {
339-
$cacheFile = 'element_' . $key . '_' . $plugin . Inflector::slug($name);
340-
$cache = cache('views' . DS . $cacheFile, null, $expires);
341-
342-
if (is_string($cache)) {
343-
return $cache;
344-
}
340+
$key = 'element_' . $caching['key'] . '_' . $plugin . Inflector::slug($name);
341+
$contents = Cache::read($key, $caching['config']);
342+
if ($contents !== false) {
343+
return $contents;
345344
}
346345
}
347346
$paths = $this->_paths($plugin);
@@ -364,8 +363,8 @@ public function element($name, $params = array(), $callbacks = false) {
364363
if ($callbacks) {
365364
$this->Helpers->trigger('afterRender', array($file, $element));
366365
}
367-
if (isset($params['cache']) && isset($cacheFile) && isset($expires)) {
368-
cache('views' . DS . $cacheFile, $element, $expires);
366+
if (isset($params['cache'])) {
367+
Cache::write($key, $element, $caching['config']);
369368
}
370369
return $element;
371370
}

cake/tests/cases/libs/view/view.test.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,22 @@ function testElementCacheHelperNoCache() {
511511
* @return void
512512
*/
513513
function testElementCache() {
514+
Cache::drop('test_view');
515+
Cache::config('test_view', array(
516+
'engine' => 'File',
517+
'duration' => '+1 day',
518+
'path' => CACHE . 'views' . DS
519+
));
520+
521+
$View = new TestView($this->PostsController);
522+
$result = $View->element('test_element', array('cache' => array('config' => 'test_view')));
523+
$expected = 'this is the test element';
524+
$this->assertEquals($expected, $result);
525+
526+
$result = Cache::read('element__test_element', 'test_view');
527+
$this->assertEquals($expected, $result);
528+
529+
/*
514530
$writable = is_writable(CACHE . 'views' . DS);
515531
if ($this->skipIf(!$writable, 'CACHE/views dir is not writable, cannot test elementCache. %s')) {
516532
return;
@@ -553,7 +569,7 @@ function testElementCache() {
553569
}
554570
$this->assertTrue($cached);
555571
$this->assertEqual($result, $expected);
556-
572+
*/
557573
}
558574

559575
/**

0 commit comments

Comments
 (0)