php - laravel4 Predis访问redis返回Error while reading line from the server
阿神
阿神 2017-04-10 15:03:42
[PHP讨论组]

我使用了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,会有几次正常。

阿神
阿神

闭关修行中......

全部回复(2)
伊谢尔伦

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 文件?那样的话会覆盖外面的配置。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号