Skip to content

Commit 3a8c658

Browse files
authored
phpredis integration (#1)
1 parent e1159fe commit 3a8c658

File tree

5 files changed

+144
-0
lines changed

5 files changed

+144
-0
lines changed

LICENSE

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Copyright 2021 Catamorphic, Co.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.

VERSION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1.0.0

composer.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "launchdarkly/server-sdk-redis-phpredis",
3+
"description": "LaunchDarkly PHP SDK Redis integration using the phpredis extension",
4+
"keywords": [
5+
"launchdarkly",
6+
"launchdarkly php"
7+
],
8+
"homepage": "https://github.com/launchdarkly/php-server-sdk-redis-phpredis",
9+
"license": "Apache-2.0",
10+
"authors": [
11+
{
12+
"name": "LaunchDarkly <sdks@launchdarkly.com>",
13+
"homepage": "http://www.launchdarkly.com/"
14+
}
15+
],
16+
"require": {
17+
"php": ">=7.3",
18+
"launchdarkly/server-sdk": "^4"
19+
},
20+
"config": {
21+
"sort-packages": true
22+
}
23+
}

src/PHPRedis.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
namespace LaunchDarkly\Integrations;
3+
4+
/**
5+
* Integration with a Redis data store using the `phpredis` extension.
6+
*/
7+
class PHPRedis
8+
{
9+
/**
10+
* Configures an adapter for reading feature flag data from Redis using persistent connections.
11+
*
12+
* To use this method, you must have installed the `phpredis` extension. After calling this
13+
* method, store its return value in the `feature_requester` property of your client configuration:
14+
*
15+
* $fr = LaunchDarkly\Integrations\PHPRedis::featureRequester([ "redis_prefix" => "env1" ]);
16+
* $config = [ "feature_requester" => $fr ];
17+
* $client = new LDClient("sdk_key", $config);
18+
*
19+
* For more about using LaunchDarkly with databases, see the
20+
* [SDK reference guide](https://docs.launchdarkly.com/sdk/features/storing-data).
21+
*
22+
* @param array $options Configuration settings (can also be passed in the main client configuration):
23+
* - `redis_host`: hostname of the Redis server; defaults to `localhost`
24+
* - `redis_port`: port of the Redis server; defaults to 6379
25+
* - `redis_timeout`: connection timeout in seconds; defaults to 5
26+
* - `redis_prefix`: a string to be prepended to all database keys; corresponds to the prefix
27+
* setting in ld-relay
28+
* - `phpredis_client`: an already-configured Redis client instance if you wish to reuse one
29+
* - `apc_expiration`: expiration time in seconds for local caching, if `APCu` is installed
30+
* @return mixed an object to be stored in the `feature_requester` configuration property
31+
*/
32+
public static function featureRequester($options = array())
33+
{
34+
if (!extension_loaded('redis')) {
35+
throw new \RuntimeException("phpredis extension is required to use Integrations\\PHPRedis");
36+
}
37+
38+
return function ($baseUri, $sdkKey, $baseOptions) use ($options) {
39+
return new Impl\PHPRedisFeatureRequester($baseUri, $sdkKey, array_merge($baseOptions, $options));
40+
};
41+
}
42+
}

src/PHPRedisFeatureRequester.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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

Comments
 (0)