yii2 刷新缓存(刷新模型缓存)

痛定思痛。 2023-01-10 01:29 354阅读 0赞

Yii2开启表结构缓存,因为当运用模型(model)时,AR的一些公共属性都会从DB中获取,这样会导致服务器负担一些额外的资源开销,实际上对于成品来说,服务器这些开始销是多余的,故应该阻止这种默认行为,把表结构进行缓存起来,提高效率.Yii2的缓存值得深入研究学习.

开启数据库表结构的schema缓存的方法:








1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

//配置文件的方式 

‘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或故障。这个时候就需要刷新或者清除数据库表结构的缓存信息。








1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

//方法一:清空表结构缓存的方法 

    

//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();

发表评论

表情:
评论列表 (有 0 条评论,354人围观)

还没有评论,来说两句吧...

相关阅读

    相关 MacOS13刷新DNS缓存

    当你在浏览网站或者使用网络服务时,你的 MacOS 系统会自动保存(缓存)DNS 查询结果,以加速后续的访问。但是,有时候,DNS 缓存可能会造成问题,例如网站已经迁移到新的服