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,
)
));
Warning! This is wrong.
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.
RE: Warning! This is wrong.
Hi.
This working for me on Yii 1.1.8.
I enable WebLogRoute:
array( 'class'=>'CWebLogRoute', ),
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.
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 :)
Alban.
Re: My apologies
Alban, don't worry about this. Thought thrives on conflict.
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.
No pagination
When you don't use a pagination setting
CActiveDataProvider
's pagination property tofalse
you probably have to setqueryCount
parameter of cache() method to1
but not to2
.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/
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.