Yii 1.1: Using cache in CActiveDataProvider


First param in CActiveDataProvider could be a string with model name or instance of class. So, you may use CActiveRecord::cache() for cache, but you need set third param to 2, because you should cache 2 queries: counting and get data.

Don't forget to use dependecy for cache invalidate.

$dependecy = new CDbCacheDependency('SELECT MAX(update_time) FROM {{post}}')
CActiveDataProvider(Post::model()->cache($duration, $dependecy, 2), array ( 
    'criteria' => array ( 
        'condition' => 'status = 1',
        'order' => 'DESC create_time',
    'pagination' => array ( 
        'pageSize' => 20, 

Total 7 comments

#13159 report it
Nabi at 2013/05/09 12:00pm
seems $dependecy does not work with CActiveDataProvider!

I see all query run in background but I don't see the SELECT MAX(update_time) FROM... query! seems $dependecy does not work with CActiveDataProvider! http://www.yiiframework.com/forum/index.php/topic/43115-why-dont-apply-dependency-for-cache-in-cactivedataprovider/

#12877 report it
Ezze at 2013/04/17 10:17am
No pagination

When you don't use a pagination setting CActiveDataProvider's pagination property to false you probably have to set queryCount parameter of cache() method to 1 but not to 2.

#8026 report it
Lê Văn Chiến at 2012/05/04 06:34am
has a unique index when I use the query
public static function articlesFeatureInCat($catId){
        $dependecy = new CDbCacheDependency('SELECT MAX(created_time) FROM article');
        return new CActiveDataProvider(self::model()->cache(3600, $dependecy, 2), array ( 
                'criteria' => array ( 
                    'condition' => 'primary_category = '.$catId,
                    'condition' => 'is_feature = '.self::FEATURE,
                    'condition' => 'status = '.self::STATUS_AVAILABLE,
                    'order' => 'DESC sorder',
                ) ,
                'pagination' => array ( 
                    'pageSize' => 20, 

please help me.

#5092 report it
Stamm at 2011/09/14 06:50am
Re: My apologies

Alban, don't worry about this. Thought thrives on conflict.

#5087 report it
juban_ at 2011/09/14 02:11am
My apologies

Yes you are right. This is perfectly working.

I owe you an apology.

In fact, I had it all wrong about the third parameter. It's what make it work with the data provider. Forget my comments.

So, great tip, thanks :)


#5079 report it
Stamm at 2011/09/13 10:11am
RE: Warning! This is wrong.

Hi. This working for me on Yii 1.1.8. I enable WebLogRoute:


And profiling queries (it's highlight actually query to database):

'enableParamLogging' => true,
'enableProfiling' => true,

And I see that query actually gets from cache. But also this queries puts in queries list.

#5072 report it
juban_ at 2011/09/13 02:29am
Warning! This is wrong.


I'm afraid that what you describe here will not work at all (not with Yii 1.1.7 or 1.1.8 at least).

First, activate the Yii queries log. You will see that the data provider is never cached. Second, third parameter of the cache method is meant to set the number of the same query needed to activate the cache mechanism. Meaning that, if this was really working, you will need to call the data provider twice to have it cached.

To properly use the cache with a data provider, you could use this extension: EActiveDataProviderEx



Leave a comment

Please to leave your comment.

Write new article
  • Written by: Stamm
  • Category: Tips
  • Yii Version: 1.1
  • Votes: +21 / -1
  • Viewed: 22,178 times
  • Created on: Sep 12, 2011
  • Last updated: never
  • Tags: CActiveDataProvider, cache