My cache component is configured in the config\main.php file as follows:
'components' => [
'cache' => [
'class' => 'yii\caching\FileCache',
'cachePath' => '@runtime/cache2',
],
],
The so-called file cache is actually storing the data we want to cache in the file.
File cache path:
The default cache path is in the @app\runtime\cache directory. If you want to modify the cache path, you can configure the cachePath like the above configuration.
$cache = Yii:: $app -> cache;
$data = $cache ->get('cache_data_key' );
if ( $data === false ) {
// Here we can manipulate the database to get data, and then pass $cache ->set method to cache
$cacheData = ......
// The first parameter of the set method is the key value corresponding to our data, which is convenient for us to obtain
//The second parameter is the data we want to cache
//The third parameter is the cache time, if it is 0, it means permanent cache. Default is 0
$cache ->set('cache_data_key', $cacheData , 60*60 );
}
var_dump ( $ data );
Reference from: http://www.manks.top/yii2_cache.html