[Cache] Add support for Predis, RedisArray and RedisCluster

This commit is contained in:
Nicolas Grekas 2016-05-01 19:15:43 +02:00
parent cfcf263e24
commit b004243cb3
7 changed files with 219 additions and 48 deletions

View File

@ -83,6 +83,7 @@
"doctrine/doctrine-bundle": "~1.4", "doctrine/doctrine-bundle": "~1.4",
"monolog/monolog": "~1.11", "monolog/monolog": "~1.11",
"ocramius/proxy-manager": "~0.4|~1.0|~2.0", "ocramius/proxy-manager": "~0.4|~1.0|~2.0",
"predis/predis": "~1.0",
"egulias/email-validator": "~1.2", "egulias/email-validator": "~1.2",
"symfony/polyfill-apcu": "~1.1", "symfony/polyfill-apcu": "~1.1",
"symfony/security-acl": "~2.8|~3.0", "symfony/security-acl": "~2.8|~3.0",

View File

@ -11,15 +11,19 @@
namespace Symfony\Component\Cache\Adapter; namespace Symfony\Component\Cache\Adapter;
use Predis\Connection\Factory;
use Predis\Connection\Aggregate\PredisCluster;
use Predis\Connection\Aggregate\RedisCluster;
use Symfony\Component\Cache\Exception\InvalidArgumentException; use Symfony\Component\Cache\Exception\InvalidArgumentException;
/** /**
* @author Aurimas Niekis <aurimas@niekis.lt> * @author Aurimas Niekis <aurimas@niekis.lt>
* @author Nicolas Grekas <p@tchwork.com>
*/ */
class RedisAdapter extends AbstractAdapter class RedisAdapter extends AbstractAdapter
{ {
private static $defaultConnectionOptions = array( private static $defaultConnectionOptions = array(
'class' => \Redis::class, 'class' => null,
'persistent' => 0, 'persistent' => 0,
'timeout' => 0, 'timeout' => 0,
'read_timeout' => 0, 'read_timeout' => 0,
@ -27,13 +31,19 @@ class RedisAdapter extends AbstractAdapter
); );
private $redis; private $redis;
public function __construct(\Redis $redisClient, $namespace = '', $defaultLifetime = 0) /**
* @param \Redis|\RedisArray|\RedisCluster|\Predis\Client $redisClient
*/
public function __construct($redisClient, $namespace = '', $defaultLifetime = 0)
{ {
parent::__construct($namespace, $defaultLifetime); parent::__construct($namespace, $defaultLifetime);
if (preg_match('#[^-+_.A-Za-z0-9]#', $namespace, $match)) { if (preg_match('#[^-+_.A-Za-z0-9]#', $namespace, $match)) {
throw new InvalidArgumentException(sprintf('RedisAdapter namespace contains "%s" but only characters in [-+_.A-Za-z0-9] are allowed.', $match[0])); throw new InvalidArgumentException(sprintf('RedisAdapter namespace contains "%s" but only characters in [-+_.A-Za-z0-9] are allowed.', $match[0]));
} }
if (!$redisClient instanceof \Redis && !$redisClient instanceof \RedisArray && !$redisClient instanceof \RedisCluster && !$redisClient instanceof \Predis\Client) {
throw new InvalidArgumentException(sprintf('%s() expects parameter 1 to be Redis, RedisArray, RedisCluster or Predis\Client, %s given', __METHOD__, is_object($redisClient) ? get_class($redisClient) : gettype($redisClient)));
}
$this->redis = $redisClient; $this->redis = $redisClient;
} }
@ -52,7 +62,7 @@ class RedisAdapter extends AbstractAdapter
* *
* @throws InvalidArgumentException When the DSN is invalid. * @throws InvalidArgumentException When the DSN is invalid.
* *
* @return \Redis * @return \Redis|\Predis\Client According to the "class" option.
*/ */
public static function createConnection($dsn, array $options = array()) public static function createConnection($dsn, array $options = array())
{ {
@ -86,7 +96,7 @@ class RedisAdapter extends AbstractAdapter
$params += $query; $params += $query;
} }
$params += $options + self::$defaultConnectionOptions; $params += $options + self::$defaultConnectionOptions;
$class = $params['class']; $class = null === $params['class'] ? (extension_loaded('redis') ? \Redis::class : \Predis\Client::class) : $params['class'];
if (is_a($class, \Redis::class, true)) { if (is_a($class, \Redis::class, true)) {
$connect = empty($params['persistent']) ? 'connect' : 'pconnect'; $connect = empty($params['persistent']) ? 'connect' : 'pconnect';
@ -105,8 +115,13 @@ class RedisAdapter extends AbstractAdapter
$e = preg_replace('/^ERR /', '', $redis->getLastError()); $e = preg_replace('/^ERR /', '', $redis->getLastError());
throw new InvalidArgumentException(sprintf('Redis connection failed (%s): %s', $e, $dsn)); throw new InvalidArgumentException(sprintf('Redis connection failed (%s): %s', $e, $dsn));
} }
} elseif (is_a($class, \Predis\Client::class, true)) {
$params['scheme'] = isset($params['host']) ? 'tcp' : 'unix';
$params['database'] = $params['dbindex'] ?: null;
$params['password'] = $auth;
$redis = new $class((new Factory())->create($params));
} elseif (class_exists($class, false)) { } elseif (class_exists($class, false)) {
throw new InvalidArgumentException(sprintf('"%s" is not a subclass of "Redis"', $class)); throw new InvalidArgumentException(sprintf('"%s" is not a subclass of "Redis" or "Predis\Client"', $class));
} else { } else {
throw new InvalidArgumentException(sprintf('Class "%s" does not exist', $class)); throw new InvalidArgumentException(sprintf('Class "%s" does not exist', $class));
} }
@ -139,7 +154,7 @@ class RedisAdapter extends AbstractAdapter
*/ */
protected function doHave($id) protected function doHave($id)
{ {
return $this->redis->exists($id); return (bool) $this->redis->exists($id);
} }
/** /**
@ -147,16 +162,41 @@ class RedisAdapter extends AbstractAdapter
*/ */
protected function doClear($namespace) protected function doClear($namespace)
{ {
// As documented in Redis documentation (http://redis.io/commands/keys) using KEYS // When using a native Redis cluster, clearing the cache cannot work and always returns false.
// can hang your server when it is executed against large databases (millions of items). // Clearing the cache should then be done by any other means (e.g. by restarting the cluster).
// Whenever you hit this scale, it is advised to deploy one Redis database per cache pool
// instead of using namespaces, so that FLUSHDB is used instead.
$lua = "local keys=redis.call('KEYS',ARGV[1]..'*') for i=1,#keys,5000 do redis.call('DEL',unpack(keys,i,math.min(i+4999,#keys))) end";
if (!isset($namespace[0])) { $hosts = array($this->redis);
$this->redis->flushDb(); $evalArgs = array(array($namespace), 0);
} else {
$this->redis->eval($lua, array($namespace), 0); if ($this->redis instanceof \Predis\Client) {
$evalArgs = array(0, $namespace);
$connection = $this->redis->getConnection();
if ($connection instanceof PredisCluster) {
$hosts = array();
foreach ($connection as $c) {
$hosts[] = new \Predis\Client($c);
}
} elseif ($connection instanceof RedisCluster) {
return false;
}
} elseif ($this->redis instanceof \RedisArray) {
foreach ($this->redis->_hosts() as $host) {
$hosts[] = $this->redis->_instance($host);
}
} elseif ($this->redis instanceof \RedisCluster) {
return false;
}
foreach ($hosts as $host) {
if (!isset($namespace[0])) {
$host->flushDb();
} else {
// As documented in Redis documentation (http://redis.io/commands/keys) using KEYS
// can hang your server when it is executed against large databases (millions of items).
// Whenever you hit this scale, it is advised to deploy one Redis database per cache pool
// instead of using namespaces, so that FLUSHDB is used instead.
$host->eval("local keys=redis.call('KEYS',ARGV[1]..'*') for i=1,#keys,5000 do redis.call('DEL',unpack(keys,i,math.min(i+4999,#keys))) end", $evalArgs[0], $evalArgs[1]);
}
} }
return true; return true;
@ -194,12 +234,26 @@ class RedisAdapter extends AbstractAdapter
return $failed; return $failed;
} }
if ($lifetime > 0) { if ($lifetime > 0) {
$this->redis->multi(\Redis::PIPELINE); if ($this->redis instanceof \RedisArray) {
foreach ($serialized as $id => $value) { $redis = array();
$this->redis->setEx($id, $lifetime, $value); foreach ($serialized as $id => $value) {
} if (!isset($redis[$h = $this->redis->_target($id)])) {
if (!$this->redis->exec()) { $redis[$h] = $this->redis->_instance($h);
return false; $redis[$h]->multi(\Redis::PIPELINE);
}
$redis[$h]->setEx($id, $lifetime, $value);
}
foreach ($redis as $h) {
if (!$h->exec()) {
$failed = false;
}
}
} else {
$this->pipeline(function ($pipe) use ($serialized, $lifetime) {
foreach ($serialized as $id => $value) {
$pipe->setEx($id, $lifetime, $value);
}
});
} }
} elseif (!$this->redis->mSet($serialized)) { } elseif (!$this->redis->mSet($serialized)) {
return false; return false;
@ -207,4 +261,23 @@ class RedisAdapter extends AbstractAdapter
return $failed; return $failed;
} }
private function pipeline(\Closure $callback)
{
if ($this->redis instanceof \Predis\Client) {
return $this->redis->pipeline($callback);
}
$pipe = $this->redis instanceof \Redis && $this->redis->multi(\Redis::PIPELINE);
try {
$e = null;
$callback($this->redis);
} catch (\Exception $e) {
}
if ($pipe) {
$this->redis->exec();
}
if (null !== $e) {
throw $e;
}
}
} }

View File

@ -0,0 +1,46 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Cache\Tests\Adapter;
use Cache\IntegrationTests\CachePoolTest;
use Symfony\Component\Cache\Adapter\RedisAdapter;
abstract class AbstractRedisAdapterTest extends CachePoolTest
{
protected static $redis;
public function createCachePool()
{
if (defined('HHVM_VERSION')) {
$this->skippedTests['testDeferredSaveWithoutCommit'] = 'Fails on HHVM';
}
return new RedisAdapter(self::$redis, str_replace('\\', '.', __CLASS__));
}
public static function setupBeforeClass()
{
if (!extension_loaded('redis')) {
self::markTestSkipped('Extension redis required.');
}
if (!@((new \Redis())->connect('127.0.0.1'))) {
$e = error_get_last();
self::markTestSkipped($e['message']);
}
}
public static function tearDownAfterClass()
{
self::$redis->flushDB();
self::$redis = null;
}
}

View File

@ -0,0 +1,49 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Cache\Tests\Adapter;
use Predis\Connection\StreamConnection;
use Symfony\Component\Cache\Adapter\RedisAdapter;
class PredisAdapterTest extends AbstractRedisAdapterTest
{
public static function setupBeforeClass()
{
parent::setupBeforeClass();
self::$redis = new \Predis\Client();
}
public function testCreateConnection()
{
$redis = RedisAdapter::createConnection('redis://localhost/1', array('class' => \Predis\Client::class, 'timeout' => 3));
$this->assertInstanceOf(\Predis\Client::class, $redis);
$connection = $redis->getConnection();
$this->assertInstanceOf(StreamConnection::class, $connection);
$params = array(
'scheme' => 'tcp',
'host' => 'localhost',
'path' => '',
'dbindex' => '1',
'port' => 6379,
'class' => 'Predis\Client',
'timeout' => 3,
'persistent' => 0,
'read_timeout' => 0,
'retry_interval' => 0,
'database' => '1',
'password' => null,
);
$this->assertSame($params, $connection->getParameters()->toArray());
}
}

View File

@ -11,38 +11,15 @@
namespace Symfony\Component\Cache\Tests\Adapter; namespace Symfony\Component\Cache\Tests\Adapter;
use Cache\IntegrationTests\CachePoolTest;
use Symfony\Component\Cache\Adapter\RedisAdapter; use Symfony\Component\Cache\Adapter\RedisAdapter;
/** class RedisAdapterTest extends AbstractRedisAdapterTest
* @requires extension redis
*/
class RedisAdapterTest extends CachePoolTest
{ {
private static $redis;
public function createCachePool()
{
if (defined('HHVM_VERSION')) {
$this->skippedTests['testDeferredSaveWithoutCommit'] = 'Fails on HHVM';
}
return new RedisAdapter(self::$redis, str_replace('\\', '.', __CLASS__));
}
public static function setupBeforeClass() public static function setupBeforeClass()
{ {
parent::setupBeforeClass();
self::$redis = new \Redis(); self::$redis = new \Redis();
if (!@self::$redis->connect('127.0.0.1')) { self::$redis->connect('127.0.0.1');
$e = error_get_last();
self::markTestSkipped($e['message']);
}
}
public static function tearDownAfterClass()
{
self::$redis->flushDB();
self::$redis->close();
} }
public function testCreateConnection() public function testCreateConnection()

View File

@ -0,0 +1,24 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Cache\Tests\Adapter;
class RedisArrayAdapterTest extends AbstractRedisAdapterTest
{
public static function setupBeforeClass()
{
parent::setupBeforeClass();
if (!class_exists('RedisArray')) {
self::markTestSkipped('The RedisArray class is required.');
}
self::$redis = new \RedisArray(array('localhost'), array('lazy_connect' => true));
}
}

View File

@ -25,7 +25,8 @@
}, },
"require-dev": { "require-dev": {
"cache/integration-tests": "dev-master", "cache/integration-tests": "dev-master",
"doctrine/cache": "~1.6" "doctrine/cache": "~1.6",
"predis/predis": "~1.0"
}, },
"suggest": { "suggest": {
"symfony/polyfill-apcu": "For using ApcuAdapter on HHVM" "symfony/polyfill-apcu": "For using ApcuAdapter on HHVM"