简体   繁体   中英

Yii2 - session_start(): Cannot find save handler 'memcached' - session startup failed

I am getting this error: session_start(): Cannot find save handler 'memcached' - session startup failed

in config/web.php my settings are like this: under components

'cache' => [
          //  'class' => 'yii\caching\FileCache',
            'class'        => 'yii\caching\MemCache',
            'useMemcached' => true,
            'servers' => [
                [
                    'host' => '127.0.0.1',
                    'port' => 11211

                ],
            ],

The phpinfo shows memcache is installed correctly. 在此处输入图片说明

I also used below script from this link to check if memcache is working

<?php
if (class_exists('Memcache')) {
    $server = '127.0.0.1';
    if (!empty($_REQUEST['server'])) {
        $server = $_REQUEST['server'];
    }
    $memcache = new Memcache;
    $isMemcacheAvailable = @$memcache->connect($server);

    if ($isMemcacheAvailable) {
        $aData = $memcache->get('data');
        echo '<pre>';
        if ($aData) {
            echo '<h2>Data from Cache:</h2>';
            print_r($aData);
        } else {
            $aData = array(
                'me' => 'you',
                'us' => 'them',
            );
            echo '<h2>Fresh Data:</h2>';
            print_r($aData);
            $memcache->set('data', $aData, 0, 300);
        }
        $aData = $memcache->get('data');
        if ($aData) {
            echo '<h3>Memcache seem to be working fine!</h3>';
        } else {
            echo '<h3>Memcache DOES NOT seem to be working!</h3>';
        }
        echo '</pre>';
    }
}
if (!$isMemcacheAvailable) {
    echo 'Memcache not available';
}

?>

and I am getting the result

Fresh Data:
Array
(
    [me] => you
    [us] => them
)
Memcache seem to be working fine!

what I am missing here and how to fix this error.

In top of config/web.php file

use app\cart\storage\SessionStorage;
Yii::$container->setSingleton('app\cart\ShoppingCart');

Yii::$container->set('app\cart\storage\StorageInterface', function() {
    return new SessionStorage(Yii::$app->session, 'primary-cart');
});

Actually in addition to memcache you need to install memcached. when I tried apt-get install memcached I was getting memcached is installed and already newest version

but you need to install php-memcache like

apt-get install php-memcached and in your phpinfo both memcache memcached should show like below image. 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM