Zend cache frontend configuration

安稳与你 提交于 2020-01-23 23:27:04

问题


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.

Given this controller and related views how would you go with caching?From what some of you suggested here (see @marcin) I understood that clearing the whole cache for just a single comment or a post-update would be too much.How should I go for them to be cached separately?

Basically I have a blog page where I'm loading all of the posts with relative users comments.

-Index controller (home-page):

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;

Please post practical examples

thanks again


回答1:


Sorry I did not replay earlier. Quickly, I'll would like to present one way of catching this for your consideration. For simplicity, I'll just concentrate on caching your outputs using Output frontend.

In you application.ini you can setup your catching as follows:

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

Note, that I use Apc as a backend. You may use file backend if you don't have or don't want Apc.

With this, I would cache your posts and comments separately. For example, in _posts.phtml you could do something similar to the following:

// 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());
}

In this example, viewCache() view helper is as follows:

class My_View_Helper_ViewCache extends Zend_View_Helper_Abstract {

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

Whereas I set outputCache into registry in the Bootstrap.php:

protected function _initPutChachesIntoRegistry() {
    $this->bootstrap('cachemanager');
    $cacheManager = $this->getResource('cachemanager');

    Zend_Registry::set('outputCache', $cacheManager->getCache('myviewcache'));
}

Notice that caches are associated with keys which in turn relate to a given post and its comments. With this, when you get a new comment, you just reset a cache related to the given post. For example, in an action, you can remove comment cache for a post with $post_id=5 as follows:

$this->view->viewCache()->remove('post_comments_' . $post_id);

Hope that this will help you or at least give you some ideas how to make it.




回答2:


I don`t know how to do better, but your can do like this:

Write plugin, that will be cache response of controller using postDispatch, and restore it if it in cache using preDispatch



来源:https://stackoverflow.com/questions/6289291/zend-cache-frontend-configuration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!