yii-starter-kit / yii2-starter-kit

Yii2 Starter Kit
http://yii2-starter-kit.terentev.net
Other
1.42k stars 648 forks source link

mb_strlen() expects parameter 1 to be string, array given while using redis cache #733

Closed badmansan closed 4 years ago

badmansan commented 4 years ago

Code from common/widgets/DbText.php (for example):

$cacheKey = [
    WidgetText::class,
    $keyLocale,
];
$content = Yii::$app->cache->get($cacheKey);

This code will cause warning:

yii\base\ErrorException: mb_strlen() expects parameter 1 to be string, array given in C:\webservers\home\site.local\vendor\yiisoft\yii2-redis\src\Connection.php:721 Stack trace:

0 C:\webservers\home\site.local\vendor\yiisoft\yii2-redis\src\Connection.php(721): yii\web\ErrorHandler->handleError('???', '???', '???', '???', '???')

1 C:\webservers\home\site.local\vendor\yiisoft\yii2-redis\src\Connection.php(721): ::mb_strlen('???', '???')

2 C:\webservers\home\site.local\vendor\yiisoft\yii2-redis\src\Connection.php(675): yii\redis\Connection->executeCommand('???', '???')

3 C:\webservers\home\site.local\common\widgets\DbText.php(33): yii\redis\Connection->__call('???', '???')

4 C:\webservers\home\site.local\common\widgets\DbText.php(33): yii\redis\Connection->get('???')

5 C:\webservers\home\site.local\vendor\yiisoft\yii2\base\Widget.php(140): common\widgets\DbText->run()

6 C:\webservers\home\site.local\frontend\views\site\index.php(38): yii\base\Widget::widget('???')

7 C:\webservers\home\site.local\vendor\yiisoft\yii2\base\View.php(348): yii\web\View->unknown()

8 C:\webservers\home\site.local\vendor\yiisoft\yii2\base\View.php(257): yii\web\View->renderPhpFile('???', '???')

9 C:\webservers\home\site.local\vendor\yiisoft\yii2\base\View.php(156): yii\web\View->renderFile('???', '???', '???')

10 C:\webservers\home\site.local\vendor\yiisoft\yii2\base\Controller.php(386): yii\web\View->render('???', '???', '???')

11 C:\webservers\home\site.local\frontend\controllers\SiteController.php(46): frontend\controllers\SiteController->render('???', '???')

12 C:\webservers\home\site.local\vendor\yiisoft\yii2\base\InlineAction.php(57): frontend\controllers\SiteController->actionIndex()

13 C:\webservers\home\site.local\vendor\yiisoft\yii2\base\InlineAction.php(57): ::call_user_func_array:{C:\webservers\home\site.local\vendor\yiisoft\yii2\base\InlineAction.php:57}('???', '???')

14 C:\webservers\home\site.local\vendor\yiisoft\yii2\base\Controller.php(157): yii\base\InlineAction->runWithParams('???')

15 C:\webservers\home\site.local\vendor\yiisoft\yii2\base\Module.php(528): frontend\controllers\SiteController->runAction('???', '???')

16 C:\webservers\home\site.local\vendor\yiisoft\yii2\web\Application.php(103): yii\web\Application->runAction('???', '???')

17 C:\webservers\home\site.local\vendor\yiisoft\yii2\base\Application.php(386): yii\web\Application->handleRequest('???')

18 C:\webservers\home\site.local\frontend\web\index.php(22): yii\web\Application->run()

19 {main}

I think this is because of array in key value instead of string. Redis cache executeCommand() can't work with multidimensional array.

Let's see at section maintenance in frontend/config/web.php: return $app->keyStorage->get('frontend.maintenance') === 'enabled';

It call

executeCommand('GET', [
    [0] => [
            [0] => common\components\keyStorage\KeyStorage
            [1] => _keyStorage
            [2] => frontend.maintenance
    ]
])

instead of

executeCommand('GET', [
    [0] => common\components\keyStorage\KeyStorage
    [1] => _keyStorage
    [2] => frontend.maintenance
])
badmansan commented 4 years ago

Sorry, this isn't issue. I was mistake in cache class declaration:

'cache' => [
    'class' => \yii\redis\Connection::class,
    'hostname' => env('REDIS_HOST'),
    'port' => env('REDIS_PORT'),
    'database' => env('REDIS_DB'),
],

must be

'cache' => [
    'class' => \yii\redis\Cache::class,
    'redis' => [
        'hostname' => env('REDIS_HOST'),
        'port' => env('REDIS_PORT'),
        'database' => env('REDIS_DB'),
    ],
],