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
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/When you don't use a pagination setting
CActiveDataProvider's pagination property tofalseyou probably have to setqueryCountparameter of cache() method to1but not to2.please help me.
Alban, don't worry about this. Thought thrives on conflict.
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 :)
Alban.
Hi. This working for me on Yii 1.1.8. I enable WebLogRoute:
And profiling queries (it's highlight actually query to database):
And I see that query actually gets from cache. But also this queries puts in queries list.
Hi,
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
Regards.
Alban.
Leave a comment
Please login to leave your comment.