Skip to content
This repository has been archived by the owner on Aug 22, 2023. It is now read-only.

Commit

Permalink
Merge pull request #38 from bestit/feature/add-contentful-cache-option
Browse files Browse the repository at this point in the history
Add caching option for contentful activator
  • Loading branch information
migo315 committed Jul 10, 2018
2 parents 7ffd535 + b3e3d9e commit bb31064
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- Add `php-mock` as dev dependency and add missing contentful configurator test @migo315
- \#31 Add support for auto configuration for `FeatureActivatorInterface` @migo315
- \#32 Add support for auto configuration for `ContectDecoratorInterface` @migo315
- Add caching option for `ContentfulActivator` @migo315

### Removed
- Remove unneeded models and bags (just internal stuff) @migo315
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"php": "^5.6||^7.0",
"symfony/framework-bundle": ">=2.7",
"doctrine/common": "^2.3",
"flagception/flagception": "^1.2"
"flagception/flagception": "^1.3"
},
"require-dev": {
"phing/phing": "^2.15",
Expand Down
35 changes: 35 additions & 0 deletions docs/contentful.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,38 @@ flagception:
name: 'featureName'
state: 'isActive'
```

Enable caching
-------------------------
This activator will request Contentful for every check. This is not advantageous due to the
long HTTP communication. Therefore, you can set up a cache for this activator. Identical feature queries are
then loaded from the cache instead of being retrieved from Contentful. All you have to do is specify a cache
pool and cache interval.

By default, the cache is disabled to avoid a BC.

Example:

```yml
# config.yml

flagception:
features:
feature_123:
default: false

activators:
contentful:
enable: true
# ...
cache:

# Enable the cache option (default: false)
enable: true

# Set cache pool (default: cache.app)
pool: cache.app

# Set lifetime for cache in seconds (default: 3600)
lifetime: 3600
```
35 changes: 34 additions & 1 deletion src/DependencyInjection/Configurator/ContentfulConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Flagception\Bundle\FlagceptionBundle\DependencyInjection\Configurator;

use Flagception\Activator\CacheActivator;
use Flagception\Contentful\Activator\ContentfulActivator;
use LogicException;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
Expand All @@ -10,7 +11,7 @@
use Symfony\Component\DependencyInjection\Reference;

/**
* Class ContentfulConfigurator
* Configurator for contentful activator
*
* @author Michel Chowanski <chowanski@bestit-online.de>
* @package Flagception\Bundle\FlagceptionBundle\DependencyInjection\Configurator
Expand Down Expand Up @@ -48,6 +49,17 @@ public function addActivator(ContainerBuilder $container, array $config, array $
]);

$container->setDefinition('flagception.activator.contentful_activator', $definition);

// Set caching
if ($config['cache']['enable'] === true) {
$cacheDefinition = new Definition(CacheActivator::class);
$cacheDefinition->setDecoratedService('flagception.activator.contentful_activator');
$cacheDefinition->addArgument(new Reference('flagception.activator.contentful_activator.cache.inner'));
$cacheDefinition->addArgument(new Reference($config['cache']['pool']));
$cacheDefinition->addArgument($config['cache']['lifetime']);

$container->setDefinition('flagception.activator.contentful_activator.cache', $cacheDefinition);
}
}

/**
Expand Down Expand Up @@ -91,6 +103,27 @@ public function addConfiguration(ArrayNodeDefinition $node)
->end()
->end()
->end()
->arrayNode('cache')
->addDefaultsIfNotSet()
->children()
->booleanNode('enable')
->beforeNormalization()
->ifString()
->then(function ($value) {
return filter_var($value, FILTER_VALIDATE_BOOLEAN);
})
->end()
->defaultFalse()
->end()
->scalarNode('pool')
->defaultValue('cache.app')
->cannotBeEmpty()
->end()
->integerNode('lifetime')
->defaultValue(3600)
->end()
->end()
->end()
->end();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,55 @@ public function testActivatorSetPriority()
static::assertEquals(10, $definition->getTag('flagception.activator')[0]['priority']);
}

/**
* Test activator cache can be enabled by string
*
* @return void
*/
public function testCacheCanByEnabledByString()
{
$config = [
[
'activators' => [
'contentful' => [
'enable' => 'true',
'client_id' => 'foobar',
'cache' => [
'enable' => 'true'
]
]
]
]
];
$extension = new FlagceptionExtension();
$extension->load($config, $this->container);

static::assertTrue($this->container->hasDefinition('flagception.activator.contentful_activator.cache'));
}

/**
* Test cache activator is disabled by default
*
* @return void
*/
public function testCacheIsDisabledByDefault()
{
$config = [
[
'activators' => [
'contentful' => [
'enable' => 'true',
'client_id' => 'foobar'
]
]
]
];
$extension = new FlagceptionExtension();
$extension->load($config, $this->container);

static::assertFalse($this->container->hasDefinition('flagception.activator.contentful_activator.cache'));
}

/**
* Test minimal configuration
*
Expand Down Expand Up @@ -278,6 +327,8 @@ public function testMinimalConfiguration()
],
$this->container->getDefinition('flagception.activator.contentful_activator')->getArgument(2)
);

static::assertFalse($this->container->hasDefinition('flagception.activator.contentful_activator.cache'));
}

/**
Expand All @@ -297,6 +348,11 @@ public function testFullConfiguration()
'mapping' => [
'name' => $name = uniqid(),
'state' => $state = uniqid()
],
'cache' => [
'enable' => true,
'pool' => 'cache.app',
'lifetime' => 3600
]
]
]
Expand All @@ -323,5 +379,11 @@ public function testFullConfiguration()
],
$this->container->getDefinition('flagception.activator.contentful_activator')->getArgument(2)
);

$definition = $this->container->getDefinition('flagception.activator.contentful_activator.cache');
static::assertEquals(
'flagception.activator.contentful_activator',
$definition->getDecoratedService()[0]
);
}
}

0 comments on commit bb31064

Please sign in to comment.