Skip to content

Commit

Permalink
bug #30351 Fix getItems() performance issue with RedisCluster (php-re…
Browse files Browse the repository at this point in the history
…dis) (andrerom)

This PR was merged into the 3.4 branch.

Discussion
----------

Fix getItems() performance issue with RedisCluster (php-redis)

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | TBD
| License       | MIT

On any kind of multi loads, including tags loading where it's always the case, current code leads to an explosion of Redis lookups affecting performance on RedisCluster _(as it does not support pipeline)_.

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 (ref redis doc).

Commits
-------

178506e Fix getItems() performance issue with RedisCluster (php-redis)
  • Loading branch information
fabpot committed Feb 23, 2019
2 parents 83fec23 + 178506e commit 4cc1006
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 4cc1006

Please sign in to comment.