1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dtc\QueueBundle\Redis; |
4
|
|
|
|
5
|
|
|
class PhpRedis implements RedisInterface |
6
|
|
|
{ |
7
|
|
|
protected $redis; |
8
|
|
|
protected $maxRetries; |
9
|
|
|
|
10
|
1 |
|
public function __construct(\Redis $redis, $maxRetries = 5) |
11
|
|
|
{ |
12
|
1 |
|
$this->redis = $redis; |
13
|
1 |
|
$this->maxRetries = $maxRetries; |
14
|
1 |
|
} |
15
|
|
|
|
16
|
4 |
|
public function mGet(array $keys) |
17
|
|
|
{ |
18
|
4 |
|
return $this->redis->mGet($keys); |
19
|
|
|
} |
20
|
|
|
|
21
|
4 |
|
public function hScan($key, &$cursor, $pattern = '', $count = 0) |
22
|
|
|
{ |
23
|
4 |
|
return $this->redis->hScan($key, $cursor, $pattern, $count); |
24
|
|
|
} |
25
|
|
|
|
26
|
4 |
|
public function zScan($key, &$cursor, $pattern = '', $count = 0) |
27
|
|
|
{ |
28
|
4 |
|
return $this->redis->zScan($key, $cursor, $pattern, $count); |
29
|
|
|
} |
30
|
|
|
|
31
|
4 |
|
public function zCount($key, $min, $max) |
32
|
|
|
{ |
33
|
4 |
|
return $this->redis->zCount($key, $min, $max); |
34
|
|
|
} |
35
|
|
|
|
36
|
29 |
|
public function zAdd($zkey, $score, $value) |
37
|
|
|
{ |
38
|
29 |
|
return $this->redis->zadd($zkey, $score, $value); |
39
|
|
|
} |
40
|
|
|
|
41
|
29 |
|
public function set($key, $value) |
42
|
|
|
{ |
43
|
29 |
|
return $this->redis->set($key, $value); |
44
|
|
|
} |
45
|
|
|
|
46
|
21 |
|
public function get($key) |
47
|
|
|
{ |
48
|
21 |
|
return $this->redis->get($key); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
13 |
|
public function hIncrBy($key, $hashKey, $value) |
52
|
|
|
{ |
53
|
13 |
|
return $this->redis->hIncrBy($key, $hashKey, $value); |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
public function hGetAll($key) |
57
|
|
|
{ |
58
|
1 |
|
return $this->redis->hGetAll($key); |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
public function setEx($key, $seconds, $value) |
62
|
|
|
{ |
63
|
1 |
|
return $this->redis->setex($key, $seconds, $value); |
64
|
|
|
} |
65
|
|
|
|
66
|
24 |
|
public function lRem($lKey, $count, $value) |
67
|
|
|
{ |
68
|
24 |
|
return $this->redis->lrem($lKey, $value, $count); |
69
|
|
|
} |
70
|
|
|
|
71
|
29 |
|
public function lPush($lKey, array $values) |
72
|
|
|
{ |
73
|
29 |
|
$args = $values; |
74
|
29 |
|
array_unshift($args, $lKey); |
75
|
|
|
|
76
|
29 |
|
return call_user_func_array([$this->redis, 'lPush'], $args); |
77
|
|
|
} |
78
|
|
|
|
79
|
4 |
|
public function lRange($lKey, $start, $stop) |
80
|
|
|
{ |
81
|
4 |
|
return $this->redis->lrange($lKey, $start, $stop); |
82
|
|
|
} |
83
|
|
|
|
84
|
24 |
|
public function del(array $keys) |
85
|
|
|
{ |
86
|
24 |
|
return $this->redis->del($keys); |
87
|
|
|
} |
88
|
|
|
|
89
|
7 |
|
public function zRem($zkey, $value) |
90
|
|
|
{ |
91
|
7 |
|
return $this->redis->zrem($zkey, $value); |
92
|
|
|
} |
93
|
|
|
|
94
|
10 |
|
public function zPop($key) |
95
|
|
|
{ |
96
|
10 |
|
$retries = 0; |
97
|
|
|
do { |
98
|
10 |
|
$this->redis->watch($key); |
99
|
10 |
|
$elements = $this->redis->zrange($key, 0, 0); |
100
|
10 |
|
if (empty($elements)) { |
101
|
8 |
|
$this->redis->unwatch(); |
102
|
|
|
|
103
|
8 |
|
return null; |
104
|
|
|
} |
105
|
9 |
|
$result = $this->redis->multi() |
106
|
9 |
|
->zrem($key, $elements[0]) |
107
|
9 |
|
->exec(); |
|
|
|
|
108
|
9 |
|
if (false !== $result) { |
109
|
9 |
|
return $elements[0]; |
110
|
|
|
} |
111
|
|
|
++$retries; |
112
|
|
|
} while ($retries < $this->maxRetries); |
113
|
|
|
|
114
|
|
|
return null; |
115
|
|
|
} |
116
|
|
|
|
117
|
24 |
|
public function zPopByMaxScore($key, $max) |
118
|
|
|
{ |
119
|
24 |
|
$retries = 0; |
120
|
|
|
do { |
121
|
24 |
|
$this->redis->watch($key); |
122
|
24 |
|
$elements = $this->redis->zrangebyscore($key, 0, $max, ['limit' => [0, 1]]); |
123
|
24 |
|
if (empty($elements)) { |
124
|
24 |
|
$this->redis->unwatch(); |
125
|
|
|
|
126
|
24 |
|
return null; |
127
|
|
|
} |
128
|
21 |
|
$result = $this->redis->multi() |
129
|
21 |
|
->zrem($key, $elements[0]) |
130
|
21 |
|
->exec(); |
131
|
21 |
|
if (false !== $result) { |
132
|
21 |
|
return $elements[0]; |
133
|
|
|
} |
134
|
|
|
++$retries; |
135
|
|
|
} while ($retries < $this->maxRetries); |
136
|
|
|
|
137
|
|
|
return null; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|