1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MGDigital\BusQue\Redis\Predis; |
4
|
|
|
|
5
|
|
|
use MGDigital\BusQue\Exception\RedisException; |
6
|
|
|
use MGDigital\BusQue\Redis\RedisAdapterInterface; |
7
|
|
|
use Predis\ClientInterface; |
8
|
|
|
use Predis\Connection\ConnectionException as PredisConnectionException; |
9
|
|
|
|
10
|
|
|
class PredisAdapter implements RedisAdapterInterface |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
private $client; |
14
|
|
|
|
15
|
|
|
public function __construct(ClientInterface $client) |
16
|
|
|
{ |
17
|
|
|
$this->client = $client; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function ping() |
21
|
|
|
{ |
22
|
|
|
try { |
23
|
|
|
$this->client->ping(); |
24
|
|
|
} catch (PredisConnectionException $e) { |
25
|
|
|
throw new RedisException(); |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function bRPopLPush(string $source, string $destination, int $timeout) |
30
|
|
|
{ |
31
|
|
|
try { |
32
|
|
|
return $this->client->brpoplpush($source, $destination, $timeout); |
33
|
|
|
} catch (PredisConnectionException $e) { |
34
|
|
|
throw new RedisException(); |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function hGet(string $key, string $field) |
39
|
|
|
{ |
40
|
|
|
try { |
41
|
|
|
return $this->client->hget($key, $field); |
42
|
|
|
} catch (PredisConnectionException $e) { |
43
|
|
|
throw new RedisException(); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function sAdd(string $key, array $members) |
48
|
|
|
{ |
49
|
|
|
try { |
50
|
|
|
$this->client->sadd($key, $members); |
51
|
|
|
} catch (PredisConnectionException $e) { |
52
|
|
|
throw new RedisException(); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function sRem(string $key, array $members) |
57
|
|
|
{ |
58
|
|
|
try { |
59
|
|
|
$this->client->srem($key, $members); |
60
|
|
|
} catch (PredisConnectionException $e) { |
61
|
|
|
throw new RedisException(); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function sIsMember(string $key, string $value): bool |
66
|
|
|
{ |
67
|
|
|
try { |
68
|
|
|
return $this->client->sismember($key, $value); |
69
|
|
|
} catch (PredisConnectionException $e) { |
70
|
|
|
throw new RedisException(); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function sMembers(string $key): array |
75
|
|
|
{ |
76
|
|
|
try { |
77
|
|
|
return $this->client->smembers($key); |
78
|
|
|
} catch (PredisConnectionException $e) { |
79
|
|
|
throw new RedisException(); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function lLen(string $key): int |
84
|
|
|
{ |
85
|
|
|
try { |
86
|
|
|
return $this->client->llen($key); |
87
|
|
|
} catch (PredisConnectionException $e) { |
88
|
|
|
throw new RedisException(); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function lRange(string $key, int $offset = 0, int $limit = 10): array |
93
|
|
|
{ |
94
|
|
|
try { |
95
|
|
|
return $this->client->lrange($key, $offset, $limit); |
96
|
|
|
} catch (PredisConnectionException $e) { |
97
|
|
|
throw new RedisException(); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
View Code Duplication |
public function zScore(string $key, string $value) |
|
|
|
|
102
|
|
|
{ |
103
|
|
|
try { |
104
|
|
|
$score = $this->client->zscore($key, $value); |
105
|
|
|
} catch (PredisConnectionException $e) { |
106
|
|
|
throw new RedisException(); |
107
|
|
|
} |
108
|
|
|
return $score === null ? $score : intval($score); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function del(string $key) |
112
|
|
|
{ |
113
|
|
|
try { |
114
|
|
|
$this->client->del($key); |
115
|
|
|
} catch (PredisConnectionException $e) { |
116
|
|
|
throw new RedisException(); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function evalScript(string $path, array $args) |
121
|
|
|
{ |
122
|
|
|
$command = new LuaFileCommand($path); |
123
|
|
|
$command->setArguments($args); |
124
|
|
|
return $this->client->executeCommand($command); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
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.