3

I have this in my frontend/SiteController.php

public function actionPage($slug = 'home') {
    Url::remember();
    $val = Yii::$app->cache->getOrSet($slug, function() use ($slug) {
        $model = DBMenu::find()->with('pagesTranslations')->where(['slug' => $slug])->one();
        if ($model === null) {
            return $this->redirect(['/site/page', 'slug' => 'home']);
        }
        return $model;
    }, 0, new TagDependency(['tags' => 'page']));

    $searchModel = new Search();
    return $this->render('page',[
        'model' => $val,
        'searchModel' => $searchModel
    ]);
}

And this piece of code in backend/PageController.php

public function actionTest() {
    TagDependency::invalidate(Yii::$app->cache, 'page');
}

And after I go to this 'test' action and after that go to site/page?slug=home I see in log that DB count is 50 and it should be 68. Why invalidate method doesn't work?

EDIT: In common/config/main.php cache is set like this:

'cache' => [
    'class' => 'yii\caching\FileCache',
],
Marko Mikulić
  • 1,047
  • 2
  • 10
  • 15
  • Does it work with classic cache setting? I mean like `$val = Yii::$app->cache->get(...); if ($val === false) { $val = ...; Yii::$app->cache->set(...); }`. Why do you redirect inside the closure? – Bizley Feb 02 '17 at 12:46
  • It doesn't work even that way @Bizley – Marko Mikulić Feb 02 '17 at 12:54
  • You need to check if the cache has been saved at all. It's not a good idea to cache whole model btw. – Bizley Feb 02 '17 at 12:59
  • I think the problem is that frontend doesn't share cache with backend. Is there any way to configure that? I can't find the solution in the documentation.. – Marko Mikulić Feb 02 '17 at 13:07
  • 1
    Configure cache with `cachePath`. As default it's `'cachePath' => '@runtime/cache'`. You can set i.e. `@frontend/runtime/cache` to store all cache in frontend folder. – Bizley Feb 02 '17 at 13:19

1 Answers1

0

I like to answer this old post in case someone has the same problem and no solution.
This problem happens because of a bug in Yii2, which not seams to fixed in the last 5 years.

https://github.com/yiisoft/yii2/issues/15350
https://github.com/yiisoft/yii2-redis/issues/156

The Bndr
  • 13,204
  • 16
  • 68
  • 107
  • This question (and https://github.com/yiisoft/yii2/issues/15350) is probably related to `FileCache` and different cache locations for web and console apps - this is more like configuration error than framework bug. https://github.com/yiisoft/yii2-redis/issues/156 sounds like a known bug for `exists()`, but it should work fine for `get()` - if you have problem with `get()`, having some reproducible example may help to fix your issue. – rob006 Aug 29 '23 at 18:06