Skip to content

Commit 3a85e5b

Browse files
authored
RedisStorage: Introduced support of php-redis module. Fixed clear() method (php-debugbar#480)
* RedisStorage: Introduced support of php-redis module. Fixed clear() method * Fixed sorting order for RedisStorage to show newest requests on top * Revert BC breaking changes
1 parent 0a35325 commit 3a85e5b

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

src/DebugBar/Storage/RedisStorage.php

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@
1515
*/
1616
class RedisStorage implements StorageInterface
1717
{
18+
/** @var \Predis\Client|\Redis */
1819
protected $redis;
1920

21+
/** @var string */
2022
protected $hash;
2123

2224
/**
23-
* @param \Predis\Client $redis Redis Client
24-
* @param string $hash
25+
* @param \Predis\Client|\Redis $redis Redis Client
26+
* @param string $hash
2527
*/
2628
public function __construct($redis, $hash = 'phpdebugbar')
2729
{
@@ -34,29 +36,35 @@ public function __construct($redis, $hash = 'phpdebugbar')
3436
*/
3537
public function save($id, $data)
3638
{
37-
$this->redis->hset("$this->hash:meta", $id, serialize($data['__meta']));
39+
$this->redis->hSet("$this->hash:meta", $id, serialize($data['__meta']));
3840
unset($data['__meta']);
39-
$this->redis->hset("$this->hash:data", $id, serialize($data));
41+
$this->redis->hSet("$this->hash:data", $id, serialize($data));
4042
}
4143

4244
/**
4345
* {@inheritdoc}
4446
*/
4547
public function get($id)
4648
{
47-
return array_merge(unserialize($this->redis->hget("$this->hash:data", $id)),
48-
array('__meta' => unserialize($this->redis->hget("$this->hash:meta", $id))));
49+
return array_merge(unserialize($this->redis->hGet("$this->hash:data", $id)),
50+
array('__meta' => unserialize($this->redis->hGet("$this->hash:meta", $id))));
4951
}
5052

5153
/**
5254
* {@inheritdoc}
5355
*/
54-
public function find(array $filters = array(), $max = 20, $offset = 0)
56+
public function find(array $filters = [], $max = 20, $offset = 0)
5557
{
56-
$results = array();
58+
$results = [];
5759
$cursor = "0";
60+
$isPhpRedis = get_class($this->redis) === 'Redis';
61+
5862
do {
59-
list($cursor, $data) = $this->redis->hscan("$this->hash:meta", $cursor);
63+
if ($isPhpRedis) {
64+
$data = $this->redis->hScan("$this->hash:meta", $cursor);
65+
} else {
66+
[$cursor, $data] = $this->redis->hScan("$this->hash:meta", $cursor);
67+
}
6068

6169
foreach ($data as $meta) {
6270
if ($meta = unserialize($meta)) {
@@ -66,11 +74,11 @@ public function find(array $filters = array(), $max = 20, $offset = 0)
6674
}
6775
}
6876
} while($cursor);
69-
70-
usort($results, function ($a, $b) {
71-
return $a['utime'] < $b['utime'];
77+
78+
usort($results, static function ($a, $b) {
79+
return $b['utime'] <=> $a['utime'];
7280
});
73-
81+
7482
return array_slice($results, $offset, $max);
7583
}
7684

@@ -92,6 +100,7 @@ protected function filter($meta, $filters)
92100
*/
93101
public function clear()
94102
{
95-
$this->redis->del($this->hash);
103+
$this->redis->del("$this->hash:data");
104+
$this->redis->del("$this->hash:meta");
96105
}
97106
}

0 commit comments

Comments
 (0)