Skip to content

Commit 64251a3

Browse files
ilnickibarryvdh
authored andcommitted
RedisStorage performance fix (php-debugbar#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.
1 parent 9640a66 commit 64251a3

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src/DebugBar/Storage/RedisStorage.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,18 @@ public function __construct($redis, $hash = 'phpdebugbar')
3434
*/
3535
public function save($id, $data)
3636
{
37-
$this->redis->hset($this->hash, $id, serialize($data));
37+
$this->redis->hset("$this->hash:meta", $id, serialize($data['__meta']));
38+
unset($data['__meta']);
39+
$this->redis->hset("$this->hash:data", $id, serialize($data));
3840
}
3941

4042
/**
4143
* {@inheritdoc}
4244
*/
4345
public function get($id)
4446
{
45-
return unserialize($this->redis->hget($this->hash, $id));
47+
return array_merge(unserialize($this->redis->hget("$this->hash:data", $id)),
48+
array('__meta' => unserialize($this->redis->hget("$this->hash:meta", $id))));
4649
}
4750

4851
/**
@@ -51,14 +54,18 @@ public function get($id)
5154
public function find(array $filters = array(), $max = 20, $offset = 0)
5255
{
5356
$results = array();
54-
foreach ($this->redis->hgetall($this->hash) as $id => $data) {
55-
if ($data = unserialize($data)) {
56-
$meta = $data['__meta'];
57-
if ($this->filter($meta, $filters)) {
58-
$results[] = $meta;
57+
$cursor = "0";
58+
do {
59+
list($cursor, $data) = $this->redis->hscan("$this->hash:meta", $cursor);
60+
61+
foreach ($data as $meta) {
62+
if ($meta = unserialize($meta)) {
63+
if ($this->filter($meta, $filters)) {
64+
$results[] = $meta;
65+
}
5966
}
6067
}
61-
}
68+
} while($cursor);
6269

6370
usort($results, function ($a, $b) {
6471
return $a['utime'] < $b['utime'];

0 commit comments

Comments
 (0)