I
I
Ilya Beloborodov2017-02-01 16:42:56
Yii
Ilya Beloborodov, 2017-02-01 16:42:56

How to cache yii\db\ActiveQuery query?

$query = Material::find()
            ->where(['in', 'id_material', $ids])
            ->andWhere('start_publish < :time')
            ->params([':time' => time()])
            ->orderBy(['start_publish' => SORT_DESC]);

$count = $query->count();

Google yuzal, everything that I tried - does not work. Well, or I'm curmudgeonly. Tell me, I will be grateful
___
The fact is that I have already tried this. The request is still taking a long time
$query = Material::find()
            ->where(['in', 'id_material', $ids])
            ->andWhere('start_publish < :time')
            ->params([':time' => time()])
            ->orderBy(['start_publish' => SORT_DESC]);

        $count = Material::getDb()->cache(function() use ($query) {
            return $query->count();
        });

Maybe even in the config you need to register?
__
with each page refresh, a binary file /runtime/cache/ee is added, which means it caches. but why does it cache with every request? And does not get the cache?
____
removed the time from the request, inserted the component into the config,
$query = Material::find()
            ->where(['in', 'id_material', $ids])
            ->orderBy(['start_publish' => SORT_DESC]);

        $count = Material::getDb()->cache(function($db) use ($query) {
            return $query->count();
        });

but anyway, with each page refresh, the binary file is created anew, and each request takes a long time

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Maxim Timofeev, 2017-02-01
@webinar

Request or received data, so as not to make a request all the time?
Everything is here:
www.yiiframework.com/doc-2.0/guide-caching-data.html
Alternatively:

$data = $cache->get($key);
if ($data === false) {
    $query = Material::find()
            ->where(['in', 'id_material', $ids])
            ->andWhere('start_publish < :time')
            ->params([':time' => time()])
            ->orderBy(['start_publish' => SORT_DESC])
->all();
    $cache->set($key, $data);
}
print_r($data);

If you need to make a dependency on count, then the same link has the Cache Dependencies topic
. You can also cache the request:
$data= Material::getDb()->cache(function ($db) {
    return Material::find()->where(['in', 'id_material', $ids])
            ->andWhere('start_publish < :time')
            ->params([':time' => time()])
            ->orderBy(['start_publish' => SORT_DESC])->all();
});

A
Abdula Magomedov, 2017-02-01
@Avarskiy

$result = Customer::getDb()->cache(function ($db) {
    return Customer::find()->where(['id' => 1])->one();
});

https://github.com/yiisoft/yii2/blob/master/docs/g...
Request caching section

A
Anton, 2017-02-01
@karminski

Taking into account the advice of Abdula Magomedov , replace $query with the following

$query = Material::find()
            ->where(['in', 'id_material', $ids])
            ->andWhere('start_publish < NOW()')
            ->orderBy(['start_publish' => SORT_DESC]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question