|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Prometheus\Storage; |
| 6 | + |
| 7 | +use Predis\Client; |
| 8 | +use Prometheus\Exception\StorageException; |
| 9 | +use Prometheus\Storage\RedisClients\Predis as PredisClient; |
| 10 | + |
| 11 | +class Predis extends AbstractRedis |
| 12 | +{ |
| 13 | + /** |
| 14 | + * @var mixed[] |
| 15 | + */ |
| 16 | + private static $defaultParameters = [ |
| 17 | + 'scheme' => 'tcp', |
| 18 | + 'host' => '127.0.0.1', |
| 19 | + 'port' => 6379, |
| 20 | + 'timeout' => 0.1, |
| 21 | + 'read_write_timeout' => 10, |
| 22 | + 'persistent' => false, |
| 23 | + 'password' => null, |
| 24 | + 'username' => null, |
| 25 | + ]; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var mixed[] |
| 29 | + */ |
| 30 | + private static $defaultOptions = [ |
| 31 | + 'prefix' => '', |
| 32 | + 'throw_errors' => true, |
| 33 | + ]; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var mixed[] |
| 37 | + */ |
| 38 | + private $parameters = []; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var mixed[] |
| 42 | + */ |
| 43 | + private $options = []; |
| 44 | + |
| 45 | + /** |
| 46 | + * Redis constructor. |
| 47 | + * |
| 48 | + * @param mixed[] $options |
| 49 | + */ |
| 50 | + public function __construct(array $parameters = [], array $options = []) |
| 51 | + { |
| 52 | + $this->parameters = array_merge(self::$defaultParameters, $parameters); |
| 53 | + $this->options = array_merge(self::$defaultOptions, $options); |
| 54 | + $this->redis = PredisClient::create($this->parameters, $this->options); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @throws StorageException |
| 59 | + */ |
| 60 | + public static function fromExistingConnection(Client $client): self |
| 61 | + { |
| 62 | + $options = $client->getOptions(); |
| 63 | + $allOptions = [ |
| 64 | + 'aggregate' => $options->aggregate, |
| 65 | + 'cluster' => $options->cluster, |
| 66 | + 'connections' => $options->connections, |
| 67 | + 'exceptions' => $options->exceptions, |
| 68 | + 'prefix' => $options->prefix, |
| 69 | + 'commands' => $options->commands, |
| 70 | + 'replication' => $options->replication, |
| 71 | + ]; |
| 72 | + |
| 73 | + $self = new self; |
| 74 | + $self->redis = new PredisClient($client, self::$defaultParameters, $allOptions); |
| 75 | + |
| 76 | + return $self; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * @param mixed[] $parameters |
| 81 | + */ |
| 82 | + public static function setDefaultParameters(array $parameters): void |
| 83 | + { |
| 84 | + self::$defaultParameters = array_merge(self::$defaultParameters, $parameters); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * @param mixed[] $options |
| 89 | + */ |
| 90 | + public static function setDefaultOptions(array $options): void |
| 91 | + { |
| 92 | + self::$defaultOptions = array_merge(self::$defaultOptions, $options); |
| 93 | + } |
| 94 | +} |
0 commit comments