yii 清除mysql缓存_Yii2开启数据表结构缓存和清除
Yii2開啟表結構緩存,因為當運用模型(model)時,AR的一些公共屬性都會從DB中獲取,這樣會導致服務器負擔一些額外的資源開銷,實際上對于成品來說,服務器這些開始銷是多余的,故應該阻止這種默認行為,把表結構進行緩存起來,提高效率.Yii2的緩存值得深入研究學習.
開啟數據庫表結構的schema緩存的方法:
//配置文件的方式
'db'=>array(
...
'enableSchemaCache' => true,
'schemaCacheDuration' => 86400, // time in seconds
...
),
//區分環境--代碼基類里面實現
$dsn = "mysql:host=" . $config['host'] . ":" . $config['port'] . ";dbname=" . $config['name'];
$connection = new Connection([
'dsn' => $dsn,
'username' => $config['user'],
'password' => $config['password']
]);
$connection->charset = "utf8mb4";
if(YII_ENV == 'prod'){ //正式環境才開啟
$connection->enableSchemaCache = true;
}
//........
return $connection;
當開啟了數據庫的表結構緩存之后,需要改動或執行一些改變表結構的sql語句的時候,就會出現表結構被緩存了無法立即修復BUG或故障。這個時候就需要刷新或者清除數據庫表結構的緩存信息。
//方法一:清空表結構緩存的方法
//flush all the schema cache
Yii::$app->db->schema->refresh();
//clear the particular table schema cache
Yii::$app->db->schema->refreshTableSchema($tableName);
//方法二:清空所有的緩存--不僅僅是mysql表結構
Yii::$app->cache->flush();
//方法三:使用 yii命令行的方式commond清除緩存
cache/flush Flushes given cache components.
cache/flush-all Flushes all caches registered in the system.
cache/flush-schema Clears DB schema cache for a given connection component.
cache/index (default) Lists the caches that can be flushed.
//執行
./yii cache/flush-all
Yii::$app->cache->flush();
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的yii 清除mysql缓存_Yii2开启数据表结构缓存和清除的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python什么时候用框架_python
- 下一篇: docker配置容器mysql启动参数_