T
T
tigra2017-02-22 15:38:03
Yii
tigra, 2017-02-22 15:38:03

How to cache a request by timer?

I have a costly new Query() query that counts a large amount of data that changes very quickly, therefore, when the query is called, it takes a long time to count, I would like it to remember it at the time of the response and be in the cache for 3 minutes then work again. How can this be implemented, can anyone give examples?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
tigra, 2017-03-07
@tigroid3

$query = Model::find()->where(['field' => 5, 'field2' => 777]);

$other = Yii::$app->db->cache(function ($db) use ($query) {
    /** @var Connection $db */
    return $db->createCommand($query->createCommand()->rawSql)->queryOne();
}, 120);

at the end time in seconds, how long to cache the request

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question