|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Dtc\QueueBundle\Redis; |
|
4
|
|
|
|
|
5
|
|
|
use Predis\Client; |
|
6
|
|
|
|
|
7
|
|
|
class Predis implements RedisInterface |
|
8
|
|
|
{ |
|
9
|
|
|
protected $predis; |
|
10
|
|
|
protected $maxRetries; |
|
11
|
|
|
|
|
12
|
|
|
public function __construct(Client $predis, $maxRetries = 5) |
|
13
|
|
|
{ |
|
14
|
|
|
$this->predis = $predis; |
|
15
|
|
|
$this->maxRetries = $maxRetries; |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
public function zAdd($zkey, $score, $value) |
|
19
|
|
|
{ |
|
20
|
|
|
return $this->predis->zadd($zkey, [$value => $score]); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function set($key, $value) |
|
24
|
|
|
{ |
|
25
|
|
|
return $this->predis->set($key, $value); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function get($key) |
|
29
|
|
|
{ |
|
30
|
|
|
return $this->predis->get($key); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function setEx($key, $seconds, $value) |
|
34
|
|
|
{ |
|
35
|
|
|
return $this->predis->setex($key, $seconds, $value); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function lRem($lKey, $count, $value) |
|
39
|
|
|
{ |
|
40
|
|
|
return $this->predis->lrem($lKey, $count, $value); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function lPush($lKey, array $values) |
|
44
|
|
|
{ |
|
45
|
|
|
return $this->predis->lpush($lKey, $values); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function lRange($lKey, $start, $stop) |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->predis->lrange($lKey, $start, $stop); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function del(array $keys) |
|
54
|
|
|
{ |
|
55
|
|
|
return $this->predis->del($keys); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function zRem($zkey, $value) |
|
59
|
|
|
{ |
|
60
|
|
|
return $this->predis->zrem($zkey, $value); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function zPop($key) |
|
64
|
|
|
{ |
|
65
|
|
|
$element = null; |
|
66
|
|
|
$options = [ |
|
67
|
|
|
'cas' => true, |
|
68
|
|
|
'watch' => $key, |
|
69
|
|
|
'retry' => $this->maxRetries, |
|
70
|
|
|
]; |
|
71
|
|
|
|
|
72
|
|
|
try { |
|
73
|
|
|
$this->predis->transaction($options, function ($tx) use ($key, &$element) { |
|
74
|
|
|
@list($element) = $tx->zrange($key, 0, 0); |
|
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
if (isset($element)) { |
|
77
|
|
|
$tx->multi(); |
|
78
|
|
|
$tx->zrem($key, $element); |
|
79
|
|
|
} |
|
80
|
|
|
}); |
|
81
|
|
|
} catch (\Exception $exception) { |
|
82
|
|
|
return null; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return $element; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function zPopByMaxScore($key, $max) |
|
89
|
|
|
{ |
|
90
|
|
|
$element = null; |
|
91
|
|
|
$options = [ |
|
92
|
|
|
'cas' => true, |
|
93
|
|
|
'watch' => $key, |
|
94
|
|
|
'retry' => $this->maxRetries, |
|
95
|
|
|
]; |
|
96
|
|
|
|
|
97
|
|
|
try { |
|
98
|
|
|
$this->predis->transaction($options, function ($tx) use ($key, $max, &$element) { |
|
99
|
|
|
@list($element) = $tx->zrangebyscore($key, 0, $max, ['LIMIT' => [0, 1]]); |
|
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
if (isset($element)) { |
|
102
|
|
|
$tx->multi(); |
|
103
|
|
|
$tx->zrem($key, $element); |
|
104
|
|
|
} |
|
105
|
|
|
}); |
|
106
|
|
|
} catch (\Exception $exception) { |
|
107
|
|
|
return null; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return $element; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: