我想找到登录用户的总分和&想要缓存它。 我的代码是:
$dependency = new CDbCacheDependency('SELECT MAX(id) FROM tbl_points_log where user_id='.Yii::app()->user->id);
$sql='SELECT SUM( point) as user_point FROM tbl_points_log left join tbl_action on tbl_action.id = tbl_points_log.action_type_id where user_id='.Yii::app()->user->id;
$user_point = Yii::app()->db->cache(1000, $dependency)->createCommand($sql)->queryAll();
以上代码是否正确? 我是否必须在配置文件中进行一些更改才能使查询缓存有效?我刚刚添加了
'cache' => array(
'class' => 'CDbCache'
),
在组件
下 var_dump($dependency->getHasChanged());
总是评估为true,即使我没有对数据库进行任何更改,那么为什么呢?
P.S不要担心SQL语句。它的工作!
答案 0 :(得分:1)
查找来自缓存或数据库的查询结果的最佳方法是打开日志记录,如下所示
'log'=>array(
'class'=>'CLogRouter',
'routes'=>array(
array(
'class'=>'CFileLogRoute',
'levels'=>'error, warning',
),
array(
'class'=>'CWebLogRoute',
),
),
),