Yii Framework Forum: Problem with cache when change user's info and password - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Problem with cache when change user's info and password How to remove individual cache key when users change their info, pass Rate Topic: -----

#1 User is offline   sonnb 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 24-October 11

Posted 24 October 2011 - 11:39 AM

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...
User::model()->cache($time)->findByPk(x)

and
User::model()->findByPk(x)
//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']);
        }
        else
        {
            $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.

Thanks.
0

#2 User is offline   jellysandwich 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 86
  • Joined: 03-May 11

Posted 25 October 2011 - 07:28 AM

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

#3 User is offline   sonnb 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 24-October 11

Posted 25 October 2011 - 09:21 AM

View Postjellysandwich, on 25 October 2011 - 07:28 AM, said:

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.
0

#4 User is offline   sonnb 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 24-October 11

Posted 26 October 2011 - 10:25 PM

Please give me a direction :)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users