|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MGDigital\BusQue\Redis\Predis; |
|
4
|
|
|
|
|
5
|
|
|
use MGDigital\BusQue\Exception\DriverException; |
|
6
|
|
|
use MGDigital\BusQue\Redis\RedisAdapterInterface; |
|
7
|
|
|
use Predis\ClientInterface; |
|
8
|
|
|
use Predis\Connection\ConnectionException; |
|
9
|
|
|
|
|
10
|
|
|
class PredisAdapter implements RedisAdapterInterface |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
private $client; |
|
14
|
|
|
|
|
15
|
10 |
|
public function __construct(ClientInterface $client) |
|
16
|
|
|
{ |
|
17
|
10 |
|
$this->client = $client; |
|
18
|
10 |
|
} |
|
19
|
|
|
|
|
20
|
1 |
|
public function bRPopLPush(string $source, string $destination, int $timeout) |
|
21
|
|
|
{ |
|
22
|
|
|
return $this->tryCatch(function () use ($source, $destination, $timeout) { |
|
23
|
1 |
|
return $this->client->brpoplpush($source, $destination, $timeout); |
|
24
|
1 |
|
}); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
1 |
|
public function hGet(string $key, string $field) |
|
28
|
|
|
{ |
|
29
|
|
|
return $this->tryCatch(function () use ($key, $field) { |
|
30
|
1 |
|
return $this->client->hget($key, $field); |
|
31
|
1 |
|
}); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
1 |
|
public function sAdd(string $key, array $members) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->tryCatch(function () use ($key, $members) { |
|
37
|
1 |
|
$this->client->sadd($key, $members); |
|
38
|
1 |
|
}); |
|
39
|
1 |
|
} |
|
40
|
|
|
|
|
41
|
1 |
|
public function sRem(string $key, array $members) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->tryCatch(function () use ($key, $members) { |
|
44
|
1 |
|
$this->client->srem($key, $members); |
|
45
|
1 |
|
}); |
|
46
|
1 |
|
} |
|
47
|
|
|
|
|
48
|
1 |
|
public function sIsMember(string $key, string $value): bool |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->tryCatch(function () use ($key, $value) { |
|
51
|
1 |
|
return $this->client->sismember($key, $value); |
|
52
|
1 |
|
}); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
1 |
|
public function sMembers(string $key): array |
|
56
|
|
|
{ |
|
57
|
|
|
return $this->tryCatch(function () use ($key) { |
|
58
|
1 |
|
return $this->client->smembers($key); |
|
59
|
1 |
|
}); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
1 |
|
public function lLen(string $key): int |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->tryCatch(function () use ($key) { |
|
65
|
1 |
|
return $this->client->llen($key); |
|
66
|
1 |
|
}); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
1 |
|
public function lRange(string $key, int $offset = 0, int $limit = 10): array |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->tryCatch(function () use ($key, $offset, $limit) { |
|
72
|
1 |
|
return $this->client->lrange($key, $offset, $limit); |
|
73
|
1 |
|
}); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
1 |
|
public function zScore(string $key, string $value) |
|
77
|
|
|
{ |
|
78
|
|
|
$score = $this->tryCatch(function () use ($key, $value) { |
|
79
|
1 |
|
return $this->client->zscore($key, $value); |
|
80
|
1 |
|
}); |
|
81
|
1 |
|
return $score === null ? $score : intval($score); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function evalLua(string $lua, array $args) |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->tryCatch(function () use ($lua, $args) { |
|
87
|
|
|
return $this->client->eval($lua, 0, ...$args); |
|
88
|
|
|
}); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
9 |
|
private function tryCatch(callable $callable) |
|
92
|
|
|
{ |
|
93
|
|
|
try { |
|
94
|
9 |
|
return $callable(); |
|
95
|
9 |
|
} catch (ConnectionException $e) { |
|
96
|
9 |
|
throw new DriverException($e->getMessage(), 0, $e); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|