【发布时间】:2014-02-25 15:04:31
【问题描述】:
在我的一个模型中,我使用文件缓存来记住预处理数据以加快列表的查看速度。
在我的回调 afterSave() 中,我调用了应该强制更新或创建缓存条目的方法,但由于某些原因,它仅适用于更新,不适用于创建。
代码如下:
public function afterSave($created, $options = array())
{
exec('sudo /root/tools/sync-home-wap-GKM.sh');
$content = $this->findById($this->id);
$this->enhanceContent($content, true);
}
重要的调用是 enhanceContent($content, true) 生成附加数据或从缓存中获取数据。 true 初始化缓存的强制更新(至少我希望如此)。
private function enhanceContent($content, $bForce = false)
{
if (!isset($content['Content']['id']))
throw new NotFoundException(__('Invalid Content Array: '.$content));
if ($bForce)
Cache::delete('ContentPretty'.$content['Content']['id'], 'long');
$contentpretty = Cache::read('ContentPretty'.$content['Content']['id'], 'long');
if ($contentpretty === false) {
$content['Content']['preview'] = $this->getImageName(
(int)$content['Content']['objnbr'], 'small', false
);
.... Do a lot of other things not important
Cache::write('ContentPretty'.$content['Content']['id'], $content['Content'], 'long');
} else {
$content['Content'] = $contentpretty;
}
//debug($content);
return $content;
}
有没有人知道为什么添加新内容后列表的数据不存在,但在编辑相同的内容并再次保存后仍然存在?
感谢您的任何提示!
灾难简
【问题讨论】:
-
在某些情况下 afterSave 不会被调用。所以我会验证这实际上是在你创建新内容之后被调用的。
-
我确实在我的测试环境中的 afterSave 方法中插入了一个退出语句,并且在添加新内容时执行停止了。所以我猜这个方法确实被调用了。
标签: cakephp caching callback cakephp-2.3