Problem with cache when change user's info and password

Dear all,

I’m new in our forum. I’ve stared with Yii Framework about 3 month ago.

Today I have a question, I tried to search forum, wiki and google but could not able to find a good solution.

I want to use cache for my profile, so I enabled cache class in config file (CFileCache and/or CMemcached).

It’s working. But I met problem when processing user password changing and user profile changing.

I used 2 ways to use caching system:

//The same for find, findAll, findBySql...




//The same for find, findAll, findBySql...

In Model User

public function findByPk($pk, $condition = '', $params = array()) {

        $cacheKey = 'user:'.$pk.':'.$condition.':'.serialize($params);

        $cache = Yii::app()->cache;

        if ($cacheKey === null)


            $result =  parent::findByPk($pk, $condition, $params);


        else if ($cache->get($cacheKey) === false && $cacheKey !== null)


            $result =  parent::findByPk($pk, $condition, $params);

            $cache->set($cacheKey, $result, Yii::app()->params['cacheExpire']);




            $result = $cache->get($cacheKey);


        return $result;


In both ways above. I cannot find the solution to delete cached key that belong to user when he/she change password or his own information in profile.

Looking forward for your helps.


Modify the model save() function so that it deletes the old cache. Or to be slightly more efficient, set the new cache.

Thank jellysandwich for your reply.

I tried your solution before.

  • Flush the whole cache: This way is a bad idea. Because for a big site this will make all cached contents to be invalid and server would be ‘kicked’ every time someone change their information.

  • Delete individual cache id: This way seems hard to be done.

++ For the default cache key (that was automatically created by Yii), the keys are randomly by md5 so it’s impossible.

++ For specific key (as the code I posted above). Also hard because it depends on the query. We maybe need query by user_id, user_email, user_password, user_name… (depends on scenario) so it’s quite hard to determine what was queried to delete.

Please give me a direction :)