CakePHP:缓存组如何工作?

问题描述 投票:1回答:1

我正在尝试使用缓存组,但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

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

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个回答
0
投票

因为您的群组名称是“页面”,而不是“页面”。

© www.soinside.com 2019 - 2024. All rights reserved.