Detailed description of fragment caching in yiiframework

Source: Internet
Author: User
Detailed description of fragment caching in yiiframework

1. modify the main/config configuration file and add it to the cache component.

123 'Cache' => array ('class' => 'system. caching. cfilecache '),

2. define the filter in the controller to be cached. for example, I want to define SiteController

123456789 Publicfunctionfilters () {returnarray (array ('coutputcache + index, category, content', 'duration' => 3600, 'varybyparam' => array ('id ', 'page '),));}

COutputCache-customizable parameters

COutputCache is a class used to process cache. if you only specify 'coutputcache', all actions in the controller will be filtered by cache, which is defined as 'coutputcache + index, category, content ', indicates that only actionIndex, actionCategory, and actionContent are cached.

Duration is the cache time, measured in seconds. in the previous example, 3600 is 1 hour.

VaryByParam specifies a list of GET parameter names and uses corresponding values to determine the cached content version. that is, the same action is used to differentiate parameters on different pages, here, I use id and page to differentiate different pages. if I delete the page parameter and write it as 'varybyparam' => array ('id'), the following two pages use the same cache, leading to page turning failure:

Index. php? R = site/index & id = 2 & page = 1

Index. php? R = site/index & id = 2 & page = 2

In addition to varyByParam, you can use other conditions to differentiate pages:

VaryByExpression: specifies whether the cached content varies according to the results of the specified PHP expression.

VaryByRoute: specify that the cached content varies based on the requested route (controller and action)

VaryBySession: specifies whether the content is cached. changes due to different user sessions.

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.