Yii2 introduction and use of cache-White Wolf stack author: White Wolf Source: http://www.manks.top/yii2_cache.html this article copyright to the author, welcome to reprint, but without the author's consent must retain this statement, the original article connection is clearly provided on the article page, otherwise the right to pursue legal liability will be reserved.
A good framework is definitely inseparable from the use of cache. On the contrary, a framework without cache is certainly not a good framework. it seems to mean that no matter what it means, let's take a look at how the cache is used in yii2.
It's time for our first step. let's configure the components first.
For convenience, our cache component is configured in the common \ config \ main. php file. First, we can simply configure the file cache.
'components' => [
'cache' => [
'class' => 'yii\caching\FileCache',
'cachePath' => '@runtime/cache2',
],
],
The so-called file cache actually stores the data we want to cache in the file. where is the data cached?
// The default cache path is under the @ app \ runtime \ cache Directory. if you want to modify the cache path, configure the cachePath as configured above.
Let's take a look at the operation.
$ Cache = Yii: $ app-> cache; $ data = $ cache-> get ('cache _ data_key '); if ($ data = false) {// Here we can operate the database to obtain data, and then cache $ cacheData = ...... // The first parameter of the set method is the key value corresponding to our data, the second parameter is the data to be cached. The third parameter is the cache time. if the value is 0, the data is permanently cached. The default value is 0 $ cache-> set ('cache _ data_key ', $ cacheData, 60*60);} var_dump ($ data );
Continue reading