【问题标题】:CakePHP 2.4: Updating the cache fails with insert, but not on updateCakePHP 2.4:插入时更新缓存失败,但更新时没有
【发布时间】: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


【解决方案1】:

于是我找到了问题的原因:

是的,回调被调用。 是的,缓存已写入。

但是: 连接模型的数据写入之前发生的所有事情,因此这些模型的信息无法通过afterSave保存在缓存中。

我会将缓存的写入移出模型并在所有其他数据保存到数据库后从控制器调用它。这应该可以解决问题。

灾难简

【讨论】:

    猜你喜欢
    • 2011-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-05
    • 1970-01-01
    • 2015-01-28
    • 1970-01-01
    相关资源
    最近更新 更多