Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Page caching in Yii is very easy

Friday, July 20th, 2012 by Servage

Cahing is one of the best tools to improve performance and thereby create faster load times and an increased user experience for your users. There are many standardized cahing mechanisms you can implement, but Yii actually gives it’d developers a high level implemntation, so you don’t even have to worry about the cahcing system itself. You can simply apply cahing to pages, or wherever you need it.  So check out the example on page cahing from The Definite Guide to Yii here:

Page caching can be considered as a special case of fragment caching. Because the content of a page is often generated by applying a layout to a view, it will not work if we simply call beginCache() and endCache() in the layout. The reason is because the layout is applied within the CController::render() method AFTER the content view is evaluated.

To cache a whole page, we should skip the execution of the action generating the page content. We can use COutputCache as an action filter to accomplish this task. The following code shows how we configure the cache filter:

public function filters()
{
    return array(
        array(
            'COutputCache',
            'duration'=>100,
            'varyByParam'=>array('id'),
        ),
    );
}

The above filter configuration would make the filter to be applied to all actions in the controller. We may limit it to one or a few actions only by using the plus operator. More details can be found in filter.

Page caching in Yii is very easy, 4.8 out of 5 based on 5 ratings
Categories: Guides & Tutorials

Keywords:

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.