laravel4 Predis訪問redis返回Error while reading line from the server

來源:互聯網
上載者:User
我使用了laravel 4 framework開發,想把cache server從memcache切換到redis,在config/cache.php的driver設定為redis。但是調用cache::get()報錯,報錯資訊如:

[2014-10-21 18:47:44] production.ERROR: exception 'Predis\Connection\ConnectionException' with message 'Error while reading line from the server [tcp://127.0.0.1:6379]' in /home/nginx/mono/vendor/predis/predis/lib/Predis/Connection/AbstractConnection.php:141

但是從redis-cli訪問redis-server是ok的,在phpunit中單獨測試redis也沒問題。但是在Route中直接使用有問題。

Route::get('/test', function(){    Cache::put('name', 'Taylor', 60); //報錯!    $name = Cache::get('name');     echo $name;});

誰能解釋下問題在哪?

補充:重新整理多次/test,會有幾次正常。

回複內容:

我使用了laravel 4 framework開發,想把cache server從memcache切換到redis,在config/cache.php的driver設定為redis。但是調用cache::get()報錯,報錯資訊如:

[2014-10-21 18:47:44] production.ERROR: exception 'Predis\Connection\ConnectionException' with message 'Error while reading line from the server [tcp://127.0.0.1:6379]' in /home/nginx/mono/vendor/predis/predis/lib/Predis/Connection/AbstractConnection.php:141

但是從redis-cli訪問redis-server是ok的,在phpunit中單獨測試redis也沒問題。但是在Route中直接使用有問題。

Route::get('/test', function(){    Cache::put('name', 'Taylor', 60); //報錯!    $name = Cache::get('name');     echo $name;});

誰能解釋下問題在哪?

補充:重新整理多次/test,會有幾次正常。

Predis的StreamConnection Class

/** * Initializes a TCP stream resource. * * @param  ConnectionParametersInterface $parameters Parameters used to initialize the connection. * @return resource */protected function tcpStreamInitializer(ConnectionParametersInterface $parameters){    $uri = "tcp://{$parameters->host}:{$parameters->port}";    $flags = STREAM_CLIENT_CONNECT;    if (isset($parameters->async_connect) && $parameters->async_connect) {        $flags |= STREAM_CLIENT_ASYNC_CONNECT;    }    if (isset($parameters->persistent) && $parameters->persistent) {        $flags |= STREAM_CLIENT_PERSISTENT;        $uri .= strpos($path = $parameters->path, '/') === 0 ? $path : "/$path";    }    $resource = @stream_socket_client($uri, $errno, $errstr, $parameters->timeout, $flags);    if (!$resource) {        $this->onConnectionError(trim($errstr), $errno);    }    //問題在這裡,需要給讀取流設定逾時時間,否則就在讀取流資料時串連報錯,    //可以直接設定read_write_timeout為0,以此解決問題。    if (isset($parameters->read_write_timeout)) {        $rwtimeout = $parameters->read_write_timeout;        $rwtimeout = $rwtimeout > 0 ? $rwtimeout : -1;        $timeoutSeconds  = floor($rwtimeout);        $timeoutUSeconds = ($rwtimeout - $timeoutSeconds) * 1000000;        stream_set_timeout($resource, $timeoutSeconds, $timeoutUSeconds);    }    if (isset($parameters->tcp_nodelay) && function_exists('socket_import_stream')) {        $socket = socket_import_stream($resource);        socket_set_option($socket, SOL_TCP, TCP_NODELAY, (int) $parameters->tcp_nodelay);    }    return $resource;}

production.ERROR

config 中是否有 production 檔案夾?裡面是否有 cache.php 檔案?那樣的話會覆蓋外面的配置。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.