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 ?
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.
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.
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.