Yii2 cache introduction and use. Yii2 cache introduction and use, yii2 cache introduction author: White Wolf Source: www.manks.topyii2_cache.html this article copyright belongs to the author, you are welcome to reprint, but without the author's consent must be guaranteed yii2 cache introduction and use, yii2 cache introduction
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 paragraph of the statement, and in the article page clearly given the original connection, otherwise, you are entitled to pursue legal liability.
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
Author: White Wolf Source: http://www.manks.top/yii2_cache.html文, welcome to the download, but the author does not agree to the copyright protection...