dbcache 1.1.7

where i want to use dbcache to cache sql query.but i don’t know how to use it.

for example : this is sql query,$sql = ‘SELECT * FROM tbl_post’; if i need cache it ,how to use it

the official website document do it as the following,but what do "SELECT MAX(update_time) FROM tbl_post " do?

// cache the results of $sql for 1000 seconds or until tbl_post is updated

$sql = ‘SELECT * FROM tbl_post LIMIT 20’;

$dependency = new CDbCacheDependency(‘SELECT MAX(update_time) FROM tbl_post’);

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

the below is my sql query,how should i cache it ?

$connection=Yii::app()->db;

    $command=$connection->createCommand();


    $content=$command->select('*')


                     ->from('user')


                     ->queryAll();

$dependency = new CDbCacheDependency('SELECT MAX(update_time) FROM tbl_post');

Is a dependency. It is used to invalidate the cache when there are changes to the underlying table.

To cache a query, try:




$connection=Yii::app()->db;

$command=$connection->createCommand();

$dependency = new CDbCacheDependency('select count(*) from user'); // Set your dependency.

$connection->cache(60, $dependency); // Set your cache duration.

$content=$command->select('*')

->from('user')

->queryAll(); 



See http://www.yiiframework.com/doc/api/1.1/CDbConnection#cache-detail and http://www.yiiframework.com/doc/guide/1.1/en/caching.data#query-caching.

thank you very much for your answer!

$connection=Yii::app()->db;

$command=$connection->createCommand();

$dependency = new CDbCacheDependency(‘select count(*) from user’); // Set your dependency.

[color="#4B0082"][font=“Arial Black”]here 'select count(*) from user ,don’t you write it wrong, it should be select * from user ,shouldn’t it?,must the sql be same as the sql query i need to cache?[/font][/color]

$connection->cache(60, $dependency); // Set your cache duration.

$content=$command->select(’*’)

->from(‘user’)

->queryAll();

The query for the cache dependency should be a simple query to detect the changes to your table, e.g. max timestamp, or some value that changes every time the table data is modified. Doing a ‘select * from user’ may not be a good candidate.

thank you! i get it