Zend缓存前端配置 [英] Zend cache frontend configuration

查看:52
本文介绍了Zend缓存前端配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.

鉴于此控制器和相关视图,您将如何进行缓存? href = https://stackoverflow.com/questions/6263696/zend-how-to-use-cache-component>此处(请参阅@marcin),我了解清除整个缓存仅需一个注释否则更新后的内容将太多。

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:

-index.phtml:

echo $this->partialLoop('_posts.phtml',$this->posts);

-_ posts.phtml:

-_posts.phtml:

echo $this->object->title;
echo $this->object->text;
echo $this->partialLoop('_comments.phtml',$this->object->getComments());

-_ comments.phtml:

-_comments.phtml:

echo $this->object->text;

请发布实际示例

再次感谢

推荐答案

对不起,我之前没有玩过。很快,我想提出一种捕获此问题的方法,供您考虑。为简单起见,我将仅专注于使用输出前端缓存输出。

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.

在您的application.ini文件中,您可以按以下方式设置捕获:

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

注意,我使用Apc作为后端。如果您没有或不想使用Apc,则可以使用文件后端。

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

有了这个,我将分别缓存您的帖子和评论。例如,在 _posts.phtml 中,您可以执行以下操作:

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

在此示例中,viewCache()视图帮助器如下:

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

我设置了 outputCache 进入Bootstrap.php中的注册表:

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

请注意,缓存与键相关联,而键又与给定帖子相关,并且它的评论。这样,当您收到新评论时,您只需重置与给定帖子相关的缓存。例如,在一个操作中,您可以删除具有$ post_id = 5的帖子的评论缓存,如下所示:

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.

这篇关于Zend缓存前端配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆