C
C
cyberlog2020-02-23 22:52:05
Yii
cyberlog, 2020-02-23 22:52:05

Does yii2 cache queries to the database by default?

The documentation says that the enableQueryCache flag is set to true by default;
And this is actually true, in the Connection class it is set to true;
Does this mean that now all requests generated using ActiveRecord are cached?
Or is this flag not enough and do you need to insert special commands into queries?
Those. if let's do it

$user = Profile::find()->where(["id" => $id])->asArray()->one();


will such a request be cached?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2020-02-23
@cyberlog

Requests are cached if you explicitly call the method ->cache()in the request.

$user = Profile::find()->where(["id" => $id])->cache(1000)->asArray()->one();
In other cases, requests are not cached. Sometimes, in the settings, schema caching is set:
...
        'db' => [
            'class' => 'yii\db\Connection',
            'dsn' => 'mysql:host=localhost;dbname=****',
            'username' => '****', 
            'password' => '****',
            'charset' => 'utf8',
            'enableSchemaCache' => false, //  Если не нужно кэшировать вместо `true` поставить `false`
            'schemaCacheDuration' => 3600,
            'schemaCache' => 'cache',
        ],
...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question