Yii 1.1: cachedmodelbehavior

Handles model caching and its periodic synchronization with main data storage.



This component creates middle storage for used models in cache. Thus, it helps significantly alleviate DB server load. You may define time interval between data synchronizations from cache to main data storage.

Component is recommended for frequently requested/updated small pieces of data, for high-loaded Yii applications. Stored data should be not so significant, because it may be (rarely, but...) removed from cache storage: it is fast but trustless storage. For example, component may be used to store user visits, user likes etc in cache. It would be great, if you could use more reliable storage for data, such as Redis.

Note: component is not recommended for important user data (like passwords, payment transactions etc.), for large size or rarely requested/updated data (blog posts, logs etc.).

All api methods have the same names and arguments, as CActiveRecord class. So, if you set CachedModelBehavior::isEnabled to false or if there is no cached attributes, all methods will be simply downgraded to CActiveRecord methods, without using cache storage. No code changes needed anymore.


  • Using of cache storage via Yii provided abstraction level.
  • Allows to update model attributes in cache using different operators.
  • Periodic synchronization (saving) changed cached models to DB.
  • Allows to define synchronization time interval and how long to keep cached models in cache storage.
  • Delayed insert for new models, which are saved in cache instead of DB between synchronizations. They are saved to DB then in multiinsert SQL command during synchronization.
  • Allows to select cached or not cached fields to avoid cache overflow with big size fields (BLOB, TEXT etc).
  • Log warnings for unwanted situations.


Yii 1.1 or above


Extract the class file from the package of this extension in your protected/components directory.

You should add behavior to model to use extension:

class MyModel extends CActiveRecord
    public function behaviors()
        return array(
            'cachedModel' => array(
                'class' => 'CachedModelBehavior',
                'fields' => '',
                'excludedFields' => 'action,userid',
                'syncInterval' => 15,
                'delayedInsert' => true,

Then, in controller class you can use component methods:

$model = new MyModel;
$model->title = 'New Story';

Please, see component comments for full explanation.

And also please, please feel you free to comment, ask, suggest impovements and point to bugs or design mistakes. I try to do my best in programming.

Total 4 comments

#11301 report it
vittron at 2013/01/04 08:03am
Why downvoted?

Friends, could you please write, why do you downvote this component? I am open to your suggestions. Or is it just evaluation of my enthusiasm?

#11300 report it
vittron at 2013/01/04 07:59am
Differences from default cache

Hi, Interboy! The main difference from default cache is ability to change model attributes in cache and if model attributes were changed, all changes will be copied (synchronized) to DB some time later. You may define, how often system have to do synchronization. This component is useful for high-load applications, when there are thousands models and they are changed several times per second. If you use this component, all changes for models are stored in cache and changes will be stored in DB later, during synchronization. This helps alleviate DB server load. Please, let me know, whether you are satisfied with this answer.

#10991 report it
vittron at 2012/12/09 04:16am
Not primary keys

Hello, morteza!

Thank you for comment. Could you please provide more details, what is the matter of problem? What error message do you see?

If you are trying to update primary keys of model, this part is not implemented yet, really. I can't handle it soon because of my current workload. You can do it on your own, if you want. :)

#10986 report it
morteza at 2012/12/08 04:34am
hi.problem action update



for action update

Leave a comment

Please to leave your comment.

Create extension