Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 178506e

Browse files
committed
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.
1 parent 5ad1f37 commit 178506e

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

src/Symfony/Component/Cache/Traits/RedisTrait.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,18 +169,29 @@ public static function createConnection($dsn, array $options = [])
169169
*/
170170
protected function doFetch(array $ids)
171171
{
172-
if ($ids) {
172+
if (!$ids) {
173+
return [];
174+
}
175+
176+
$result = [];
177+
178+
if ($this->redis instanceof \Predis\Client && $this->redis->getConnection() instanceof ClusterInterface) {
173179
$values = $this->pipeline(function () use ($ids) {
174180
foreach ($ids as $id) {
175181
yield 'get' => [$id];
176182
}
177183
});
178-
foreach ($values as $id => $v) {
179-
if ($v) {
180-
yield $id => parent::unserialize($v);
181-
}
184+
} else {
185+
$values = array_combine($ids, $this->redis->mget($ids));
186+
}
187+
188+
foreach ($values as $id => $v) {
189+
if ($v) {
190+
$result[$id] = parent::unserialize($v);
182191
}
183192
}
193+
194+
return $result;
184195
}
185196

186197
/**

0 commit comments

Comments
 (0)