Skip to content

Commit

Permalink
Merge pull request #35 from Phobetor/memcached-options
Browse files Browse the repository at this point in the history
Add memcached options configuration and handling
  • Loading branch information
cryptiklemur committed Nov 10, 2014
2 parents 136ffb1 + 30c05ef commit 04a0db9
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 4 deletions.
16 changes: 16 additions & 0 deletions src/DependencyInjection/Builder/ServiceBuilder.php
Expand Up @@ -157,6 +157,22 @@ public function createCacheInstance(Definition $service, $type, $id, array $inst
if (empty($instance['id'])) {
$cache = new Definition(self::$types[$type]['class']);

// set memcached options first as they need to be set before the servers are added.
if ($type === 'memcached') {
if (!empty($instance['options']['memcached'])) {
foreach ($instance['options']['memcached'] as $option => $value) {
switch ($option) {
case 'serializer':
case 'hash':
case 'distribution':
$value = constant(sprintf('\Memcached::%s_%s', strtoupper($option), strtoupper($value)));
break;
}
$cache->addMethodCall('setOption', array(constant(sprintf('\Memcached::OPT_%s', strtoupper($option))), $value));
}
}
}

if (isset($instance['persistent']) && $instance['persistent'] !== false) {
if ($instance['persistent'] !== true) {
$persistentId = $instance['persistent'];
Expand Down
69 changes: 65 additions & 4 deletions src/DependencyInjection/Configuration.php
Expand Up @@ -87,13 +87,13 @@ private function getClustersNode()
->defaultNull()
->beforeNormalization()
->ifTrue(
function($v) {
return $v === 'true' || $v === 'false';
function ($v) {
return $v === 'true' || $v === 'false';
}
)
->then(
function($v) {
return (bool) $v;
function ($v) {
return (bool) $v;
}
)
->end()
Expand All @@ -112,6 +112,9 @@ function($v) {
->end()
->arrayNode('options')
->info("Options for Redis and Memcached.")
->children()
->append($this->getMemcachedOptions())
->end()
->end()
->arrayNode('hosts')
->prototype('array')
Expand Down Expand Up @@ -164,6 +167,64 @@ function ($v) {
return $node;
}

/**
* @return ArrayNodeDefinition
*/
private function getMemcachedOptions()
{
$treeBuilder = new TreeBuilder();
$node = $treeBuilder->root('memcached');

if (class_exists('\Memcached')) {
$node
->children()
->booleanNode('compression')
->end()
->enumNode('serializer')
->values(array('php', 'igbinary', 'json'))
->end()
->scalarNode('prefix_key')
->end()
->enumNode('hash')
->values(array('default', 'md5', 'crc', 'fnv1_64', 'fnv1a_64', 'fnv1_32', 'fnv1a_32', 'hsieh', 'murmur'))
->end()
->enumNode('distribution')
->values(array('modula', 'consistent'))
->end()
->booleanNode('libketama_compatible')
->end()
->booleanNode('uffer_writes')
->end()
->booleanNode('binary_protocol')
->end()
->booleanNode('no_block')
->end()
->booleanNode('tcp_nodelay')
->end()
->integerNode('socket_send_size')
->end()
->integerNode('socket_recv_size')
->end()
->integerNode('connect_timeout')
->end()
->integerNode('retry_timeout')
->end()
->integerNode('send_timeout')
->end()
->integerNode('recv_timeout')
->end()
->integerNode('poll_timeout')
->end()
->booleanNode('cache_lookups')
->end()
->integerNode('server_failure_limit')
->end()
->end();
}

return $node;
}

/**
* Configure the "aequasi_cache.session" section
*
Expand Down

0 comments on commit 04a0db9

Please sign in to comment.