考虑以下;

Cache::write('Model.key1' , 'stuff');
Cache::write('AnotherModel.key1' , 'stuff');
Cache::write('Model.key2' , 'stuff');


我可以从缓存中删除一组密钥吗?

例如,如果我想清除“模型”的所有缓存数据,但将“ AnotherModel”保留在缓存中,我想使用以下内容:

Cache::delete('Model.*');


可以在CakePHP 1.3.x中实现这种功能吗?

谢谢!

最佳答案

对于那些只是谷歌搜索这个问题的人(就像我以前一样),Cake 2.2现在支持这种功能(不必为每个“组”创建单独的缓存配置)。

尽管缺少一些细节,但这里有一些解释:
http://book.cakephp.org/2.0/en/core-libraries/caching.html#using-groups

但是,这就是我在应用程序中所做的,并且看起来运行良好。 ;-)

/app/Config/core.php

Cache::config('default', array(
    'engine' => $engine,
    ...
    'groups' => ['navigation'],
));


型号afterSave挂钩:

function afterSave($created) {
    // This deletes all keys beginning with 'navigation'
    Cache::clearGroup('navigation');
    parent::afterSave($created);
}


然后在需要昂贵查询的控制器/模型中...

// We create a unique key based on parameters passed in
$cacheKey = "navigation.$sitemapId.$levelsDeep.$rootPageId";
$nav = Cache::read($cacheKey);
if (!$nav) {
    $nav = $this->recursiveFind(
        'ChildPage',
        ['page_id' => $rootPageId],
        $levelsDeep
    );
    Cache::write($cacheKey, $nav);
}

10-07 19:23
查看更多