Home Ask Login Register

Developers Planet

Your answer is one click away!

Yatin Mistry February 2016

How to flush db query cache in yii2?

How to handle it when some changes happen in specific table records ?

public static function getAirportWithCache($iata_code){

              $result = Airports::getDb()->cache(function ($db) use ($iata_code){
                     $res = Airports::find()
                               ->where(['iata_code' => $iata_code])
                               ->limit(1)
                               ->asArray()
                               ->one();
                     return $res;
              });
              return $result;
        }

Answers


SohelAhmedM February 2016

Just execute Yii::$app->cache->flush(); anywhere (probably in your controller)

PS: It will delete entire cache stored in your server


soju February 2016

You should simply use \yii\caching\DbDependency, e.g. :

public static function getAirportWithCache($iata_code){
    // for example if airports count change, this will update your cache
    $dependency = new \yii\caching\DbDependency(['sql' => 'SELECT COUNT(*) FROM ' . Airports::tableName()]);
    $result = Airports::getDb()->cache(function ($db) use ($iata_code){
        return Airports::find()
            ->where(['iata_code' => $iata_code])
            ->limit(1)
            ->asArray()
            ->one();
    }, 0, $dependency);
    return $result;
}

Read more...

Post Status

Asked in February 2016
Viewed 1,489 times
Voted 11
Answered 2 times

Search




Leave an answer


Quote of the day: live life