加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

【转载】yiimysql缓存_yii2刷新缓存

(2022-04-25 15:13:59)
分类: 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();

原文链接:https://blog.csdn.net/weixin_32123259/article/details/113632349


转载地址:https://blog.csdn.net/weixin_32123259/article/details/113632349

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有