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
|
1 |
|
public function zCount($key, $min, $max) |
17
|
|
|
{ |
18
|
1 |
|
return $this->redis->zCount($key, $min, $max); |
19
|
|
|
} |
20
|
|
|
|
21
|
1 |
|
public function zAdd($zkey, $score, $value) |
22
|
|
|
{ |
23
|
1 |
|
return $this->redis->zadd($zkey, $score, $value); |
24
|
|
|
} |
25
|
|
|
|
26
|
1 |
|
public function set($key, $value) |
27
|
|
|
{ |
28
|
1 |
|
return $this->redis->set($key, $value); |
29
|
|
|
} |
30
|
|
|
|
31
|
1 |
|
public function get($key) |
32
|
|
|
{ |
33
|
1 |
|
return $this->redis->get($key); |
34
|
|
|
} |
35
|
|
|
|
36
|
1 |
|
public function setEx($key, $seconds, $value) |
37
|
|
|
{ |
38
|
1 |
|
return $this->redis->setex($key, $seconds, $value); |
39
|
|
|
} |
40
|
|
|
|
41
|
1 |
|
public function lRem($lKey, $count, $value) |
42
|
|
|
{ |
43
|
1 |
|
return $this->redis->lrem($lKey, $value, $count); |
44
|
|
|
} |
45
|
|
|
|
46
|
1 |
|
public function lPush($lKey, array $values) |
47
|
|
|
{ |
48
|
1 |
|
$args = $values; |
49
|
1 |
|
array_unshift($args, $lKey); |
50
|
|
|
|
51
|
1 |
|
return call_user_func_array([$this->redis, 'lPush'], $args); |
52
|
|
|
} |
53
|
|
|
|
54
|
1 |
|
public function lRange($lKey, $start, $stop) |
55
|
|
|
{ |
56
|
1 |
|
return $this->redis->lrange($lKey, $start, $stop); |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
public function del(array $keys) |
60
|
|
|
{ |
61
|
1 |
|
return $this->redis->del($keys); |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
public function zRem($zkey, $value) |
65
|
|
|
{ |
66
|
1 |
|
return $this->redis->zrem($zkey, $value); |
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
public function zPop($key) |
70
|
|
|
{ |
71
|
1 |
|
$retries = 0; |
72
|
|
|
do { |
73
|
1 |
|
$this->redis->watch($key); |
74
|
1 |
|
$elements = $this->redis->zrange($key, 0, 0); |
75
|
1 |
|
if (empty($elements)) { |
76
|
|
|
$this->redis->unwatch(); |
77
|
|
|
|
78
|
|
|
return null; |
79
|
|
|
} |
80
|
1 |
|
$result = $this->redis->multi() |
81
|
1 |
|
->zrem($key, $elements[0]) |
82
|
1 |
|
->exec(); |
83
|
1 |
|
if (false !== $result) { |
84
|
1 |
|
return $elements[0]; |
85
|
|
|
} |
86
|
|
|
++$retries; |
87
|
|
|
} while ($retries < $this->maxRetries); |
88
|
|
|
|
89
|
|
|
return null; |
90
|
|
|
} |
91
|
|
|
|
92
|
1 |
|
public function zPopByMaxScore($key, $max) |
93
|
|
|
{ |
94
|
1 |
|
$retries = 0; |
95
|
|
|
do { |
96
|
1 |
|
$this->redis->watch($key); |
97
|
1 |
|
$elements = $this->redis->zrangebyscore($key, 0, $max, ['limit' => [0, 1]]); |
98
|
1 |
|
if (empty($elements)) { |
99
|
1 |
|
$this->redis->unwatch(); |
100
|
|
|
|
101
|
1 |
|
return null; |
102
|
|
|
} |
103
|
1 |
|
$result = $this->redis->multi() |
104
|
1 |
|
->zrem($key, $elements[0]) |
105
|
1 |
|
->exec(); |
106
|
1 |
|
if (false !== $result) { |
107
|
1 |
|
return $elements[0]; |
108
|
|
|
} |
109
|
|
|
++$retries; |
110
|
|
|
} while ($retries < $this->maxRetries); |
111
|
|
|
|
112
|
|
|
return null; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|