|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Prometheus\Storage\RedisClients; |
| 6 | + |
| 7 | +use Prometheus\Exception\StorageException; |
| 8 | + |
| 9 | +class PHPRedis implements RedisClient |
| 10 | +{ |
| 11 | + /** |
| 12 | + * @var \Redis |
| 13 | + */ |
| 14 | + private $redis; |
| 15 | + |
| 16 | + private $options = []; |
| 17 | + |
| 18 | + /** |
| 19 | + * @var bool |
| 20 | + */ |
| 21 | + private $connectionInitialized = false; |
| 22 | + |
| 23 | + public function __construct(\Redis $redis, array $options) |
| 24 | + { |
| 25 | + $this->redis = $redis; |
| 26 | + $this->options = $options; |
| 27 | + } |
| 28 | + |
| 29 | + public static function create(array $options): self |
| 30 | + { |
| 31 | + $redis = new \Redis; |
| 32 | + |
| 33 | + return new self($redis, $options); |
| 34 | + } |
| 35 | + |
| 36 | + public function getOption(int $option): mixed |
| 37 | + { |
| 38 | + return $this->redis->getOption($option); |
| 39 | + } |
| 40 | + |
| 41 | + public function eval(string $script, array $args = [], int $num_keys = 0): mixed |
| 42 | + { |
| 43 | + return $this->redis->eval($script, $args, $num_keys); |
| 44 | + } |
| 45 | + |
| 46 | + public function set(string $key, mixed $value, mixed $options = null): string|bool |
| 47 | + { |
| 48 | + return $this->redis->set($key, $value, $options); |
| 49 | + } |
| 50 | + |
| 51 | + public function setNx(string $key, mixed $value): bool |
| 52 | + { |
| 53 | + return $this->redis->setNx($key, $value); |
| 54 | + } |
| 55 | + |
| 56 | + public function hSetNx(string $key, string $field, mixed $value): bool |
| 57 | + { |
| 58 | + return $this->redis->hSetNx($key, $field, $value); |
| 59 | + } |
| 60 | + |
| 61 | + public function sMembers(string $key): array|false |
| 62 | + { |
| 63 | + return $this->redis->sMembers($key); |
| 64 | + } |
| 65 | + |
| 66 | + public function hGetAll(string $key): array|false |
| 67 | + { |
| 68 | + return $this->redis->hGetAll($key); |
| 69 | + } |
| 70 | + |
| 71 | + public function keys(string $pattern) |
| 72 | + { |
| 73 | + return $this->redis->keys($pattern); |
| 74 | + } |
| 75 | + |
| 76 | + public function get(string $key): mixed |
| 77 | + { |
| 78 | + return $this->redis->get($key); |
| 79 | + } |
| 80 | + |
| 81 | + public function del(array|string $key, string ...$other_keys): int|false |
| 82 | + { |
| 83 | + try { |
| 84 | + return $this->redis->del($key, ...$other_keys); |
| 85 | + } catch (\RedisException $e) { |
| 86 | + throw new RedisClientException($e->getMessage()); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + public function getPrefix(): string |
| 91 | + { |
| 92 | + return $this->redis->_prefix(''); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * @throws StorageException |
| 97 | + */ |
| 98 | + public function ensureOpenConnection(): void |
| 99 | + { |
| 100 | + if ($this->connectionInitialized === true) { |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + $this->connectToServer(); |
| 105 | + $authParams = []; |
| 106 | + |
| 107 | + if (isset($this->options['user']) && $this->options['user'] !== '') { |
| 108 | + $authParams[] = $this->options['user']; |
| 109 | + } |
| 110 | + |
| 111 | + if (isset($this->options['password'])) { |
| 112 | + $authParams[] = $this->options['password']; |
| 113 | + } |
| 114 | + |
| 115 | + if ($authParams !== []) { |
| 116 | + $this->redis->auth($authParams); |
| 117 | + } |
| 118 | + |
| 119 | + if (isset($this->options['database'])) { |
| 120 | + $this->redis->select($this->options['database']); |
| 121 | + } |
| 122 | + |
| 123 | + $this->redis->setOption(RedisClient::OPT_READ_TIMEOUT, $this->options['read_timeout']); |
| 124 | + |
| 125 | + $this->connectionInitialized = true; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * @throws StorageException |
| 130 | + */ |
| 131 | + private function connectToServer(): void |
| 132 | + { |
| 133 | + try { |
| 134 | + $connection_successful = false; |
| 135 | + if ($this->options['persistent_connections'] !== false) { |
| 136 | + $connection_successful = $this->redis->pconnect( |
| 137 | + $this->options['host'], |
| 138 | + (int) $this->options['port'], |
| 139 | + (float) $this->options['timeout'] |
| 140 | + ); |
| 141 | + } else { |
| 142 | + $connection_successful = $this->redis->connect($this->options['host'], (int) $this->options['port'], (float) $this->options['timeout']); |
| 143 | + } |
| 144 | + if (! $connection_successful) { |
| 145 | + throw new StorageException( |
| 146 | + sprintf("Can't connect to Redis server. %s", $this->redis->getLastError()), |
| 147 | + 0 |
| 148 | + ); |
| 149 | + } |
| 150 | + } catch (\RedisException $e) { |
| 151 | + throw new StorageException( |
| 152 | + sprintf("Can't connect to Redis server. %s", $e->getMessage()), |
| 153 | + $e->getCode(), |
| 154 | + ); |
| 155 | + } |
| 156 | + } |
| 157 | +} |
0 commit comments