开发者

How do I use Zend Cache on this particular problem

开发者 https://www.devze.com 2023-01-01 08:08 出处:网络
I have an action that renders two different view scripts based on whether the user is logged in or not.

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('indexregi开发者_开发技巧stered');
                    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.


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);


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消