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