Answer the question
In order to leave comments, you need to log in
How to cache data in Yii2?
I wrote my own class with the derivation of certain data from the table.
How can they be cached and displayed with a key now in the same class?
I read the documentation, I did not understand anything.
It seems that you need to configure the cache component and set the yii\caching\DbCache class.
In the end, I got completely confused.
Answer the question
In order to leave comments, you need to log in
Have you created a table?
CREATE TABLE cache (
id char(128) NOT NULL PRIMARY KEY,
expire int(11),
data BLOB
);
You can use any kind of cache (File, DB, Memcached, Redis...). The file cache is used by default, for the DB cache you need to create a table in the database.
Example cache in view:
<?php if ($this->beginCache('_header')): ?>
<header></header>
<?php
$this->endCache();
endif;
?>
$response = \Yii::$app->cache->getOrSet('categoriesFilterHierarchy', function () { //ищу в кэше переменную с ключом 'categoriesFilterHierarchy', если не нахожу, то обращаюсь в БД и кеширую на 600 секунд
$categories = self::find()->orderBy('place ASC')->all();
return $categories;
},600);
I wrote my own class with the derivation of certain data from the table
'cache' => [
'class' => 'yii\caching\DbCache',
// 'db' => 'mydb',
// 'cacheTable' => 'my_cache',
]
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question