I’m using https://github.com/dektrium/yii2-user module to manage the users. Im many places on the code I call things like:
Yii::$app->user->id
Yii::$app->user->identity->profile->name
Is possible to cache this information?
I’m using https://github.com/dektrium/yii2-user module to manage the users. Im many places on the code I call things like:
Yii::$app->user->id
Yii::$app->user->identity->profile->name
Is possible to cache this information?
[color="#006400"]/* Moved from "General" to "Extensions" */[/color]
I think you should not try to do it.
But there are any reason to not cache a object that don’t change frequently and that is used in a lot of locals?
Yii::$app->user->identity->profile->name;
Here we have 4 objects: the application, the user component, the identity class object, and the profile object that is a relational object of the identity object.
The first 2 objects are not designed to be cached. When you want to cache user specific data, you have to get the id of the user itself beforehand. So you can’t cache the user component.
The identity class object will be loaded from the database when you first access it via “Yii::$app->user->identity”. And also the profile class object will be loaded from the database when you first access it via “identity->profile”. But once they are loaded, accessing them again won’t trigger database operations.
The identity object and the profile object can be cached. But I don’t think it’s worth trying, since you have to execute additional query in order to check the validity of the cache. Usually the cache works great for an array of objects that is fetched by some filter, but not for a single object that is simple and small.
Perfect after read your reply I agree with you. Thank you!