[Solved] Page Cache For Multiple Actions In A Controller?

I added a file cache to an index view and it’s working great:

main.php




'components'=>array(

    'cache'=>array(

                'class'=>'system.caching.CFileCache',

                ),




PostController.php




public function filters()

{

    return array(

        'accessControl', // perform access control for CRUD operations

        'postOnly + delete', // we only allow deletion via POST request

         array(

        'COutputCache + index',

        'requestTypes' => array('GET'),

        'duration'=>3600,

        'varyByParam' => array('Post_page'),     

        'dependency'=>array(

            'class'=>'system.caching.dependencies.CDbCacheDependency',

            'sql'=>'SELECT GREATEST(MAX(created), MAX(modified)) FROM post',

             ),

        ),

    );

}



However this controller has several actions and I want to have a cache for a few others. But the dependency for each cache will vary. For example I have a view action to view a single post with comments. However the dependency for this should be "has either this specific post or comments changed?" rather than "has any post changed?" as above.

How can I specify a different cache dependency for each controller action? Additionally, is there an easier way to go about this caching without specifying bespoke caching strategies for each page?

Is it possible to do differentiate the cache based on the action type such as below?




public function filters()

{

    return array(

        'accessControl', // perform access control for CRUD operations

        'postOnly + delete', // we only allow deletion via POST request

         array(

        'COutputCache + index',

        'requestTypes' => array('GET'),

        'duration'=>3600,

        'varyByParam' => array('Post_page'),     

        'dependency'=>array(

            'class'=>'system.caching.dependencies.CDbCacheDependency',

            'sql'=>'SELECT MAX(modified) FROM post',

             ),

         array(

        'COutputCache + view',

        'requestTypes' => array('GET'),

        'duration'=>3600,

        'varyByParam' => array('id'),     

        'dependency'=>array(

            'class'=>'system.caching.dependencies.CDbCacheDependency',

            'sql'=>'SELECT GREATEST(MAX(p.modified, MAX(c.modified)) FROM post p INNER JOIN comment c ON p.id = c.post_id WHERE p.id = :id',

            'params' => array('id', $_GET["id"]),

             ),

        ),

    );

}



Anyone? I thought this would be a pretty normal use case.

Yes, that is perfectly possible. See also the Yii Guide on controllers and filters.

Note the dependency sql I used above for the view won’t work. Instead I used something like:




SELECT MAX(modified)

    FROM 

    (

    SELECT modified FROM post WHERE slug = :slug

    UNION

    SELECT MAX(c.modified) AS modified

    FROM comment c INNER JOIN post p ON c.post_id = p.id WHERE p.slug = :slug

    )

    AS subquery



And the params should be:


'params' => array('id' => $_GET["id"]),

not:


'params' => array('id', $_GET["id"]),

Thanks. It wasn’t obvious to me from the documentation whether you could specify the same action more than one time.