简体   繁体   English

如何防止yii2在调用Yii :: $ app-> user-> id时执行选择查询

[英]how prevent yii2 to execute select query when ever call Yii::$app->user->id

When ever I use 每当我使用

Yii::$app->user->id

or 要么

Yii::$app->user->isGuest 

Yii2 execute SELECT * FROM user WHERE id ='4' . Yii2执行SELECT * FROM user WHERE id ='4'

How can I prevent this repetitive query in order to optimize my web app? 我如何防止重复查询以优化我的Web应用程序?
Caching or defining global session variable and something like that. 缓存或定义全局会话变量等。

Enable one of the supported cache storages and replace the code return static::findOne(['id' => $id]); 启用一种受支持的缓存存储,并替换代码return static::findOne(['id' => $id]); you mentioned in your comment with 您在评论中提到的

$db = self::getDb();
return static::getDb()->cache(function ($db) use ($id) {
    return static::find()->where(['id' => $id])->one();
});

For more details, see http://www.yiiframework.com/doc-2.0/guide-caching-data.html 有关更多详细信息,请参见http://www.yiiframework.com/doc-2.0/guide-caching-data.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM