yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.23k stars 6.92k forks source link

Caching DbDependency makes multiple SQL queries #11263

Open cherifGsoul opened 8 years ago

cherifGsoul commented 8 years ago

I have this code:

$dataProvider = new ActiveDataProvider([
            'query'=>Todolist::find()
            ]);
        $dependency = \Yii::createObject([
            'class'=>'\yii\caching\DbDependency',
            'sql' => 'SELECT MAX(updated_at) FROM todolist'
            ]);

        Todolist::getDb()->cache(function ($db) use ($dataProvider){
            $dataProvider->prepare();
        },3600,$dependency);

I was surprised by the number of the sql query executed with the sql param, it generates 4 queries mq

I wonder if this is a bug or is the default behavior of the framework? The yii\caching\FileCache was used.

Q A
Yii version 2.0.7
PHP version 5.6.11-1ubuntu3.1
Operating system Ubuntu 15.10 & 14.04
cebe commented 8 years ago

from a quick look it seems you just need to set the dependency to be reuseable: http://www.yiiframework.com/doc-2.0/yii-caching-dependency.html#$reusable-detail

cherifGsoul commented 8 years ago

@cebe thank you for the answer it works like a charm :+1:

cebe commented 8 years ago

still weird that it generates 4 queries, should be checked.

cherifGsoul commented 8 years ago

I dont find any test for DbDependency component, can someone point me to the right test?

cebe commented 8 years ago

I'm afraid there is none, it should be in https://github.com/yiisoft/yii2/tree/master/tests/framework/caching