Yii2 cache introduction and use, yii2 cache introduction _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
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...

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.