Skip to content

Commit 6bcfdbd

Browse files
committed
feat: FeatureRequester requires configured Redis instance
1 parent c0e4524 commit 6bcfdbd

File tree

6 files changed

+357
-489
lines changed

6 files changed

+357
-489
lines changed

src/LaunchDarkly/Impl/Integrations/PHPRedisFeatureRequester.php

Lines changed: 15 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -10,65 +10,32 @@
1010
*/
1111
class PHPRedisFeatureRequester extends FeatureRequesterBase
1212
{
13-
private ?array $redisOptions = null;
14-
private ?Redis $redisInstance = null;
15-
private ?string $prefix;
16-
17-
public function __construct(string $baseUri, string $sdkKey, array $options)
18-
{
13+
private readonly string $prefix;
14+
15+
public function __construct(
16+
private readonly Redis $client,
17+
string $baseUri,
18+
string $sdkKey,
19+
array $options
20+
) {
1921
parent::__construct($baseUri, $sdkKey, $options);
2022

21-
/** @var ?string **/
22-
$this->prefix = $options['redis_prefix'] ?? null;
23-
if ($this->prefix === null || $this->prefix === '') {
24-
$this->prefix = Integrations\PHPRedis::DEFAULT_PREFIX;
25-
}
26-
27-
/** @var ?Redis */
28-
$client = $this->_options['phpredis_client'] ?? null;
29-
if ($client instanceof Redis) {
30-
$this->redisInstance = $client;
31-
} else {
32-
$this->redisOptions = [
33-
"timeout" => $options['redis_timeout'] ?? 5,
34-
"host" => $options['redis_host'] ?? 'localhost',
35-
"port" => $options['redis_port'] ?? 6379,
36-
"password" => $options['redis_password'] ?? null
37-
];
23+
/** @var ?string */
24+
$prefix = $options['prefix'] ?? null;
25+
if ($prefix === null || $prefix === '') {
26+
$prefix = Integrations\PHPRedis::DEFAULT_PREFIX;
3827
}
28+
$this->prefix = $prefix;
3929
}
4030

4131
protected function readItemString(string $namespace, string $key): ?string
4232
{
43-
$redis = $this->getConnection();
44-
return $redis->hget("$this->prefix:$namespace", $key);
33+
return $this->client->hget("$this->prefix:$namespace", $key);
4534
}
4635

4736
protected function readItemStringList(string $namespace): ?array
4837
{
49-
$redis = $this->getConnection();
50-
$raw = $redis->hgetall("$this->prefix:$namespace");
38+
$raw = $this->client->hgetall("$this->prefix:$namespace");
5139
return $raw ? array_values($raw) : null;
5240
}
53-
54-
protected function getConnection(): Redis
55-
{
56-
if ($this->redisInstance instanceof Redis) {
57-
return $this->redisInstance;
58-
}
59-
60-
$redis = new Redis();
61-
$redis->pconnect(
62-
$this->redisOptions["host"],
63-
$this->redisOptions["port"],
64-
$this->redisOptions["timeout"],
65-
'launchdarkly/php-server-sdk-redis-phpredis'
66-
);
67-
68-
if ($this->redisOptions['password']) {
69-
$redis->auth($this->redisOptions['password']);
70-
}
71-
72-
return $this->redisInstance = $redis;
73-
}
7441
}

src/LaunchDarkly/Integrations/PHPRedis.php

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use LaunchDarkly\Impl\Integrations;
66
use LaunchDarkly\Subsystems;
7+
use LaunchDarkly\Subsystems\FeatureRequester;
78
use Psr\Log\LoggerInterface;
89
use Redis;
910

@@ -28,40 +29,33 @@ class PHPRedis
2829
* [SDK reference guide](https://docs.launchdarkly.com/sdk/features/storing-data).
2930
*
3031
* @param array $options Configuration settings (can also be passed in the main client configuration):
31-
* - `redis_host`: hostname of the Redis server; defaults to `localhost`
32-
* - `redis_port`: port of the Redis server; defaults to 6379
33-
* - `redis_password`: password to auth against the Redis server; optional
34-
* - `redis_timeout`: connection timeout in seconds; defaults to 5
35-
* - `redis_prefix`: a string to be prepended to all database keys; corresponds to the prefix
36-
* setting in ld-relay
37-
* - `phpredis_client`: an already-configured Redis client instance if you wish to reuse one
32+
* - `prefix`: a string to be prepended to all database keys; corresponds
33+
* to the prefix setting in ld-relay
3834
* - `apc_expiration`: expiration time in seconds for local caching, if `APCu` is installed
39-
* @return mixed an object to be stored in the `feature_requester` configuration property
35+
* @return callable(string, string, array): FeatureRequester an object to be stored in the `feature_requester` configuration property
4036
*/
41-
public static function featureRequester($options = [])
37+
public static function featureRequester(Redis $client, $options = []): callable
4238
{
4339
if (!extension_loaded('redis')) {
4440
throw new \RuntimeException("phpredis extension is required to use Integrations\\PHPRedis");
4541
}
4642

47-
return function (string $baseUri, string $sdkKey, array $baseOptions) use ($options) {
48-
return new Integrations\PHPRedisFeatureRequester($baseUri, $sdkKey, array_merge($baseOptions, $options));
43+
return function (string $baseUri, string $sdkKey, array $baseOptions) use ($client, $options): FeatureRequester {
44+
return new Integrations\PHPRedisFeatureRequester($client, $baseUri, $sdkKey, array_merge($baseOptions, $options));
4945
};
5046
}
5147

5248
/**
5349
* @param array<string,mixed> $options
54-
* - `prefix`: namespace prefix to add to all hash keys
55-
* @return callable(LoggerInterface, array): Subsystems\BigSegmentsStore
50+
* - `prefix`: a string to be prepended to all database keys; corresponds
51+
* to the prefix setting in ld-relay
5652
*/
57-
public static function bigSegmentsStore(Redis $client, array $options = []): callable
53+
public static function bigSegmentsStore(Redis $client, LoggerInterface $logger, array $options = []): Subsystems\BigSegmentsStore
5854
{
5955
if (!extension_loaded('redis')) {
6056
throw new \RuntimeException("phpredis extension is required to use Integrations\\PHPRedis");
6157
}
6258

63-
return function (LoggerInterface $logger, array $baseOptions) use ($client, $options): Subsystems\BigSegmentsStore {
64-
return new Integrations\PHPRedisBigSegmentsStore($client, $logger, array_merge($baseOptions, $options));
65-
};
59+
return new Integrations\PHPRedisBigSegmentsStore($client, $logger, $options);
6660
}
6761
}

0 commit comments

Comments
 (0)