From 64251a392344e3d22f3d21c3b7c531ba96eb01d2 Mon Sep 17 00:00:00 2001 From: Dmytro Ilnicki Date: Wed, 13 Sep 2017 15:19:36 +0300 Subject: [PATCH] RedisStorage performance fix (#357) Patch increases possible number of records stored and loaded from Redis DB by separating metadata and collector's data and storing them in different hashes. --- src/DebugBar/Storage/RedisStorage.php | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/DebugBar/Storage/RedisStorage.php b/src/DebugBar/Storage/RedisStorage.php index 311293cd..f1453696 100644 --- a/src/DebugBar/Storage/RedisStorage.php +++ b/src/DebugBar/Storage/RedisStorage.php @@ -34,7 +34,9 @@ public function __construct($redis, $hash = 'phpdebugbar') */ public function save($id, $data) { - $this->redis->hset($this->hash, $id, serialize($data)); + $this->redis->hset("$this->hash:meta", $id, serialize($data['__meta'])); + unset($data['__meta']); + $this->redis->hset("$this->hash:data", $id, serialize($data)); } /** @@ -42,7 +44,8 @@ public function save($id, $data) */ public function get($id) { - return unserialize($this->redis->hget($this->hash, $id)); + return array_merge(unserialize($this->redis->hget("$this->hash:data", $id)), + array('__meta' => unserialize($this->redis->hget("$this->hash:meta", $id)))); } /** @@ -51,14 +54,18 @@ public function get($id) public function find(array $filters = array(), $max = 20, $offset = 0) { $results = array(); - foreach ($this->redis->hgetall($this->hash) as $id => $data) { - if ($data = unserialize($data)) { - $meta = $data['__meta']; - if ($this->filter($meta, $filters)) { - $results[] = $meta; + $cursor = "0"; + do { + list($cursor, $data) = $this->redis->hscan("$this->hash:meta", $cursor); + + foreach ($data as $meta) { + if ($meta = unserialize($meta)) { + if ($this->filter($meta, $filters)) { + $results[] = $meta; + } } } - } + } while($cursor); usort($results, function ($a, $b) { return $a['utime'] < $b['utime'];