W
W
websiller2018-08-03 14:10:44
Yii
websiller, 2018-08-03 14:10:44

Is it correct to cache the result from the database in a static property of the model class?

There is a Users model:

class Users extends CActiveRecord
{
  public static $userInfo = null;
...

This model has a method that retrieves information about the current user from the database and saves it in self::$userInfo so that the database is not pulled the next time the method is called. The question is - is it correct to store this information in the model class or is there another more correct way?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mysterion, 2018-08-03
@Mysterion

If your method that retrieves data from the cache completes earlier than the request from the database, then everything is fine.
But I would rather make a separate method that checks for the presence of data in the variable and, if there is none, then we make a request and enter the data into the variable and return it upon request in the same method. If there is data, then we get it from the cache and return it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question