The usage of Yii2 cache and Yii2 cache are analyzed. Analysis of Yii2 cache usage, analysis of Yii2 cache using a good framework is certainly inseparable from the use of cache, on the contrary, a framework without cache is certainly not a good framework, it seems to be an analysis of Yii2 cache usage, and analysis of Yii2 cache usage
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 );
The above content is the Yii2 cache usage method introduced by Xiaobian. you can refer to it.
The following describes how to set Cache for Yii.
Add the following in the components array:
The code is as follows:
'Cache' => array ('class' => 'cfilecache '),
Set Cache:
Yii: app ()-> cache-> set ('testcache', array (,); // The default validity period is one year. Yii: app () -> cache-> set ('testcache', array (3600,),); // one minute, in seconds
Get Cache:
$data = Yii::app()->cache->get('testcache');
Delete a single Cache:
Yii::app()->cache->delete('testcache');
Clear all Cache:
Yii::app()->cache->flush();
A good framework for caching is certainly inseparable from the use of caching. On the contrary, a framework without caching is certainly not a good framework, as if it was...