How to flush db query cache in yii2? -
how handle when 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; }
you should use \yii\caching\dbdependency
, e.g. :
public static function getairportwithcache($iata_code){ // example if airports count change, update cache $dependency = new \yii\caching\dbdependency(['sql' => 'select count(*) ' . 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...
Comments
Post a Comment