|
| 1 | +<?php |
| 2 | +namespace LaunchDarkly\Impl\Integrations; |
| 3 | + |
| 4 | +use LaunchDarkly\Impl\Integrations\FeatureRequesterBase; |
| 5 | + |
| 6 | +class PHPRedisFeatureRequester extends FeatureRequesterBase |
| 7 | +{ |
| 8 | + /** @var array */ |
| 9 | + private $_redisOptions; |
| 10 | + /** @var \Redis */ |
| 11 | + private $_redisInstance; |
| 12 | + /** @var string */ |
| 13 | + private $_prefix; |
| 14 | + |
| 15 | + public function __construct($baseUri, $sdkKey, $options) |
| 16 | + { |
| 17 | + parent::__construct($baseUri, $sdkKey, $options); |
| 18 | + |
| 19 | + $this->_prefix = $options['redis_prefix'] ?? 'launchdarkly'; |
| 20 | + |
| 21 | + $client = $this->_options['phpredis_client'] ?? null; |
| 22 | + if ($client instanceof Redis) { |
| 23 | + $this->_redisInstance = $client; |
| 24 | + } else { |
| 25 | + $this->_redisOptions = [ |
| 26 | + "timeout" => $options['redis_timeout'] ?? 5, |
| 27 | + "host" => $options['redis_host'] ?? 'localhost', |
| 28 | + "port" => $options['redis_port'] ?? 6379 |
| 29 | + ]; |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + protected function readItemString($namespace, $key) |
| 34 | + { |
| 35 | + $redis = $this->getConnection(); |
| 36 | + return $redis->hget($namespace, $key); |
| 37 | + } |
| 38 | + |
| 39 | + protected function readItemStringList($namespace) |
| 40 | + { |
| 41 | + $redis = $this->getConnection(); |
| 42 | + $raw = $redis->hgetall($namespace); |
| 43 | + return $raw ? array_values($raw) : null; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * @return \Redis |
| 48 | + */ |
| 49 | + protected function getConnection() |
| 50 | + { |
| 51 | + if ($this->_redisInstance instanceof Redis) { |
| 52 | + return $this->_redisInstance; |
| 53 | + } |
| 54 | + |
| 55 | + $redis = new \Redis(); |
| 56 | + $redis->pconnect( |
| 57 | + $this->_redisOptions["host"], |
| 58 | + $this->_redisOptions["port"], |
| 59 | + $this->_redisOptions["timeout"], |
| 60 | + 'launchdarkly/php-server-sdk-redis-phpredis' |
| 61 | + ); |
| 62 | + $redis->setOption(\Redis::OPT_PREFIX, "$this->_prefix:"); // use custom prefix on all keys |
| 63 | + return $this->_redisInstance = $redis; |
| 64 | + } |
| 65 | +} |
0 commit comments