如果在特定表记录中发生某些更改,如何处理它?</ p>
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;
}
答案 0 :(得分:8)
对于全局缓存,可以使用:
Yii::$app->cache->flush();
for spesific你可以使用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');
请参阅文档http://www.yiiframework.com/doc-2.0/yii-caching-tagdependency.html
答案 1 :(得分:3)
您应该只使用\yii\caching\DbDependency
,例如:
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 ...
答案 2 :(得分:2)
只需在任何地方(可能在您的控制器中)执行Yii::$app->cache->flush();
PS:它将删除存储在服务器中的整个缓存