Laravel4 Predis access to Redis returns error while reading line from the server

Source: Internet
Author: User
I used the Laravel 4 framework development and wanted to switch the cache server from Memcache to Redis, and the driver in config/cache.php was set to Redis. But call Cache::get () error, error information such as:


[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

 


However, accessing Redis-server from Redis-cli is OK, and it is no problem to test Redis separately in PHPUnit. But the direct use of the route is problematic.


Route::get('/test', function(){
    Cache::put('name', 'Taylor', 60); //error!

    $name = Cache::get('name'); 

    echo $name;
});


Who can explain the problem?



Add: Refresh multiple/test, there will be a few normal.






Reply content:



I used the Laravel 4 framework development and wanted to switch the cache server from Memcache to Redis, and the driver in config/cache.php was set to Redis. But call Cache::get () error, error information such as:


[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

 


However, accessing Redis-server from Redis-cli is OK, and it is no problem to test Redis separately in PHPUnit. But the direct use of the route is problematic.


Route::get('/test', function(){
    Cache::put('name', 'Taylor', 60); //error!

    $name = Cache::get('name'); 

    echo $name;
});


Who can explain the problem?



Add: Refresh multiple/test, there will be a few normal.






Predis's 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);
    }
    // The problem is that you need to set the timeout for the read stream, otherwise the connection will report an error when reading the stream data,
    // You can directly set read_write_timeout to 0 to solve the problem.
    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


Is there a folder in configproduction? Is there a cache.php file inside? That would overwrite the outside configuration.


  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.