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