E
E
Evgeny Musonov2020-01-30 10:30:58
Yii
Evgeny Musonov, 2020-01-30 10:30:58

Why is query caching not working?

Can you please tell me why caching does not work, what is required besides specifying the component in web.php?

$productsForFilter = Product::getDb()->cache(function ($db) use ($productQuery) {
            return $productQuery->select('product.id')->asArray()->all();
        });

I understand that if the data is retrieved from the cache, then the memory is used much less than with the first non-cached request?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2020-01-30
@evmusonov

one.

// Кэширование запросов для DAO
// Возвращает ассоциативный массив с именами столбцов и значений
$categories = Yii::$app->db->cache(function () {
  return Yii::$app->db->createCommand('SELECT * FROM `category`')->queryAll();
});
 
 
// Кэширование запросов для ActiveRecord (на 1 час)
// Возвращает объект
$categories = Category::getDb()->cache(function (){
    return Category::find()->all();
}, 3600);

2. Caching is done when necessary, and not for the sake of caching. Are you having loading problems? Why cache?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question