|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Utopia\Queue\Connection; |
| 4 | + |
| 5 | +use Utopia\Queue\Connection; |
| 6 | + |
| 7 | +class RedisCluster implements Connection |
| 8 | +{ |
| 9 | + |
| 10 | + protected array $seeds; |
| 11 | + protected ?\RedisCluster $redis = null; |
| 12 | + |
| 13 | + public function __construct(array $seeds) |
| 14 | + { |
| 15 | + $this->seeds = $seeds; |
| 16 | + } |
| 17 | + |
| 18 | + public function rightPopLeftPushArray(string $queue, string $destination, int $timeout): array|false |
| 19 | + { |
| 20 | + $response = $this->rightPopLeftPush($queue, $destination, $timeout); |
| 21 | + |
| 22 | + if (!$response) { |
| 23 | + return false; |
| 24 | + } |
| 25 | + |
| 26 | + return json_decode($response, true); |
| 27 | + } |
| 28 | + public function rightPopLeftPush(string $queue, string $destination, int $timeout): string|false |
| 29 | + { |
| 30 | + $response = $this->getRedis()->bRPopLPush($queue, $destination, $timeout); |
| 31 | + |
| 32 | + if (!$response) { |
| 33 | + return false; |
| 34 | + } |
| 35 | + |
| 36 | + return $response; |
| 37 | + } |
| 38 | + public function rightPushArray(string $queue, array $value): bool |
| 39 | + { |
| 40 | + return !!$this->getRedis()->rPush($queue, json_encode($value)); |
| 41 | + } |
| 42 | + |
| 43 | + public function rightPush(string $queue, string $value): bool |
| 44 | + { |
| 45 | + return !!$this->getRedis()->rPush($queue, $value); |
| 46 | + } |
| 47 | + |
| 48 | + public function leftPushArray(string $queue, array $value): bool |
| 49 | + { |
| 50 | + return !!$this->getRedis()->lPush($queue, json_encode($value)); |
| 51 | + } |
| 52 | + |
| 53 | + public function leftPush(string $queue, string $value): bool |
| 54 | + { |
| 55 | + return !!$this->getRedis()->lPush($queue, $value); |
| 56 | + } |
| 57 | + |
| 58 | + public function rightPopArray(string $queue, int $timeout): array|false |
| 59 | + { |
| 60 | + $response = $this->rightPop($queue, $timeout); |
| 61 | + |
| 62 | + if ($response === false) { |
| 63 | + return false; |
| 64 | + } |
| 65 | + |
| 66 | + return json_decode($response, true) ?? false; |
| 67 | + } |
| 68 | + |
| 69 | + public function rightPop(string $queue, int $timeout): string|false |
| 70 | + { |
| 71 | + $response = $this->getRedis()->brPop([$queue], $timeout); |
| 72 | + |
| 73 | + if (empty($response)) { |
| 74 | + return false; |
| 75 | + } |
| 76 | + |
| 77 | + return $response[1]; |
| 78 | + } |
| 79 | + |
| 80 | + public function leftPopArray(string $queue, int $timeout): array|false |
| 81 | + { |
| 82 | + $response = $this->getRedis()->blPop($queue, $timeout); |
| 83 | + |
| 84 | + if (empty($response)) { |
| 85 | + return false; |
| 86 | + } |
| 87 | + |
| 88 | + return json_decode($response[1], true) ?? false; |
| 89 | + } |
| 90 | + |
| 91 | + public function leftPop(string $queue, int $timeout): string|false |
| 92 | + { |
| 93 | + $response = $this->getRedis()->blPop($queue, $timeout); |
| 94 | + |
| 95 | + if (empty($response)) { |
| 96 | + return false; |
| 97 | + } |
| 98 | + |
| 99 | + return $response[1]; |
| 100 | + } |
| 101 | + |
| 102 | + public function listRemove(string $queue, string $key): bool |
| 103 | + { |
| 104 | + return !!$this->getRedis()->lRem($queue, $key, 1); |
| 105 | + } |
| 106 | + |
| 107 | + public function remove(string $key): bool |
| 108 | + { |
| 109 | + return !!$this->getRedis()->del($key); |
| 110 | + } |
| 111 | + |
| 112 | + public function move(string $queue, string $destination): bool |
| 113 | + { |
| 114 | + return $this->getRedis()->move($queue, $destination); |
| 115 | + } |
| 116 | + |
| 117 | + public function setArray(string $key, array $value): bool |
| 118 | + { |
| 119 | + return $this->set($key, json_encode($value)); |
| 120 | + } |
| 121 | + |
| 122 | + public function set(string $key, string $value): bool |
| 123 | + { |
| 124 | + return $this->getRedis()->set($key, $value); |
| 125 | + } |
| 126 | + |
| 127 | + public function get(string $key): array|string|null |
| 128 | + { |
| 129 | + return $this->getRedis()->get($key); |
| 130 | + } |
| 131 | + |
| 132 | + public function listSize(string $key): int |
| 133 | + { |
| 134 | + return $this->getRedis()->lLen($key); |
| 135 | + } |
| 136 | + |
| 137 | + public function increment(string $key): int |
| 138 | + { |
| 139 | + return $this->getRedis()->incr($key); |
| 140 | + } |
| 141 | + |
| 142 | + public function decrement(string $key): int |
| 143 | + { |
| 144 | + return $this->getRedis()->decr($key); |
| 145 | + } |
| 146 | + |
| 147 | + public function listRange(string $key, int $total, int $offset): array |
| 148 | + { |
| 149 | + $start = $offset; |
| 150 | + $end = $start + $total - 1; |
| 151 | + $results = $this->getRedis()->lRange($key, $start, $end); |
| 152 | + |
| 153 | + return $results; |
| 154 | + } |
| 155 | + |
| 156 | + public function ping(): bool |
| 157 | + { |
| 158 | + try { |
| 159 | + foreach ($this->getRedis()->_masters() as $master) { |
| 160 | + $this->getRedis()->ping($master); |
| 161 | + } |
| 162 | + |
| 163 | + return true; |
| 164 | + } catch (Exception $e) { |
| 165 | + return false; |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + protected function getRedis(): \RedisCluster |
| 170 | + { |
| 171 | + if ($this->redis) { |
| 172 | + return $this->redis; |
| 173 | + } |
| 174 | + |
| 175 | + $this->redis = new \RedisCluster(null, $this->seeds); |
| 176 | + return $this->redis; |
| 177 | + } |
| 178 | +} |
0 commit comments