Skip to content

Commit

Permalink
Fix getItems() performance issue with RedisCluster (php-redis)
Browse files Browse the repository at this point in the history
On any kind of multi loads, including tags loading, current code leads to
an explosion of Redis lookups slowing down performance.

This backports the code for mget() usage from 4.x in order to fix it.
It's done with one small improvment which would also be relevant for 4.x,
only using pipeline on cluster on predis as mget is more efficient.
  • Loading branch information
andrerom committed Feb 22, 2019
1 parent 5ad1f37 commit 178506e
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/Symfony/Component/Cache/Traits/RedisTrait.php
Expand Up @@ -169,18 +169,29 @@ public static function createConnection($dsn, array $options = [])
*/
protected function doFetch(array $ids)
{
if ($ids) {
if (!$ids) {
return [];
}

$result = [];

if ($this->redis instanceof \Predis\Client && $this->redis->getConnection() instanceof ClusterInterface) {
$values = $this->pipeline(function () use ($ids) {
foreach ($ids as $id) {
yield 'get' => [$id];
}
});
foreach ($values as $id => $v) {
if ($v) {
yield $id => parent::unserialize($v);
}
} else {
$values = array_combine($ids, $this->redis->mget($ids));
}

foreach ($values as $id => $v) {
if ($v) {
$result[$id] = parent::unserialize($v);
}
}

return $result;
}

/**
Expand Down

0 comments on commit 178506e

Please sign in to comment.