8000 [Cache] Added support for Redis igbinary serializer by kmadejski · Pull Request #27709 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Cache] Added support for Redis igbinary serializer #27709

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function testCreateConnection()
'read_timeout' => 0,
'retry_interval' => 0,
'lazy' => false,
'serializer' => 0,
'database' => '1',
'password' => null,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public function testCreateConnection()
$this->assertInstanceOf(\Redis::class, $redis);
$this->assertTrue($redis->isConnected());
$this->assertSame(0, $redis->getDbNum());
$this->assertEquals(0, $redis->getOption(\Redis::OPT_SERIALIZER));

$redis = RedisAdapter::createConnection('redis://'.$redisHost.'/2');
$this->assertSame(2, $redis->getDbNum());
Expand All @@ -51,6 +52,9 @@ public function testCreateConnection()

$redis = RedisAdapter::createConnection('redis://'.$redisHost, array('read_timeout' => 5));
$this->assertEquals(5, $redis->getReadTimeout());

$redis = RedisAdapter::createConnection('redis://'.$redisHost, array('serializer' => 2));
$this->assertEquals(2, $redis->getOption(\Redis::OPT_SERIALIZER));
}

/**
Expand Down
11 changes: 11 additions & 0 deletions src/Symfony/Component/Cache/Traits/RedisTrait.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ trait RedisTrait
'read_timeout' => 0,
'retry_interval' => 0,
'lazy' => false,
'serializer' => 0,
);

private $redis;

/**
Expand Down Expand Up @@ -65,6 +67,7 @@ private function init($redisClient, $namespace = '', $defaultLifetime = 0)
* - redis://secret@example.com/13
* - redis:///var/run/redis.sock
* - redis://secret@/var/run/redis.sock/13
* - redis://localhost?serializer=igbinary
*
* @param string $dsn
* @param array $options See self::$defaultConnectionOptions
Expand Down Expand Up @@ -116,6 +119,13 @@ public static function createConnection($dsn, array $options = array())
$class = null === $params['class'] ? (extension_loaded('redis') ? \Redis::class : \Predis\Client::class) : $params['class'];

if (is_a($class, \Redis::class, true)) {
if (isset($params['serializer']) && array_key_exists($params['serializer'], $serializers = [
'none' => \Redis::SERIALIZER_NONE,
'php' => \Redis::SERIALIZER_PHP,
'igbinary' => \Redis::SERIALIZER_IGBINARY,
])) {
$params['serializer'] = $serializers[$params['serializer']];
}
$connect = $params['persistent'] || $params['persistent_id'] ? 'pconnect' : 'connect';
$redis = new $class();

Expand All @@ -137,6 +147,7 @@ public static function createConnection($dsn, array $options = array())
if ((null !== $auth && !$redis->auth($auth))
|| ($params['dbindex'] && !$redis->select($params['dbindex']))
|| ($params['read_timeout'] && !$redis->setOption(\Redis::OPT_READ_TIMEOUT, $params['read_timeout']))
|| ($params['serializer'] && !$redis->setOption(\Redis::OPT_SERIALIZER, $params['serializer']))
) {
$e = preg_replace('/^ERR /', '', $redis->getLastError());
throw new InvalidArgumentException(sprintf('Redis connection failed (%s): %s', $e, $dsn));
Expand Down
0