Skip to content

Commit

Permalink
RedisStorage performance fix (#357)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ilnicki authored and barryvdh committed Sep 13, 2017
1 parent 9640a66 commit 64251a3
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/DebugBar/Storage/RedisStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,18 @@ 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));
}

/**
* {@inheritdoc}
*/
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))));
}

/**
Expand All @@ -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'];
Expand Down

0 comments on commit 64251a3

Please sign in to comment.