2011-06-09 84 views
1
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)我明白,清除整個緩存只是一個單一的評論或更新會太多。我應該如何去分別緩存它們?Zend的緩存前端配置

基本上我有一個博客頁面,我用相對用戶評論加載所有的帖子。

- 索引控制器(家庭頁):

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:

- _comments.phtml:

echo $this->object->text; 

請張貼實際的例子

再次感謝

回答

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,你可以使用文件後端。

有了這個,我會緩存您的文章和評論分開。例如,在_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()); 
} 

在這個例子中,視圖緩衝()視圖助手如下:

class My_View_Helper_ViewCache extends Zend_View_Helper_Abstract { 

    /** 
    * 
    * @return Zend_Cache_Frontend_Output 
    */ 
    public function viewCache() { 
     return Zend_Registry::get('outputCache'); 
    }  
} 

而我設置outputCache到註冊表中的自舉.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); 

希望這會幫助你,或者至少給你一些想法如何做到這一點。

+0

感謝您的解決方案永遠是優秀的! ;) – luca 2011-06-09 07:55:45

+0

我還有一些未解決的zend問題..如果你想!他們在這裏稱他們爲'簡單的錢'=)http://stackoverflow.com/questions/6166699/zend-wrap-formerrors-with-user-defined-html-tags – luca 2011-06-09 08:02:18

0

我不`噸知道該怎麼做的更好,但你可以這樣做:

plugin,那將是高速緩存響應的控制器使用postDispatch,如果它在高速緩存中使用,則恢復它preDispatch