Caching In An Activerecord Subclass

Hello all,

I have recently inhereted a Yii 1.1.8 application (had no prior knowledge of Yii), and am tasked with improving the performance.

Pageviewes commonly generate between a 100 and 200 database queries, and since the database is hosted on another server this means a lot of round-trips which increase the page load times (and will probably hinder scalability in the future).

Some of these queries get data that almost never change relatively to the amount of reads. For example, each page shows a message (which is stored in the DB), which might go unchanged for days (but it might also be changed multiple times in one day).

Now what I want to do is implement some sort of caching (using CFileCache) in the ActiveRecord classes that are used for such data items.

Basicly what I want to do is the following:




onBeforeSave(){

    invalidate the cash

}

onBeforeFind() {

    if(item is in cash){

        return cashed_item;

    } else {

        get item from db and return it;

    }

}



I haven’t yet started to implement this, because I am not familiar enough with Yii to know wether this is actually possibly within the ActiveRecord paradigm.

So my question is: Would this be easy to implemented in your opinion? Or should I setup cashing in some other way althogether??

Very interested in your opinions.

Instead of doing your caching in your models, do your caching at the points of use:

http://www.yiiframework.com/doc/guide/1.1/en/caching.data#query-caching

Should work great