Using Cache In Cactiverecord

I’m using cache in CActiveRecord as:

$myoboject = ListModel::model()->cache(60)->findAll();

According to documentation it caches the result set for 60 seconds.

I would like to ask, will it open a new DB connection if another request will come within 60 second ? What I think it should not do like this, instead it should fetch the cached data.

I’m having this problem because I wanted to avoid the multiple db connection being opened by a script and I want to cache the data for 60 seconds, and after that it can be invalidated.

Am I on the right track ? is this a normal behaviour ? will anybody please let me know if there is something wrong ?

I have used both Memcache and Sqlite.


If we trust (of course we should ) the guide Data Caching - Query Caching it should save querying the db with the same select shouldn’t open a new db connection within 60 seconds.

As I’m monitoring the ‘SHOW FULL PROCESSLIST’, I see that it is opening new connection.

Well, it doesn’t necessarily prevent the db connection. But it saves a query ;)

Thanks or correction

I was actually trying to avoid opening new connections using cache.

Is there any work around ?

I think you can use data cache or page cache instead of query cache. But …

I’m not quite sure what you mean by “the multiple db connection”. As far as I understand, there will be no multiple db connections opened in the normal circumstance, regardless of how many queries you execute or how many kind of CActiveRecords you use … they all share the single connection. Is there any particular reason why you have to avoid db connection?

I’ve a script that can be called from multiple apps/web sites. Ofourse each separate request will open a new db connection. I want to avoid this by using cache. So only first request should open db and other requests should use cache without opening new connections untill cache exires.

U got what I mean ? This is my goal.

I see. I thought you were working on a single app.

Then the basic usage of the cache (data cache) can be applied like this:

$id = 'my-objects-cache'; // or some unique name

$myobojects =Yii::app()->cache->get($id);

if($myoboject === false)


    $myobojects = ListModel::model()->findAll();

    Yii::app()->cache->set($id, $myobojects, 60);  // 60 second duration


Probably you should have already tried. :)

If you want something more convenient, I’m afraid you have to write your own extended version of CActiveRecord.

I’m not quite sure why you want to avoid opening db connections. After all, it is not that terribly expensive resource-wise. However, I’m afraid this isn’t done by just using the query or fragment cache … You will need to set CDbCOnnection.autoConnect is set to false as well. AND you need to check no other query is being made.

Thanks guys for replying so fast. Let me conclude this discussion.

According to some research, your replies and analyzing the CDConnection and CActiveRecord code, it is confirmed that it must open a new connection. So either we write our code to avoid this or wait for a feature. Till than we can use data caching by saving the data object into cache.

Thank you all for your time.