how to enable query caching yii framework

I’m using query caching.

In my config file under components I just added

     'cache' => array(

                'class' => 'CDbCache'


But I dont think this is enough as I have checked the results are fetched everytime from the database & not from the cache.

I was going through . And I couldn’t understand this part [b]To enable query caching, make sure CDbConnection::queryCacheID refers to the ID of a valid cache application component (it defaults to cache).


So I think I’ve to check my configuration. Any help?

// /protected/config.main.php



		'connectionString' => 'mysql:host=localhost;dbname=xxxxx',

		'emulatePrepare' => true,

		'username' => 'xxxxx',

		'password' => 'xxxxx',

		'charset' => 'utf8',

		'schemaCachingDuration' => 60 * 60 * 24 * 30,

		'queryCacheID' => 'cache', // this is it



I’m sorry. When you are to use the default cache component (whose name is ‘cache’), then you will not need to specify ‘queryCacheID’ explicitly like the above.

Thanks for the reply. But still var_dump($dependency->getHasChanged()); always evaluates to true, even if I did no changes into database, so why is that? Is it because my query caching is not enabled?

  1. I’m sorry. I think I was wrong in the previous post.

The reference seems to say that it’s not necessary to specify ‘queryCacheID’ explicitly when you use default cache component (that is ‘cache’) for it.

  1. about $dependency->getHasChanged()

I guess you can not get the right value by var_dump($dependency->getHasChanged()).

I think the method won’t have meaning when you call it directly. It should be called by the cache component that need to check the dependency.


The cache dependency definition and its result are saved together with the cache content into the cache entry. And the cache dependency definition will be re-evaluated to check if the result is still the same or not when they are loaded with the cache content next time when you access the cache entry.

So, checking getHasChanged() right after you’ve created the dependency will have no meaning …

I think I should paste my code


'db' => array(

            'connectionString' => 'mysql:host=localhost;dbname=mydb',

            'emulatePrepare' => true,

            'username' => 'root',

            'password' => '',

            'charset' => 'utf8',

            'tablePrefix' => 'tbl_',

            'schemaCachingDuration' => 60 * 60 * 24 * 30,

            'queryCachingDuration'=>60 * 60 * 24 * 30,

            //'queryCacheID' => 'cache', // this is it



$dependency = new CDbCacheDependency('SELECT MAX(id) FROM tbl_points_log');    

    $sql='SELECT SUM( point) as user_point FROM tbl_points_log left join tbl_action on = tbl_points_log.action_type_id where user_id='.Yii::app()->user->id;

    $user_point = Yii::app()->db->cache(1000, $dependency)->createCommand($sql)->queryAll();


& its always returning me true.

Ah, sorry. I’ve edited my previous post.

In short, I think the caching should be working fine, and you have to use some other means to check the functionality of cache dependency. Probably by sql logging.

Thanks for trying to help me out. but I still have a doubt. I don’t understand why it says in that

[b]Enabling Query Caching

To enable query caching, make sure CDbConnection::queryCacheID refers to the ID of a valid cache application component (it defaults to cache).[/b]

So now what the heck is this "cache application component"?

I have checked ‘/framework/db/CDbConnection.php’. Please take a look at it yourself.

You will notice that these properties are defined and have their default values regarding the caching.

	public $schemaCachingDuration=0;

	public $schemaCacheID='cache';

	public $queryCachingDuration=0;

	public $queryCachingDependency;

	public $queryCachingCount=0;

	public $queryCacheID='cache';

We have to override schemaCachingDuration and queryCachingDuration if we want schema caching and query caching as you did in your configuration. But we don’t need to explicitly set schemaCacheID and queryCacheID if we are going to use ‘cache’ for cache component.

We can use multiple cache components and we may name them as we like, for example:

	'cache' => array(

		'class' => 'CFileCache',


	'apcCache' => array(

		'class' => 'CApcCache',


In the above we use CFileCache as the default ‘cache’ component. And we use another cache component which is named ‘apcCache’ and is a CApcCache.

And if we want to use ‘apcCache’ for query caching, then we have to set ‘queryCacheID’ to ‘apcCache’.

'db' => array(


            'queryCacheID' => 'apcCache',