Reputation: 1264
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;
}
Upvotes: 8
Views: 18397
Reputation: 161
For global cache, can use :
Yii::$app->cache->flush();
for spesific you can use TagDependency :
//way to use
return Yii::$app->db->cache(function(){
$query = new Query();
return $query->all();
}, 0, new TagDependency(['tags'=>'cache_table_1']));
//way to flush when modify table
TagDependency::invalidate(Yii::$app->cache, 'cache_table_1');
see the documentation http://www.yiiframework.com/doc-2.0/yii-caching-tagdependency.html
Upvotes: 14
Reputation: 3450
Just execute Yii::$app->cache->flush();
anywhere (probably in your controller)
PS: It will delete entire cache stored in your server
Upvotes: 2
Reputation: 25312
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...
Upvotes: 4