APC and CActiveRecord

Did anyone already use APC with CActiveRecord to perform CData caching so there is no db queries for db data loading ?

I was thinking about using afterfind method to store in APC cache and the beforeFind and populateRecord method for restoring from cache

hi,

check this out for more performance http://www.yiiframework.com/doc/guide/1.1/en/topics.performance

Already gone through this page. It does not tell about CActiveRecord data caching but only caching db schema

I think you must to use your own cashing system, like memcashe or other.

I think current Yii AR implementation makes this troublesome. Make a CActiveRecordBehavior and implement beforeFind($event). In there you can access criteria for this find call (and maybe hash it for use as a cache key). Then if cache doesn’t contain your data (Yii::app()->cache->get($yourFindQuery) === false), you should call query and populateRecords and store it in cache. But if the key is found in cache, you should just return the existing data (probably an array of ARs) and prevent further AR behaviour, that is to continue with finding data. I haven’t figured how to do this though… I can store/find data in cache but AR will still fetch its data from DB the old way. There should be some way to prevent the execution of AR’s find method after data is found in cache… Any ideas are welcome!

I was able to get it working with CActiveRecord as follows:




$tableName  = [size=2] MyModel[/size][size=2]::model()->tableName();[/size]


$dependencySql ='select max('.$[size=2]tableName.'[/size][size=2].update_time)  from [/size]'.$[size=2]tableName;[/size]

[size=2]

[/size]

[size=2]$dependency = new CDbCacheDependency($dependencySql);[/size]


$myModelSql= 'select * from '. $tablename;


$result = MyModel::model()->cache(1000, $dependency)->findAllBySql($myModelSql);

Yep, that’s it. Also mentioned in this part of the guide.