简体   繁体   中英

Yii Cache Filter Specify Actions

I would like to cache only specified actions in my controller, how can I specify using that filter?

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

INFO:

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.

But I dont know how to use that =/

It's quite simple actually, see the guide :

Using the plus and the minus operators, we can specify which actions the filter should and should not be applied to. In the above, the postOnly filter will be applied to the edit and create actions, while PerformanceFilter filter will be applied to all actions EXCEPT edit and create. If neither plus nor minus appears in the filter configuration, the filter will be applied to all actions.

So your code will become like so:

array(
    'COutputCache + actionId, actionId2', // applies filter to only actions actionId and actionId2
    'duration'=>100,
    'varyByParam'=>array('id'),
),

Or if you want to apply filter to all actions but actionId, and actionId2:

array(
    'COutputCache - actionId, actionId2', // applies filter to all actions except actions actionId and actionId2
    'duration'=>100,
    'varyByParam'=>array('id'),

)

To use + or - depends on which which set is smaller to specify, like say from 10 actions you want to apply filter to all except 2 actions, then use - to indicate the 2 actions the filter should not apply to. But if from 10 actions you want to apply filter to only 2 actions then use + .


Note: Incase you want to know the actionId, it is the string after action in public function actionHelloWorld(){...} , so in this example it is helloWorld .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM