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.