views:

119

answers:

2

I have an action that renders two different view scripts based on whether the user is logged in or not.

class IndexController extends Zend_Controller_Action
{
    ....
        public function indexAction()
            {

                $auth = Zend_Auth::getInstance();
                if($auth->hasIdentity())
                {
                    $this->render('indexregistered');
                    return; 
                }
                else {
                    $this->render('indexpublic');
                    return;
            }   
    }   
    ....    
} 

I have seen quite some useful examples on how to use the Zend Cache and they seem to be based on the fact that the action renders one particular script.

What am really looking at is the best approach to cache the indexpublic script which gets quite some hits and I would really like to avoid the Zend MVC overhead if possible.

+1  A: 

Zend_Cache_Frontend_Output may be what you need here:

if (!($cache->start('indexpublic'))) {
    // output everything as usual
    $this->render('indexpublic');
    $cache->end(); // output buffering ends   
}

Before that, the cache manager needs to be initialized (could be in the bootstrap), e.g:

$frontendOptions = array(
   'lifetime' => 7200
);

$backendOptions = array(
    'cache_dir' => '/tmp/'
);

// getting a Zend_Cache_Frontend_Output object
$cache = Zend_Cache::factory('Output',
                             'File',
                             $frontendOptions,
                             $backendOptions);
nuqqsa
+1 for clear demonstration of Zend_Cache_Frontend_Output
timdev
A: 

You're not likely to "avoid the MVC overhead" in any meaningful way here, since that MVC framework is exactly the context which Zend_Cache lives in. Once you're inside a controller action, you've already used a bunch of resources for routing and setup.

That said, if expensive stuff goes on inside of indexpublic.phtml, you might look into using Zend_Cache_Frontend_Output inside your template to cache a bunch of stuff. If indexpublic kicks off expensive operations (like DB hits), this might be worthwhile. If it's just pure PHP that generates markup, you're unlikely to see much improvement.

Before doing anything, I'd suggest you study your application behavior very carefully and make sure you're optimizing in the right place, and not prematurely.

timdev