سؤال

In one of my models I use the file cache for remembering preprocessed data to speed up the view of lists.

In my callback afterSave() I call the method which should force to update or create the cache entry, but for some reasons it works only for update, but not for create.

Here is the code:

public function afterSave($created, $options = array())
{
    exec('sudo /root/tools/sync-home-wap-GKM.sh');
    $content = $this->findById($this->id);
    $this->enhanceContent($content, true);
}

The important call is enhanceContent($content, true) which generates the additional data or fetches it from cache. true initializes a forced update of the cache ( at least I hope so).

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;
}

Has anybody an idea why the data for the lists is not there after adding a new Content, but is there after editing the same Content and saving again?

Thank you for any hints!

Calamity Jane

هل كانت مفيدة؟

المحلول

So I found the cause of the problem:

Yes, the callback was called. Yes, the cache was written.

But: All that happened before the data of the connected models was written and therefore the information of these models cannot be saved in the cache by the afterSave.

I will move the writing of the cache out of the model and call it from the controller after all the other data is saved to the db. That should do the trick.

CalamityJane

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top