Skip to content

Commit

Permalink
Replace Redis parameters service configuration values
Browse files Browse the repository at this point in the history
  • Loading branch information
mfeltscher committed Feb 5, 2016
1 parent 2dd9cee commit 2189a55
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 15 deletions.
7 changes: 7 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ public function getConfigTreeBuilder()
->isRequired()
->info('Service that implements Handlebars\Cache.')
->end()
->arrayNode('redis')
->addDefaultsIfNotSet()
->children()
->scalarNode('client_service')->defaultValue('snc_redis.default')->end()
->scalarNode('key_prefix')->defaultValue('smartive-handlebars:')->end()
->end()
->end()
->end()
->end()
->arrayNode('twig')
Expand Down
28 changes: 22 additions & 6 deletions DependencyInjection/SmartiveHandlebarsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function load(array $configs, ContainerBuilder $container)
$this->loadService($serviceName, $config[$serviceName], $container, $loader);

if ('cache' === $serviceName) {
$this->loadCache($container, $config, $loader);
$this->loadCache($container, $config);
}
}
}
Expand Down Expand Up @@ -95,16 +95,15 @@ private function getParameterName($serviceName, $configName)
/**
* Loads the caching service
*
* @param array $config Configuration values
* @param ContainerBuilder $container Container instance
* @param XmlFileLoader $loader Loader instance
* @param ContainerBuilder $container Container instance
* @param array $config Configuration values
*
* @return void
*/
private function loadCache(ContainerBuilder $container, array $config, XmlFileLoader $loader)
private function loadCache(ContainerBuilder $container, array $config)
{
if (empty($config['cache']['service'])) {
return;
throw new InvalidConfigurationException('You need to specify a cache service in order to enable caching.');
}

try {
Expand All @@ -113,6 +112,23 @@ private function loadCache(ContainerBuilder $container, array $config, XmlFileLo
throw new InvalidConfigurationException('Caching can only be configured if templating is enabled.');
}

$this->prepareRedisCache($container, $config['cache']);

$templatingService->addMethodCall('setCache', [new Reference($config['cache']['service'])]);
}

/**
* Prepares the Redis cache
*
* @param ContainerBuilder $container Container instance
* @param array $cacheConfig Cache configuration values
*
* @return void
*/
private function prepareRedisCache(ContainerBuilder $container, array $cacheConfig)
{
$redisCacheService = $container->findDefinition('smartive_handlebars.cache.redis');
$redisCacheService->replaceArgument(0, new Reference($cacheConfig['redis']['client_service']));
$redisCacheService->replaceArgument(2, $cacheConfig['redis']['key_prefix']);
}
}
35 changes: 32 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,38 @@ smartive_handlebars:
```

There are several caching services / strategies available per default:
- `smartive_handlebars.cache.disk`: Uses [Handlebars\Cache\Disk](https://github.com/XaminProject/handlebars.php/blob/master/src/Handlebars/Cache/Disk.php) to read/write file cache in Symfony's cache directory
- `smartive_handlebars.cache.apc`: Uses [Handlebars\Cache\APC](https://github.com/XaminProject/handlebars.php/blob/master/src/Handlebars/Cache/APC.php) to read/write cache objects using [APC](http://php.net/manual/en/book.apc.php)
- `smartive_handlebars.cache.redis`: Uses the [RedisBundle](https://github.com/snc/SncRedisBundle) to read/write cache objects using a [Redis Server](http://redis.io/). The Redis client (has to be of type `\Predis\Client` or `\Redis`) being used can be specified using the `smartive_handlebars.cache.redis.client` parameter which defaults to `snc_redis.default`.

### Disk

Service ID: `smartive_handlebars.cache.disk`

Uses [Handlebars\Cache\Disk](https://github.com/XaminProject/handlebars.php/blob/master/src/Handlebars/Cache/Disk.php) to read/write file cache in Symfony's cache directory

### APC

Service ID: `smartive_handlebars.cache.apc`

Uses [Handlebars\Cache\APC](https://github.com/XaminProject/handlebars.php/blob/master/src/Handlebars/Cache/APC.php) to read/write cache objects using [APC](http://php.net/manual/en/book.apc.php)

### Redis

Service ID: `smartive_handlebars.cache.redis`

Uses [PhpRedis](https://github.com/phpredis/phpredis) or [Predis](https://github.com/nrk/predis) to read/write cache objects using a [Redis Server](http://redis.io/).
This bundle integrates with [RedisBundle](https://github.com/snc/SncRedisBundle) in order to make configuring your Redis implementation even easier.
The default Redis client being used is `snc_redis.default` (see [RedisBundle documentation](https://github.com/snc/SncRedisBundle/blob/master/Resources/doc/index.md#usage)).

The default configuration can be overridden looks as follows:

```
smartive_handlebars:
cache:
redis:
client_service: 'snc_redis.default'
key_prefix: 'smartive-handlebars:'
```

### Custom

You can also define your own caching services by adding a class which implements [Handlebars\Cache](https://github.com/XaminProject/handlebars.php/blob/master/src/Handlebars/Cache.php).
To use your custom cache service you have to register it in your service configuration:
Expand Down
8 changes: 2 additions & 6 deletions Resources/config/cache.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,17 @@
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<parameters>
<parameter key="smartive_handlebars.cache.redis.client">snc_redis.default</parameter>
<parameter key="smartive_handlebars.cache.redis.key_prefix">smartive-handlebars:</parameter>
</parameters>

<services>
<service id="smartive_handlebars.cache.apc" class="Handlebars\Cache\APC" public="false" />
<service id="smartive_handlebars.cache.disk" class="Handlebars\Cache\Disk" public="false">
<argument>%kernel.cache_dir%/smartive_handlebars</argument>
</service>
<service id="smartive_handlebars.cache.redis" class="Smartive\HandlebarsBundle\Cache\Redis" public="false">
<argument type="expression" on-invalid="null">service(parameter('smartive_handlebars.cache.redis.client'))</argument>
<argument type="service" id="snc_redis.default" />
<argument type="service" id="logger" on-invalid="null" />
<tag name="monolog.logger" channel="smartive_handlebars" />
<argument>%smartive_handlebars.cache.redis.key_prefix%</argument>
<argument>smartive-handlebars:</argument>
</service>
</services>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ smartive_handlebars:
cache:
enabled: true
service: 'smartive_handlebars.cache.redis'
redis:
client_service: 'snc_redis.default'
key_prefix: 'smartive-handlebars:'
templating:
template_directories:
- %kernel.root_dir%/Resources/hbs_views

0 comments on commit 2189a55

Please sign in to comment.