[Cache] Cleanups

This commit is contained in:
Nicolas Grekas 2016-05-02 14:16:25 +02:00
parent 8166094dc2
commit 9a56498c8d
3 changed files with 40 additions and 34 deletions

View File

@ -149,7 +149,7 @@ abstract class AbstractAdapter implements AdapterInterface, LoggerAwareInterface
$ids = array(); $ids = array();
foreach ($keys as $key) { foreach ($keys as $key) {
$ids[$key] = $this->getId($key); $ids[] = $this->getId($key);
} }
try { try {
$items = $this->doFetch($ids); $items = $this->doFetch($ids);
@ -157,7 +157,7 @@ abstract class AbstractAdapter implements AdapterInterface, LoggerAwareInterface
CacheItem::log($this->logger, 'Failed to fetch requested items', array('keys' => $keys, 'exception' => $e)); CacheItem::log($this->logger, 'Failed to fetch requested items', array('keys' => $keys, 'exception' => $e));
$items = array(); $items = array();
} }
$ids = array_flip($ids); $ids = array_combine($ids, $keys);
return $this->generateItems($items, $ids); return $this->generateItems($items, $ids);
} }

View File

@ -27,14 +27,14 @@ class RedisAdapter extends AbstractAdapter
); );
private $redis; private $redis;
public function __construct(\Redis $redisConnection, $namespace = '', $defaultLifetime = 0) public function __construct(\Redis $redisClient, $namespace = '', $defaultLifetime = 0)
{ {
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]));
} }
$this->redis = $redisConnection; $this->redis = $redisClient;
parent::__construct($namespace, $defaultLifetime);
} }
/** /**
@ -86,26 +86,29 @@ class RedisAdapter extends AbstractAdapter
$params += $query; $params += $query;
} }
$params += $options + self::$defaultConnectionOptions; $params += $options + self::$defaultConnectionOptions;
$class = $params['class'];
if (\Redis::class !== $params['class'] && !is_subclass_of($params['class'], \Redis::class)) { if (is_a($class, \Redis::class, true)) {
throw new InvalidArgumentException(sprintf('"%s" is not a subclass of "Redis"', $params['class'])); $connect = empty($params['persistent']) ? 'connect' : 'pconnect';
} $redis = new $class();
$connect = empty($params['persistent']) ? 'connect' : 'pconnect'; @$redis->{$connect}($params['host'], $params['port'], $params['timeout'], null, $params['retry_interval']);
$redis = new $params['class']();
@$redis->{$connect}($params['host'], $params['port'], $params['timeout'], null, $params['retry_interval']);
if (@!$redis->isConnected()) { if (@!$redis->isConnected()) {
$e = ($e = error_get_last()) && preg_match('/^Redis::p?connect\(\): (.*)/', $e['message'], $e) ? sprintf(' (%s)', $e[1]) : ''; $e = ($e = error_get_last()) && preg_match('/^Redis::p?connect\(\): (.*)/', $e['message'], $e) ? sprintf(' (%s)', $e[1]) : '';
throw new InvalidArgumentException(sprintf('Redis connection failed%s: %s', $e, $dsn)); throw new InvalidArgumentException(sprintf('Redis connection failed%s: %s', $e, $dsn));
} }
if ((null !== $auth && !$redis->auth($auth)) if ((null !== $auth && !$redis->auth($auth))
|| ($params['dbindex'] && !$redis->select($params['dbindex'])) || ($params['dbindex'] && !$redis->select($params['dbindex']))
|| ($params['read_timeout'] && !$redis->setOption(\Redis::OPT_READ_TIMEOUT, $params['read_timeout'])) || ($params['read_timeout'] && !$redis->setOption(\Redis::OPT_READ_TIMEOUT, $params['read_timeout']))
) { ) {
$e = preg_replace('/^ERR /', '', $redis->getLastError()); $e = preg_replace('/^ERR /', '', $redis->getLastError());
$redis->close(); throw new InvalidArgumentException(sprintf('Redis connection failed (%s): %s', $e, $dsn));
throw new InvalidArgumentException(sprintf('Redis connection failed (%s): %s', $e, $dsn)); }
} elseif (class_exists($class, false)) {
throw new InvalidArgumentException(sprintf('"%s" is not a subclass of "Redis"', $class));
} else {
throw new InvalidArgumentException(sprintf('Class "%s" does not exist', $class));
} }
return $redis; return $redis;
@ -119,10 +122,10 @@ class RedisAdapter extends AbstractAdapter
$result = array(); $result = array();
if ($ids) { if ($ids) {
$values = $this->redis->mget($ids); $values = $this->redis->mGet($ids);
$index = 0; $index = 0;
foreach ($ids as $id) { foreach ($ids as $id) {
if (false !== $value = $values[$index++]) { if ($value = $values[$index++]) {
$result[$id] = unserialize($value); $result[$id] = unserialize($value);
} }
} }
@ -144,14 +147,16 @@ class RedisAdapter extends AbstractAdapter
*/ */
protected function doClear($namespace) protected function doClear($namespace)
{ {
// 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.
$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])) { if (!isset($namespace[0])) {
$this->redis->flushDB(); $this->redis->flushDb();
} else { } else {
// As documented in Redis documentation (http://redis.io/commands/keys) using KEYS $this->redis->eval($lua, array($namespace), 0);
// 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 the above FLUSHDB is used instead.
$this->redis->eval(sprintf("local keys=redis.call('KEYS','%s*') for i=1,#keys,5000 do redis.call('DEL',unpack(keys,i,math.min(i+4999,#keys))) end", $namespace));
} }
return true; return true;
@ -189,11 +194,11 @@ class RedisAdapter extends AbstractAdapter
return $failed; return $failed;
} }
if ($lifetime > 0) { if ($lifetime > 0) {
$pipe = $this->redis->multi(\Redis::PIPELINE); $this->redis->multi(\Redis::PIPELINE);
foreach ($serialized as $id => $value) { foreach ($serialized as $id => $value) {
$pipe->setEx($id, $lifetime, $value); $this->redis->setEx($id, $lifetime, $value);
} }
if (!$pipe->exec()) { if (!$this->redis->exec()) {
return false; return false;
} }
} elseif (!$this->redis->mSet($serialized)) { } elseif (!$this->redis->mSet($serialized)) {

View File

@ -48,6 +48,7 @@ class RedisAdapterTest extends CachePoolTest
public function testCreateConnection() public function testCreateConnection()
{ {
$redis = RedisAdapter::createConnection('redis://localhost'); $redis = RedisAdapter::createConnection('redis://localhost');
$this->assertInstanceOf(\Redis::class, $redis);
$this->assertTrue($redis->isConnected()); $this->assertTrue($redis->isConnected());
$this->assertSame(0, $redis->getDbNum()); $this->assertSame(0, $redis->getDbNum());