laravel / ideas

Issues board used for Laravel internals discussions.
939 stars 28 forks source link

Add serializer option (igbinary) to phpredis connector [solution] #1659

Open asdf072 opened 5 years ago

asdf072 commented 5 years ago

I need igbinary support in the phpredis Redis client connector. This works:

Redis::setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_IGBINARY);

but it's not global. I already have this solution in production. Works as advertised w/ no apparent conflicts or breakage.

// .env

REDIS_SERIALIZER=igbinary
// config/database.php

'redis' => [
    'options' => [
        ...
        'serializer' => env('REDIS_SERIALIZER', ''),
    ]
]
// vendor/laravel/framework/src/Illuminate/Redis/Connectors/PhpRedisConnector.php

protected function createClient(array $config)
{
    return tap(new Redis, function ($client) use ($config) {
        $this->establishConnection($client, $config);

        if (! empty($config['password'])) {
            $client->auth($config['password']);
        }

        // .......

        // --HERE--
        if (! empty($config['serializer'])) {
            $serializer = 'Redis::SERIALIZER_' . strtoupper($config['serializer']);
            if( defined($serializer) ) {
              $serializer_const = constant($serializer);
              $client->setOption(Redis::OPT_SERIALIZER, $serializer_const);
            }
        }

    });
}
pmochine commented 4 years ago

For future readers why this would be awesome (no spam): https://medium.com/@akalongman/phpredis-vs-predis-comparison-on-real-production-data-a819b48cbadb Here is one small "hack" how to get it https://github.com/akalongman/laravel-lodash#redis-using-igbinary