Simple analysis on the use of YII2 cache, analysis of YII2 cache using _php Tutorial

Source: Internet
Author: User

Analysis on the use of YII2 cache, analysis of YII2 cache usage


A good framework is definitely inseparable from the use of the cache, on the contrary, a non-cached framework is certainly not a good frame, it seems to be a meaning, regardless, we first look at how to use the cache in Yii2.

It's time for us to do the first step, let's configure the components first.

For convenience, our cache components are configured in the common\config\main.php file, which comes first with a simple configuration under the file cache

' Components ' = [' cache ' = [' class ' = ' Yii\caching\filecache ', ' cachepath ' = ' @runtime/cache2 ',],],

The so-called file cache, in fact, is to store the data we want to cache into a file, where is the data cached?

The default cache path is @app\runtime\cache directory, if you want to modify the cache path, you can configure the same as above, configure the next CachePath can be

Let's just take a look.

$cache = Yii:: $app->cache; $data = $cache->get (' Cache_data_key '); if ($data = = = False) {//Here we can manipulate the database to get the data and then cache it through the $cache->set method $cacheData = ... the first parameter of the//set method is the key value of our data, Convenient for us to get to//The second parameter is that we want to cache the data//The third parameter is the cache time, if it is 0, means the permanent cache. The default is 0 $cache->set (' Cache_data_key ', $cacheData, 60*60); } var_dump ($data);

The above is a small section to introduce the use of YII2 cache, we can refer to the following.

Here is a description of how Yii sets cache cache

First add the configuration file in the components array:

Copy the Code code as follows:
' Cache ' =>array (' class ' = ' CFileCache '),

Set cache:

Yii::app ()->cache->set (' Testcache ', Array (1,3,4,6));//The default validity is one year Yii::app ()->cache->set (' Testcache ', Array (1,3,4,6), 3600);//One clock, second unit

Get cache:

$data = Yii::app ()->cache->get (' Testcache ');

To delete a single cache:

Yii::app ()->cache->delete (' Testcache ');

Clear all caches:

Yii::app ()->cache->flush ();

http://www.bkjia.com/PHPjc/1125890.html www.bkjia.com true http://www.bkjia.com/PHPjc/1125890.html techarticle analysis on the use of YII2 cache, analysis of YII2 cache using a good framework is definitely inseparable from the use of caching, instead, a framework without caching is certainly not a good framework, as if ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.