In the face of some data that does not change too much in a short period of time and requires time-consuming calculations, cache is often used
[DataCache]
In Configuration under the components item in the configuration file main.php
'cache' => [ 'class' => 'yii\caching'FileCache', ],
\Yii::$app->cache->set('test',$data);//Save, the type can be string, array Wait
\Yii::$app->cache->get('test');//Get, if there is no such value, return bool(false), if so, return the value Value
The cache file will find a folder called te (the first two letters of the key name test) in the runtime\cache directory, where test.bin is the cache file of the data.
In addition to set and get, there are several methods:
If you want to change the caching method to DbCache, modify the components item in main.php The following configuration is:
'cache' => [ 'class' => 'yii\caching\DbCache', 'db' => 'mydb', 'cacheTable' => 'my_cache', ],
PHP Chinese website, there are a large number of free Yii introductory tutorials, everyone is welcome to learn!
The above is the detailed content of What cache is used for yii2 user login?. For more information, please follow other related articles on the PHP Chinese website!