如何使用 Zend Cache 解决这个特定问题

发布于 2024-09-03 16:24:42 字数 694 浏览 2 评论 0原文

我有一个操作可以根据用户是否登录来呈现两个不同的视图脚本。

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;
            }   
    }   
    ....    
} 

我已经看到了一些关于如何使用 Zend Cache 的有用示例,它们似乎基于该操作呈现一个特定脚本这一事实。

我真正关注的是缓存 indexpublic 脚本的最佳方法,该脚本获得了相当多的点击,并且如果可能的话,我真的希望避免 Zend MVC 开销。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

李白 2024-09-10 16:24:42

Zend_Cache_Frontend_Output 可能是什么你需要在这里:

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

在此之前,需要初始化缓存管理器(可能在引导程序中),例如:

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

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

// getting a Zend_Cache_Frontend_Output object
$cache = Zend_Cache::factory('Output',
                             'File',
                             $frontendOptions,
                             $backendOptions);

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);
命硬 2024-09-10 16:24:42

您不太可能在这里以任何有意义的方式“避免 MVC 开销”,因为 MVC 框架正是 Zend_Cache 所在的上下文。一旦您进入控制器操作,您就已经使用了一堆资源路由和设置。

也就是说,如果 indexpublic.phtml 内部有昂贵的内容,您可能会考虑在模板内使用 Zend_Cache_Frontend_Output 来缓存一堆内容。如果 indexpublic 启动昂贵的操作(例如数据库命中),这可能是值得的。如果只是纯 PHP 生成标记,那么您不太可能看到太多改进。

在做任何事情之前,我建议您仔细研究您的应用程序行为,并确保您在正确的位置进行优化,而不是过早地进行优化。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文