Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
language: php
php:
- 5.5
- 5.6
- 7.0
- 7.1
env:
- COMPOSER_OPTS=""
- COMPOSER_OPTS="--prefer-lowest"
services:
- redis-server
before_install:
- if [[ $TRAVIS_PHP_VERSION != 'hhvm' ]] ; then pecl channel-update pecl.php.net; fi;
- if [[ $TRAVIS_PHP_VERSION != 'hhvm' && $TRAVIS_PHP_VERSION != 7.* ]]; then pecl install riak-beta; fi;
- if [[ $TRAVIS_PHP_VERSION = '5.5' ]] ; then echo yes | pecl install apcu-4.0.10; fi;
- if [[ $TRAVIS_PHP_VERSION = '5.6' ]] ; then echo yes | pecl install apcu-4.0.10; fi;
- if [[ $TRAVIS_PHP_VERSION = 7.* ]] ; then pecl config-set preferred_state beta; echo yes | pecl install apcu; fi;
- if [[ $TRAVIS_PHP_VERSION != 'hhvm' ]]; then phpenv config-add ./integration-tests/php.ini; fi;
before_script:
- composer self-update
- composer update --no-interaction $COMPOSER_OPTS
script:
- vendor/bin/phpunit tests --coverage-text
- ln -s vendor integration-tests/vendor
- vendor/bin/phpunit integration-tests/LDDFeatureRequesterTest.php
75 changes: 59 additions & 16 deletions integration-tests/LDDFeatureRequesterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

require_once 'vendor/autoload.php';

use LaunchDarkly\ApcLDDFeatureRequester;
use LaunchDarkly\ApcuLDDFeatureRequester;
use LaunchDarkly\LDClient;
use LaunchDarkly\LDUserBuilder;

Expand All @@ -28,8 +30,10 @@ public function testGetApc() {
"scheme" => "tcp",
"host" => 'localhost',
"port" => 6379));
$client = new LDClient("BOGUS_API_KEY", array('feature_requester_class' => '\\LaunchDarkly\\ApcLDDFeatureRequester',
'apc_expiration' => 1));
$client = new LDClient("BOGUS_API_KEY", [
'feature_requester_class' => extension_loaded('apcu') ? ApcuLDDFeatureRequester::class : ApcLDDFeatureRequester::class,
'apc_expiration' => 1,
]);
$builder = new LDUserBuilder(3);
$user = $builder->build();

Expand All @@ -42,24 +46,63 @@ public function testGetApc() {
$redis->hset("launchdarkly:features", 'foo', $this->gen_feature("foo", "baz"));
$this->assertEquals("bar", $client->variation('foo', $user, 'jim'));

apc_delete("launchdarkly:features.foo");
if (extension_loaded('apcu')) {
\apcu_delete("launchdarkly:features.foo");
} else {
\apc_delete("launchdarkly:features.foo");
}
$this->assertEquals("baz", $client->variation('foo', $user, 'jim'));
}

private function gen_feature($key, $val) {
$data = <<<EOF
{"name": "Feature $key", "key": "$key", "kind": "flag", "salt": "Zm9v", "on": true,
"variations": [{"value": "$val", "weight": 100,
"targets": [{"attribute": "key", "op": "in", "values": []}],
"userTarget": {"attribute": "key", "op": "in", "values": []}},
{"value": false, "weight": 0,
"targets": [{"attribute": "key", "op": "in", "values": []}],
"userTarget": {"attribute": "key", "op": "in", "values": []}}],
"commitDate": "2015-09-08T21:24:16.712Z",
"creationDate": "2015-09-08T21:06:16.527Z",
"version": 4}
EOF;
return $data;
$data = [
'name' => 'Feature ' . $val,
'key' => $key,
'kind' => 'flag',
'salt' => 'Zm9v',
'on' => true,
'variations' => [
$val,
false,
],
'commitDate' => '2015-09-08T21:24:16.712Z',
'creationDate' => '2015-09-08T21:06:16.527Z',
'version' => 4,
'prerequisites' => [],
'targets' => [
[
'values' => [
$val,
],
'variation' => 0,
],
[
'values' => [
false,
],
'variation' => 1,
],
],
'rules' => [],
'fallthrough' => [
'rollout' => [
'variations' => [
[
'variation' => 0,
'weight' => 95000,
],
[
'variation' => 1,
'weight' => 5000,
],
],
],
],
'offVariation' => null,
'deleted' => false,
];

return \json_encode($data);
}

}
Expand Down
1 change: 1 addition & 0 deletions integration-tests/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
}
],
"require": {
"monolog/monolog": "1.21.0",
"php": ">=5.3",
"predis/predis": "1.0.*"
},
Expand Down
4 changes: 4 additions & 0 deletions integration-tests/php.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
extension="redis.so"

apc.enabled=1
apc.enable_cli=1
24 changes: 22 additions & 2 deletions src/LaunchDarkly/ApcLDDFeatureRequester.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,19 @@ function __construct($baseUri, $apiKey, $options) {
}
}

/**
* @param $key
* @param $success
* @return mixed
*/
protected function fetch($key, &$success = null)
{
return \apc_fetch($key, $success);
}

protected function get_from_cache($key) {
$key = self::make_cache_key($key);
$enabled = apc_fetch($key);
$enabled = $this->fetch($key);
if ($enabled === false) {
return null;
}
Expand All @@ -30,8 +39,19 @@ protected function get_from_cache($key) {
}
}

/**
* @param $key
* @param $var
* @param int $ttl
* @return mixed
*/
protected function add($key, $var, $ttl = 0)
{
return \apc_add($key, $var, $ttl);
}

protected function store_in_cache($key, $val) {
apc_add($this->make_cache_key($key), $val, $this->_expiration);
$this->add($this->make_cache_key($key), $val, $this->_expiration);
}

private function make_cache_key($name) {
Expand Down
32 changes: 32 additions & 0 deletions src/LaunchDarkly/ApcuLDDFeatureRequester.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
namespace LaunchDarkly;


/**
* Feature requester from an LDD-populated redis, with APC caching
*
* @package LaunchDarkly
*/
class ApcuLDDFeatureRequester extends ApcLDDFeatureRequester
{
/**
* @param $key
* @param null $success
* @return mixed
*/
protected function fetch($key, &$success = null)
{
return \apcu_fetch($key, $success);
}

/**
* @param $key
* @param $var
* @param int $ttl
* @return bool
*/
protected function add($key, $var, $ttl = 0)
{
return \apcu_add($key, $var, $ttl);
}
}