| @@ 8-104 (lines=97) @@ | ||
| 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 | $this->tryCatch(function () { |
|
| 21 | $this->redis->ping(); |
|
| 22 | }); |
|
| 23 | } |
|
| 24 | ||
| 25 | public function bRPopLPush(string $source, string $destination, int $timeout) |
|
| 26 | { |
|
| 27 | return $this->tryCatch(function () use ($source, $destination, $timeout) { |
|
| 28 | return $this->redis->brpoplpush($source, $destination, $timeout); |
|
| 29 | }); |
|
| 30 | } |
|
| 31 | ||
| 32 | public function hGet(string $key, string $field) |
|
| 33 | { |
|
| 34 | return $this->tryCatch(function () use ($key, $field) { |
|
| 35 | return $this->redis->hGet($key, $field); |
|
| 36 | }); |
|
| 37 | } |
|
| 38 | ||
| 39 | public function sAdd(string $key, array $members) |
|
| 40 | { |
|
| 41 | $this->tryCatch(function () use ($key, $members) { |
|
| 42 | $this->redis->sAdd($key, ...$members); |
|
| 43 | }); |
|
| 44 | } |
|
| 45 | ||
| 46 | public function sRem(string $key, array $members) |
|
| 47 | { |
|
| 48 | $this->tryCatch(function () use ($key, $members) { |
|
| 49 | $this->redis->sRem($key, ...$members); |
|
| 50 | }); |
|
| 51 | } |
|
| 52 | ||
| 53 | public function sIsMember(string $key, string $value): bool |
|
| 54 | { |
|
| 55 | return $this->tryCatch(function () use ($key, $value) { |
|
| 56 | return $this->redis->sIsMember($key, $value); |
|
| 57 | }); |
|
| 58 | } |
|
| 59 | ||
| 60 | public function sMembers(string $key): array |
|
| 61 | { |
|
| 62 | return $this->tryCatch(function () use ($key) { |
|
| 63 | return $this->redis->sMembers($key); |
|
| 64 | }); |
|
| 65 | } |
|
| 66 | ||
| 67 | public function lLen(string $key): int |
|
| 68 | { |
|
| 69 | return $this->tryCatch(function () use ($key) { |
|
| 70 | return $this->redis->lLen($key); |
|
| 71 | }); |
|
| 72 | } |
|
| 73 | ||
| 74 | public function lRange(string $key, int $offset = 0, int $limit = 10): array |
|
| 75 | { |
|
| 76 | return $this->tryCatch(function () use ($key, $offset, $limit) { |
|
| 77 | return $this->redis->lRange($key, $offset, $limit); |
|
| 78 | }); |
|
| 79 | } |
|
| 80 | ||
| 81 | public function zScore(string $key, string $value) |
|
| 82 | { |
|
| 83 | $score = $this->tryCatch(function () use ($key, $value) { |
|
| 84 | return $this->redis->zScore($key, $value); |
|
| 85 | }); |
|
| 86 | return $score === null ? $score : intval($score); |
|
| 87 | } |
|
| 88 | ||
| 89 | public function evalLua(string $lua, array $args) |
|
| 90 | { |
|
| 91 | return $this->tryCatch(function () use ($lua, $args) { |
|
| 92 | return $this->redis->evaluate($lua, $args, 0); |
|
| 93 | }); |
|
| 94 | } |
|
| 95 | ||
| 96 | private function tryCatch(callable $callable) |
|
| 97 | { |
|
| 98 | try { |
|
| 99 | return $callable(); |
|
| 100 | } catch (\RedisException $e) { |
|
| 101 | throw new RedisException($e->getMessage(), 0, $e); |
|
| 102 | } |
|
| 103 | } |
|
| 104 | } |
|
| 105 | ||
| @@ 10-106 (lines=97) @@ | ||
| 7 | use Predis\ClientInterface; |
|
| 8 | use Predis\Connection\ConnectionException; |
|
| 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 | $this->tryCatch(function () { |
|
| 23 | $this->client->ping(); |
|
| 24 | }); |
|
| 25 | } |
|
| 26 | ||
| 27 | public function bRPopLPush(string $source, string $destination, int $timeout) |
|
| 28 | { |
|
| 29 | return $this->tryCatch(function () use ($source, $destination, $timeout) { |
|
| 30 | return $this->client->brpoplpush($source, $destination, $timeout); |
|
| 31 | }); |
|
| 32 | } |
|
| 33 | ||
| 34 | public function hGet(string $key, string $field) |
|
| 35 | { |
|
| 36 | return $this->tryCatch(function () use ($key, $field) { |
|
| 37 | return $this->client->hget($key, $field); |
|
| 38 | }); |
|
| 39 | } |
|
| 40 | ||
| 41 | public function sAdd(string $key, array $members) |
|
| 42 | { |
|
| 43 | $this->tryCatch(function () use ($key, $members) { |
|
| 44 | $this->client->sadd($key, $members); |
|
| 45 | }); |
|
| 46 | } |
|
| 47 | ||
| 48 | public function sRem(string $key, array $members) |
|
| 49 | { |
|
| 50 | $this->tryCatch(function () use ($key, $members) { |
|
| 51 | $this->client->srem($key, $members); |
|
| 52 | }); |
|
| 53 | } |
|
| 54 | ||
| 55 | public function sIsMember(string $key, string $value): bool |
|
| 56 | { |
|
| 57 | return $this->tryCatch(function () use ($key, $value) { |
|
| 58 | return $this->client->sismember($key, $value); |
|
| 59 | }); |
|
| 60 | } |
|
| 61 | ||
| 62 | public function sMembers(string $key): array |
|
| 63 | { |
|
| 64 | return $this->tryCatch(function () use ($key) { |
|
| 65 | return $this->client->smembers($key); |
|
| 66 | }); |
|
| 67 | } |
|
| 68 | ||
| 69 | public function lLen(string $key): int |
|
| 70 | { |
|
| 71 | return $this->tryCatch(function () use ($key) { |
|
| 72 | return $this->client->llen($key); |
|
| 73 | }); |
|
| 74 | } |
|
| 75 | ||
| 76 | public function lRange(string $key, int $offset = 0, int $limit = 10): array |
|
| 77 | { |
|
| 78 | return $this->tryCatch(function () use ($key, $offset, $limit) { |
|
| 79 | return $this->client->lrange($key, $offset, $limit); |
|
| 80 | }); |
|
| 81 | } |
|
| 82 | ||
| 83 | public function zScore(string $key, string $value) |
|
| 84 | { |
|
| 85 | $score = $this->tryCatch(function () use ($key, $value) { |
|
| 86 | return $this->client->zscore($key, $value); |
|
| 87 | }); |
|
| 88 | return $score === null ? $score : intval($score); |
|
| 89 | } |
|
| 90 | ||
| 91 | public function evalLua(string $lua, array $args) |
|
| 92 | { |
|
| 93 | return $this->tryCatch(function () use ($lua, $args) { |
|
| 94 | return $this->client->eval($lua, 0, ...$args); |
|
| 95 | }); |
|
| 96 | } |
|
| 97 | ||
| 98 | private function tryCatch(callable $callable) |
|
| 99 | { |
|
| 100 | try { |
|
| 101 | return $callable(); |
|
| 102 | } catch (ConnectionException $e) { |
|
| 103 | throw new RedisException($e->getMessage(), 0, $e); |
|
| 104 | } |
|
| 105 | } |
|
| 106 | } |
|
| 107 | ||