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