Reputation: 31
When ever I use
Yii::$app->user->id
or
Yii::$app->user->isGuest
Yii2 execute SELECT * FROM
userWHERE
id='4'
.
How can I prevent this repetitive query in order to optimize my web app?
Caching or defining global session variable and something like that.
Upvotes: 0
Views: 663
Reputation: 174
Enable one of the supported cache storages and replace the code 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
Upvotes: 0