Zend_Cache can be configured to cache several types of output, including
the results of function calls, the results of object and static method calls,
entire pages, and configuration data.
鉴于这个控制器和相关的视图你会如何使用缓存?从你们的一些人的建议here(参见@marcin)我明白,清除整个缓存只需一个评论或更新后太过分了。我该如何单独缓存呢?
基本上我有一个博客页面,我正在用相关用户评论加载所有帖子。
-Index controller(主页):
class IndexController extends Zend_Controller_Action
{
public function indexAction()
{
//Post is a db_table model
$post_manager=new Post();
$allPosts=$post_manager->getAllPosts();
$this->view->posts=$allPosts;
}
}
-index.phtml:
echo $this->partialLoop('_posts.phtml',$this->posts);
-_ posts.phtml:
echo $this->object->title;
echo $this->object->text;
echo $this->partialLoop('_comments.phtml',$this->object->getComments());
-_ comments.phtml:
echo $this->object->text;
请发布实例
再次感谢
答案 0 :(得分:4)
抱歉,我之前没有重播过。很快,我想提出一种方法来捕捉这个以供你考虑。为简单起见,我将专注于使用输出前端缓存输出。
在您的application.ini中,您可以按如下方式设置捕获:
resources.cachemanager.myviewcache.frontend.name = Output
resources.cachemanager.myviewcache.frontend.customFrontendNaming = false
resources.cachemanager.myviewcache.frontend.options.lifetime = 7200
resources.cachemanager.myviewcache.frontend.options.caching = true
resources.cachemanager.myviewcache.frontend.options.automatic_serialization = true
resources.cachemanager.myviewcache.backend.name = Apc
注意,我使用Apc作为后端。如果您没有或不想要Apc,您可以使用文件后端。
有了这个,我会分别缓存你的帖子和评论。例如,在_posts.phtml
中,您可以执行与以下内容类似的操作:
// first cache an output related to the body of your post with key being associated
// with your post.
if (!($this->viewCache()->start('post_' . (string) $this->object->post_id))) {
echo $this->object->title;
echo $this->object->text;
}
// now I cache an output of a comments associated with a give post
if (!($this->viewCache()->start('post_comments_' . (string) $this->object->post_id))) {
echo $this->partialLoop('_comments.phtml',$this->object->getComments());
}
在此示例中,viewCache()视图助手如下:
class My_View_Helper_ViewCache extends Zend_View_Helper_Abstract {
/**
*
* @return Zend_Cache_Frontend_Output
*/
public function viewCache() {
return Zend_Registry::get('outputCache');
}
}
我将outputCache
设置为Bootstrap.php中的注册表:
protected function _initPutChachesIntoRegistry() {
$this->bootstrap('cachemanager');
$cacheManager = $this->getResource('cachemanager');
Zend_Registry::set('outputCache', $cacheManager->getCache('myviewcache'));
}
请注意,缓存与键相关联,而键又与给定帖子及其注释相关。有了这个,当您收到新评论时,您只需重置与给定帖子相关的缓存。例如,在操作中,您可以删除$ post_id = 5的帖子的注释缓存,如下所示:
$this->view->viewCache()->remove('post_comments_' . $post_id);
希望这对您有所帮助,或至少为您提供一些如何制作的建议。
答案 1 :(得分:0)
我不知道如何做得更好,但你可以这样做:
写plugin,这将是使用postDispatch
的控制器的缓存响应,如果它在缓存中使用preDispatch