Pablo Palacios
Pablo Palacios

Reputation: 2957

How to flush frontend Cache from backend in Yii2

I am using YII2 Advanced, on the backend I needed an Action that invalidates the Cache in the frontend.

This is needed because I use yii2mod/yii2-settings, obiously, the settings are being cached on both ends. But I wasn't able to flush the cache from the backen with Yii::$app->cache->flush();, this will do it just in the backend.

Upvotes: 5

Views: 3412

Answers (1)

Pablo Palacios
Pablo Palacios

Reputation: 2957

So somehow I found that if I make a reference on the backend components, I end having access to flush on the backend.

On \backend\config\main.php

'components' => [
    //...
    'frontendCache' => [
        'class' => 'yii\caching\FileCache',
        'cachePath' => Yii::getAlias('@frontend') . '/runtime/cache'
    ],
]

Now in your controller

    Yii::$app->cache->flush(); //backend flush
    Yii::$app->frontendCache->flush(); //frontend flush

This took me a while to figure this out, so I hope this helps someone.

Upvotes: 13

Related Questions