1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dtc\QueueBundle\Redis; |
4
|
|
|
|
5
|
|
|
use Predis\Client; |
6
|
|
|
use Predis\Response\Status; |
7
|
|
|
|
8
|
|
|
class Predis implements RedisInterface |
9
|
|
|
{ |
10
|
|
|
protected $predis; |
11
|
|
|
protected $maxRetries; |
12
|
|
|
|
13
|
1 |
|
public function __construct(Client $predis, $maxRetries = 5) |
14
|
|
|
{ |
15
|
1 |
|
$this->predis = $predis; |
16
|
1 |
|
$this->maxRetries = $maxRetries; |
17
|
1 |
|
} |
18
|
|
|
|
19
|
19 |
|
public function zAdd($zkey, $score, $value) |
20
|
|
|
{ |
21
|
19 |
|
return $this->predis->zadd($zkey, [$value => $score]); |
22
|
|
|
} |
23
|
|
|
|
24
|
19 |
View Code Duplication |
public function set($key, $value) |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
/** @var Status $result */ |
27
|
19 |
|
$result = $this->predis->set($key, $value); |
28
|
19 |
|
if ('OK' == $result->getPayload()) { |
29
|
19 |
|
return true; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
return false; |
33
|
|
|
} |
34
|
|
|
|
35
|
13 |
|
public function get($key) |
36
|
|
|
{ |
37
|
13 |
|
$this->predis->multi(); |
38
|
13 |
|
$this->predis->exists($key); |
39
|
13 |
|
$this->predis->get($key); |
40
|
13 |
|
list($exists, $result) = $this->predis->exec(); |
41
|
13 |
|
if (!$exists) { |
42
|
1 |
|
return false; |
43
|
|
|
} |
44
|
|
|
|
45
|
13 |
|
return $result; |
46
|
|
|
} |
47
|
|
|
|
48
|
1 |
View Code Duplication |
public function setEx($key, $seconds, $value) |
|
|
|
|
49
|
|
|
{ |
50
|
1 |
|
$result = $this->predis->setex($key, $seconds, $value); |
51
|
1 |
|
if ('OK' == $result->getPayload()) { |
52
|
1 |
|
return true; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return false; |
56
|
|
|
} |
57
|
|
|
|
58
|
15 |
|
public function lRem($lKey, $count, $value) |
59
|
|
|
{ |
60
|
15 |
|
return $this->predis->lrem($lKey, $count, $value); |
61
|
|
|
} |
62
|
|
|
|
63
|
19 |
|
public function lPush($lKey, array $values) |
64
|
|
|
{ |
65
|
19 |
|
return $this->predis->lpush($lKey, $values); |
66
|
|
|
} |
67
|
|
|
|
68
|
3 |
|
public function lRange($lKey, $start, $stop) |
69
|
|
|
{ |
70
|
3 |
|
return $this->predis->lrange($lKey, $start, $stop); |
71
|
|
|
} |
72
|
|
|
|
73
|
15 |
|
public function del(array $keys) |
74
|
|
|
{ |
75
|
15 |
|
return $this->predis->del($keys); |
76
|
|
|
} |
77
|
|
|
|
78
|
5 |
|
public function zRem($zkey, $value) |
79
|
|
|
{ |
80
|
5 |
|
return $this->predis->zrem($zkey, $value); |
81
|
|
|
} |
82
|
|
|
|
83
|
15 |
|
public function zPop($key) |
84
|
|
|
{ |
85
|
15 |
|
$element = null; |
86
|
|
|
$options = [ |
87
|
15 |
|
'cas' => true, |
88
|
15 |
|
'watch' => $key, |
89
|
15 |
|
'retry' => $this->maxRetries, |
90
|
|
|
]; |
91
|
|
|
|
92
|
|
|
try { |
93
|
15 |
|
$this->predis->transaction($options, function($tx) use ($key, &$element) { |
94
|
15 |
|
@list($element) = $tx->zrange($key, 0, 0); |
|
|
|
|
95
|
|
|
|
96
|
15 |
|
if (isset($element)) { |
97
|
13 |
|
$tx->multi(); |
98
|
13 |
|
$tx->zrem($key, $element); |
99
|
|
|
} |
100
|
15 |
|
}); |
101
|
|
|
} catch (\Exception $exception) { |
102
|
|
|
return null; |
103
|
|
|
} |
104
|
|
|
|
105
|
15 |
|
return $element; |
106
|
|
|
} |
107
|
|
|
|
108
|
15 |
|
public function zPopByMaxScore($key, $max) |
109
|
|
|
{ |
110
|
15 |
|
$element = null; |
111
|
|
|
$options = [ |
112
|
15 |
|
'cas' => true, |
113
|
15 |
|
'watch' => $key, |
114
|
15 |
|
'retry' => $this->maxRetries, |
115
|
|
|
]; |
116
|
|
|
|
117
|
|
|
try { |
118
|
15 |
|
$this->predis->transaction($options, function($tx) use ($key, $max, &$element) { |
119
|
15 |
|
@list($element) = $tx->zrangebyscore($key, 0, $max, ['LIMIT' => [0, 1]]); |
|
|
|
|
120
|
|
|
|
121
|
15 |
|
if (isset($element)) { |
122
|
13 |
|
$tx->multi(); |
123
|
13 |
|
$tx->zrem($key, $element); |
124
|
|
|
} |
125
|
15 |
|
}); |
126
|
|
|
} catch (\Exception $exception) { |
127
|
|
|
return null; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $element; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.