N
N
Natalia2016-10-11 11:53:58
Yii
Natalia, 2016-10-11 11:53:58

Yii2 does not cache entire additional requests - what to do?

Enable caching:

return [
    'class' => 'yii\db\Connection',
    'dsn' => 'mysql:host=mysql;dbname=my',
    'username' => 'root',
    'password' => 'pass',
    'charset' => 'utf8',

    // включение кэширование схемы БД
    'enableSchemaCache' => true,
];

Before inclusion 74 requests. after - 70. Of these, 20 are SHOW... and 20 SELECT kcu.constraint_name, kcu.column_name,... Write permissions
for cache files are set - Yii2 debug panel does not display write errors in Log.
Just in case, I already wrote to the DB config
// Так
'schemaCacheDuration' => 0,
'schemaCache' => 'cache',

//и так тоже
'schemaCacheDuration' => 3600,

The cache component is also connected
'cache' => [
      'class' => 'yii\caching\FileCache',
],

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Natalia, 2016-10-12
@Hatysik

As a result, it turned out that if the owner of the cache folder is not www-data, then requests are not cached
Treatment:
1. Change the owner of the cache folder
chown www-data:www-data cache
2. Delete everything from the cache directory.
rm -rf ./*

D
Dmitry, 2016-10-11
@slo_nik

I think that to solve your problem, you need to use lazy and eager loading .
There are two tables "customer" and "orders" which are related

class Customer extends ActiveRecord
{
    public function getOrders()
    {
        return $this->hasMany(Order::className(), ['customer_id' => 'id']);
    }
}

class Order extends ActiveRecord
{
    public function getCustomer()
    {
        return $this->hasOne(Customer::className(), ['id' => 'customer_id']);
    }
}

Such code should reduce the number of database accesses.
$customers = Customer::find()
    ->with('orders')
    ->all();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question