2009年4月23日星期四

[fw-mvc] Caching Views

Hello list,

I have ZF MVC with two step view (views and layout). I cache the entire already rendered views before they are passed to layout.
The caching is easy, but I wonder how to replace the view with the cached data within the action controller.
Here is a testAction to illustrate what I mean:

<?php
class SomeController extends Zend_Controller_Action
{
public function testAction()
{
$frontendOptions = array(
'lifetime' => null,
'automatic_serialization' => true
);
$backendOptions = array(
'cache_dir' => ROOT_PATH . DIRECTORY_SEPARATOR
. 'cache' . DIRECTORY_SEPARATOR . 'actions'
);
$cache = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);
$cachedAction = $cache->load($this->_request->getActionName());

if (!$cachedAction) {
$this->view->foo = 'bar';
$cachedAction = $this->view->render($this->getViewScript());
$cache->save($cachedAction, $this->_request->getActionName());
} else {
echo 'from cache:<br />';
/*
* Here the view script must not be executed, but the view must be
* replaced by $cachedAction. It should be placed in layout as
* $this->layout()->content
*/
}
}
}

The view script simply looks like:
<p>This is foo: <?= $this->foo ?></p>

Any suggestions?
best regards
Viktor

没有评论: