【问题标题】:CakePHP: how work the cache groups?CakePHP:缓存组如何工作?
【发布时间】:2014-09-20 15:11:05
【问题描述】:

我正在尝试使用缓存组,但 examples in the documentation 对我来说不是很清楚。

这是我的引导程序

Cache::config('default', array(
    'engine' => $engine,
    'duration' => $duration,
    'prefix' => $prefix,
    'groups' => array('page', 'photo', 'post')
));

我们假设我有模型,包括文章、页面、照片等,并在各种动作中将数据写入缓存。

例如,PagesController

public function index() {
    $pages = Cache::read($cache = 'pages_index');

    if(empty($pages)) {
        $pages = $this->Page->find('all');
        Cache::write($cache, $pages);
    }

    $this->set(array(
        'pages'             => $pages
    ));
}

这会创建文件

tmp/cache/pages_index

缓存工作正常,下一个请求将使用缓存。 其他操作为 Page 模型页面写入数据。

再次PagesController

public function view($slug = NULL) {
    $page = Cache::read($cache = sprintf('pages_view_%s', $slug));

    //If the data are not available from the cache
    if(empty($page)) {
        $page = $this->Page->find('first', array(
            'conditions'    => array('slug' => $slug)
        ));

        if(empty($page))
            throw new NotFoundException(__('Invalid page'));

        Cache::write($cache, $page);
    }

    $this->set(array(
        'page'              => $page
    ));
}

这也可以正常工作。

现在我希望编辑一个页面从缓存中删除所有关于页面的数据。其他模型(帖子、照片等)也应如此。

所以,在我的 Page 模型中:

public function afterSave($created, $options = array()) {
    Cache::clearGroup('pages');
}

但这不起作用:没有文件从缓存中删除。

我哪里做错了?我不明白的是? 谢谢!

【问题讨论】:

    标签: cakephp caching cakephp-2.3


    【解决方案1】:

    因为您的群组名称是“page”,而不是“pages”。

    【讨论】:

      猜你喜欢
      • 2011-04-25
      • 2016-02-15
      • 2014-10-06
      • 1970-01-01
      • 2012-09-01
      • 1970-01-01
      • 2016-09-08
      • 2015-03-03
      • 1970-01-01
      相关资源
      最近更新 更多