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
6 changes: 6 additions & 0 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@ machine:
php:
version: 5.6.17
services:
- redis
- docker
dependencies:
pre:
- yes '' | pecl install -f apcu-4.0.10
- echo "extension=apcu.so" | sudo tee -a /opt/circleci/php/$(phpenv global)/etc/php.ini
- docker pull php
- docker pull nyanpass/php5.5

test:
override:
- vendor/bin/phpunit tests --coverage-text
- ln -s vendor integration-tests/vendor
- vendor/bin/phpunit integration-tests/LDDFeatureRequesterTest.php

- composer update && vendor/bin/phpunit tests
- composer update --prefer-lowest && vendor/bin/phpunit tests

Expand Down
11 changes: 9 additions & 2 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,7 +30,8 @@ public function testGetApc() {
"scheme" => "tcp",
"host" => 'localhost',
"port" => 6379));
$client = new LDClient("BOGUS_API_KEY", array('feature_requester_class' => '\\LaunchDarkly\\ApcLDDFeatureRequester',
$client = new LDClient("BOGUS_API_KEY", array(
'feature_requester_class' => extension_loaded('apcu') ? ApcuLDDFeatureRequester::class : ApcLDDFeatureRequester::class,
'apc_expiration' => 1));
$builder = new LDUserBuilder(3);
$user = $builder->build();
Expand All @@ -42,7 +45,11 @@ 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'));
}

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
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the intention for having this class vs ApcLDDFeatureRequester? Their phpdoc is the same. Adding use cases and examples to the Readme would also be great.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

APC and APCU are different extensions. please correct me if i'm wrong; APC is only available up to php54. beyond that, the docs recommend usage of the built-in op-code cache. so a compatible APCU extension was created for >=php56. my change will allow integrations to choose APC or APCU based on which extension they have installed and should cover a wider range of PHP versions. i felt i needed to do this as a part of this merge request because i'm trying to get the integration tests to run on CircleCI for the default php56 version, which does not support APC (i think). the integration tests that run in the Vagrant VM switch the PHP version to php54, which is fine, but i think we should be able to run integration tests on other php versions.

i'd be happy to update the docs with use cases and examples. didn't know if this was a welcome proposal. also, the integration tests are failing the build, for a couple of reasons. one is that monolog needs to be installed (addressed here #55) and the other (i think) is that the feature-generator function in the integration tests needs to be updated. i took a crack at it in another pull request, but i felt it was getting bloated. thoughts on how to proceed?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've merged #55 so that should be be ok now. Yes please update the docs regarding apc vs. apcu. Your changes are definitely welcome.
Thanks!

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);
}
}