Yii2: Db cache memory exausted

Created on 28 Mar 2020  路  10Comments  路  Source: yiisoft/yii2

I updated to Yii 2.0.33 (then 2.0.34) and all my applications broke.
I'm getting "Allowed memory size of 268435456 bytes exhausted (tried to allocate 20480 bytes)" from line 132 of yiisoft/Yii2/caching/DbCache.php.
Here's my cache and db component configuration:

return [ 'components' => [ 'cache' => [ 'class' => 'yii\caching\DbCache', 'cacheTable' => 'YiiCache', ], 'db' => [ ..., 'enableSchemaCache' => true, 'schemaCacheDuration' => 3600 * 24, ] ];

Flushing the cache doesn't fix the problem.

Additional info

| Q | A
| ---------------- | ---
| Yii version | 2.0.33
| PHP version | 7.0.33
| Operating system| linux

bug

Most helpful comment

@samdark I'm on dev-master and back on DbCache. Everything works just fine. Thank you

All 10 comments

What DB are you using?

@alex-code MySQL/MariaDB/Percona

Interesting. It is reproducible with basic application template by enabling DB cache?

@samdark Yes it is. Cache table:

CREATE TABLE YiiCache (
  id char(128) NOT NULL,
  expire int(11) DEFAULT NULL,
  data longblob DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

Db configuration:

return [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=...',
'username' => '...',
'password' => '...',
'charset' => 'utf8',
// Schema cache options (for production environment)
'enableSchemaCache' => true,
'schemaCacheDuration' => 60,
'schemaCache' => 'cache',
];

Cache component:
'cache' => [
'class' => 'yii\cachingDbCache',
'cacheTable' => 'YiiCache',
],

A simple query crashes the basic application. Didn't try with ActiveRecord but i guess it'll crash it too.
On this server PHP is 7.2.27, db is 10.3.22-MariaDB-cll-lve, but i'm having the same issue on other servers with different configurations (PHP 7.0 and other MySQL engines).

I think the issue lies here, https://github.com/yiisoft/yii2/blob/master/framework/db/mysql/QueryBuilder.php#L402 If you're using DbCache.

When the QueryBuilder is constructed the QueryBuilder::supportsFractionalSeconds method is called which tries to check the cache for a result.
Checking the cache then creates a new QueryBuilder instance which then checks the cache which then etc...

Looks like the same issue, https://github.com/yiisoft/yii2/issues/17943

Maybe QueryBuilder::supportsFractionalSeconds should avoid DB based caches?

@alex-code likely you are correct. Do you have some time to implement a fix?

@samdark Yeh, I'll try and get a PR this evening.

@mauriziocingolani would you please change Yii dependency in composer.json to dev-master@dev and check if it works?

@samdark sure. i'll let you know as soon as possible

@samdark I'm on dev-master and back on DbCache. Everything works just fine. Thank you

Was this page helpful?
0 / 5 - 0 ratings