From 178506e72ce57c030fca3a06043666525c41bff3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andre=CC=81=20R?= Date: Fri, 22 Feb 2019 22:08:54 +0100 Subject: [PATCH] Fix getItems() performance issue with RedisCluster (php-redis) 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. --- .../Component/Cache/Traits/RedisTrait.php | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Component/Cache/Traits/RedisTrait.php b/src/Symfony/Component/Cache/Traits/RedisTrait.php index a637be808003..7cbd304a0cec 100644 --- a/src/Symfony/Component/Cache/Traits/RedisTrait.php +++ b/src/Symfony/Component/Cache/Traits/RedisTrait.php @@ -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; } /**