15
15
*/
16
16
class RedisStorage implements StorageInterface
17
17
{
18
+ /** @var \Predis\Client|\Redis */
18
19
protected $ redis ;
19
20
21
+ /** @var string */
20
22
protected $ hash ;
21
23
22
24
/**
23
- * @param \Predis\Client $redis Redis Client
24
- * @param string $hash
25
+ * @param \Predis\Client|\Redis $redis Redis Client
26
+ * @param string $hash
25
27
*/
26
28
public function __construct ($ redis , $ hash = 'phpdebugbar ' )
27
29
{
@@ -34,29 +36,35 @@ public function __construct($redis, $hash = 'phpdebugbar')
34
36
*/
35
37
public function save ($ id , $ data )
36
38
{
37
- $ this ->redis ->hset ("$ this ->hash :meta " , $ id , serialize ($ data ['__meta ' ]));
39
+ $ this ->redis ->hSet ("$ this ->hash :meta " , $ id , serialize ($ data ['__meta ' ]));
38
40
unset($ data ['__meta ' ]);
39
- $ this ->redis ->hset ("$ this ->hash :data " , $ id , serialize ($ data ));
41
+ $ this ->redis ->hSet ("$ this ->hash :data " , $ id , serialize ($ data ));
40
42
}
41
43
42
44
/**
43
45
* {@inheritdoc}
44
46
*/
45
47
public function get ($ id )
46
48
{
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 ))));
49
51
}
50
52
51
53
/**
52
54
* {@inheritdoc}
53
55
*/
54
- public function find (array $ filters = array () , $ max = 20 , $ offset = 0 )
56
+ public function find (array $ filters = [] , $ max = 20 , $ offset = 0 )
55
57
{
56
- $ results = array () ;
58
+ $ results = [] ;
57
59
$ cursor = "0 " ;
60
+ $ isPhpRedis = get_class ($ this ->redis ) === 'Redis ' ;
61
+
58
62
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
+ }
60
68
61
69
foreach ($ data as $ meta ) {
62
70
if ($ meta = unserialize ($ meta )) {
@@ -66,11 +74,11 @@ public function find(array $filters = array(), $max = 20, $offset = 0)
66
74
}
67
75
}
68
76
} 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 ' ];
72
80
});
73
-
81
+
74
82
return array_slice ($ results , $ offset , $ max );
75
83
}
76
84
@@ -92,6 +100,7 @@ protected function filter($meta, $filters)
92
100
*/
93
101
public function clear ()
94
102
{
95
- $ this ->redis ->del ($ this ->hash );
103
+ $ this ->redis ->del ("$ this ->hash :data " );
104
+ $ this ->redis ->del ("$ this ->hash :meta " );
96
105
}
97
106
}
0 commit comments