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
|
16 |
|
public function zAdd($zkey, $score, $value) |
19
|
|
|
{ |
20
|
16 |
|
return $this->predis->zadd($zkey, [$value => $score]); |
21
|
|
|
} |
22
|
|
|
|
23
|
16 |
|
public function set($key, $value) |
24
|
|
|
{ |
25
|
16 |
|
return $this->predis->set($key, $value); |
26
|
|
|
} |
27
|
|
|
|
28
|
10 |
|
public function get($key) |
29
|
|
|
{ |
30
|
10 |
|
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
|
12 |
|
public function lRem($lKey, $count, $value) |
39
|
|
|
{ |
40
|
12 |
|
return $this->predis->lrem($lKey, $count, $value); |
41
|
|
|
} |
42
|
|
|
|
43
|
16 |
|
public function lPush($lKey, array $values) |
44
|
|
|
{ |
45
|
16 |
|
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
|
12 |
|
public function del(array $keys) |
54
|
|
|
{ |
55
|
12 |
|
return $this->predis->del($keys); |
56
|
|
|
} |
57
|
|
|
|
58
|
2 |
|
public function zRem($zkey, $value) |
59
|
|
|
{ |
60
|
2 |
|
return $this->predis->zrem($zkey, $value); |
61
|
|
|
} |
62
|
|
|
|
63
|
12 |
|
public function zPop($key) |
64
|
|
|
{ |
65
|
12 |
|
$element = null; |
66
|
|
|
$options = [ |
67
|
12 |
|
'cas' => true, |
68
|
12 |
|
'watch' => $key, |
69
|
12 |
|
'retry' => $this->maxRetries, |
70
|
|
|
]; |
71
|
|
|
|
72
|
|
|
try { |
73
|
12 |
|
$this->predis->transaction($options, function ($tx) use ($key, &$element) { |
74
|
12 |
|
@list($element) = $tx->zrange($key, 0, 0); |
|
|
|
|
75
|
|
|
|
76
|
12 |
|
if (isset($element)) { |
77
|
10 |
|
$tx->multi(); |
78
|
10 |
|
$tx->zrem($key, $element); |
79
|
|
|
} |
80
|
12 |
|
}); |
81
|
|
|
} catch (\Exception $exception) { |
82
|
|
|
return null; |
83
|
|
|
} |
84
|
|
|
|
85
|
12 |
|
return $element; |
86
|
|
|
} |
87
|
|
|
|
88
|
12 |
|
public function zPopByMaxScore($key, $max) |
89
|
|
|
{ |
90
|
12 |
|
$element = null; |
91
|
|
|
$options = [ |
92
|
12 |
|
'cas' => true, |
93
|
12 |
|
'watch' => $key, |
94
|
12 |
|
'retry' => $this->maxRetries, |
95
|
|
|
]; |
96
|
|
|
|
97
|
|
|
try { |
98
|
12 |
|
$this->predis->transaction($options, function ($tx) use ($key, $max, &$element) { |
99
|
12 |
|
@list($element) = $tx->zrangebyscore($key, 0, $max, ['LIMIT' => [0, 1]]); |
|
|
|
|
100
|
|
|
|
101
|
12 |
|
if (isset($element)) { |
102
|
10 |
|
$tx->multi(); |
103
|
10 |
|
$tx->zrem($key, $element); |
104
|
|
|
} |
105
|
12 |
|
}); |
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: