How does query caching works after all?

Here is the thing, i am trying to make use of the query caching feature, but somehow, the queries that should be cached, are still executed.

Beside this, when i do a




var_dump($dependency->getHasChanged());



it always evaluates to true, even if i did no changes into database, so why is that?

To be more clear, here is what i am doing:




$criteria=new CDbCriteria;

$criteria->select='t.box_id, t.title, t.status, t.sort_order';

$criteria->order='t.sort_order ASC, t.box_id DESC';

$dependency=new CDbCacheDependency('SELECT MAX(last_updated) FROM {{footer_box}}');

var_dump($dependency->getHasChanged());//this always evaluates to true.

$boxes=FooterBox::model()->cache(84000, $dependency)->findAll($criteria);



The application log says




Querying SQL: SELECT t.box_id, t.title, t.status, t.sort_order FROM `cms_footer_box` `t` ORDER BY t.sort_order ASC, t.box_id DESC

in ... line ...


Followed by 


Querying SQL: SELECT MAX(last_updated) FROM cms_footer_box 

in... line ...


And then: 


Query result found in cache

in... line ...



And this repeats for each request, but if the query result was found in cache, why the query




Querying SQL: SELECT t.box_id, t.title, t.status, t.sort_order FROM `cms_footer_box` `t` ORDER BY t.sort_order ASC, t.box_id DESC



repeats for every request and more, why $dependency->getHasChanged() evaluates to true ?

What am i missing here ?

Really, nobody ?

At least, is there a person willing to test on his own application the query caching feature and share the findings ?

Silly question but … have you configured a cache component for your app?

The question is pertinent, but yes, i have a cache component active :)

Here we are: http://code.google.com/p/yii/source/browse/tags/1.1.8/framework/db/CDbCommand.php#461

The Yii trace will contain that query regardless if it’s been cached or not.

Hah yes, i discovered this on my own, actually i got this after i did a :




<?php echo 'DB DEBUG<br /><pre>';print_r(Yii::app()->db->stats);echo '</pre>';?>



This way i saw the number of queries being sent to server.

XWebDebugRouter(yii toolbar ext) and CWebLogRoute will report all the queries even if they’ve been sent to server or not, this is creating confusion, at least for me.