In doubt about fragment cache.

Hi there,

here is my problem, or doubt: I have a menu, which varies depending on the user id, so, one user could see some options and another user some different options, depending on the privileges assigned to each one. I think that setting the cache $id to something which includes the user’s id would solve this situation (please correct me if I’m wrong). But I also need to refresh the cache if the user logs out and logs in again, because some privilege could have been changed meanwhile, and I don’t want him to wait up to one hour (that’s my cache duration) to see the new options in the menu. So, I’m using this:

if ($this->beginCache(sha1('mainMenu_'.Yii::app()->user->id), array('duration'=>3600, 'varyBySession'=>true))) {

 // menu here


Would this be right? Also, does this create an entry in the cache for every opened session?


As far as i know, varyBySession uses the session id and when the user logs out, it won’t change the session ID, unless you say so.

So, you can use the afterLogout() method to manually clear the cache based on the user_id(or to regenerate the session_id, case in which the cache would invalidate itself automatically) .

If i where you, i would try to use a CDbCacheDependency that uses the "last login" of the user to invalidate the cache.

After a quick test, the SID cookie changes after re-login

how to cache the menu to reduce the number of queries needed to build it?