Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 486
Pablo Palacios Avatar asked Dec 23 '22 09:12

Pablo Palacios


1 Answers

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.

like image 143
Pablo Palacios Avatar answered Jan 06 '23 20:01

Pablo Palacios