Skip to content

Commit 04a0db9

Browse files
committed
Merge pull request #35 from Phobetor/memcached-options
Add memcached options configuration and handling
2 parents 136ffb1 + 30c05ef commit 04a0db9

File tree

2 files changed

+81
-4
lines changed

2 files changed

+81
-4
lines changed

src/DependencyInjection/Builder/ServiceBuilder.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,22 @@ public function createCacheInstance(Definition $service, $type, $id, array $inst
157157
if (empty($instance['id'])) {
158158
$cache = new Definition(self::$types[$type]['class']);
159159

160+
// set memcached options first as they need to be set before the servers are added.
161+
if ($type === 'memcached') {
162+
if (!empty($instance['options']['memcached'])) {
163+
foreach ($instance['options']['memcached'] as $option => $value) {
164+
switch ($option) {
165+
case 'serializer':
166+
case 'hash':
167+
case 'distribution':
168+
$value = constant(sprintf('\Memcached::%s_%s', strtoupper($option), strtoupper($value)));
169+
break;
170+
}
171+
$cache->addMethodCall('setOption', array(constant(sprintf('\Memcached::OPT_%s', strtoupper($option))), $value));
172+
}
173+
}
174+
}
175+
160176
if (isset($instance['persistent']) && $instance['persistent'] !== false) {
161177
if ($instance['persistent'] !== true) {
162178
$persistentId = $instance['persistent'];

src/DependencyInjection/Configuration.php

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,13 @@ private function getClustersNode()
8787
->defaultNull()
8888
->beforeNormalization()
8989
->ifTrue(
90-
function($v) {
91-
return $v === 'true' || $v === 'false';
90+
function ($v) {
91+
return $v === 'true' || $v === 'false';
9292
}
9393
)
9494
->then(
95-
function($v) {
96-
return (bool) $v;
95+
function ($v) {
96+
return (bool) $v;
9797
}
9898
)
9999
->end()
@@ -112,6 +112,9 @@ function($v) {
112112
->end()
113113
->arrayNode('options')
114114
->info("Options for Redis and Memcached.")
115+
->children()
116+
->append($this->getMemcachedOptions())
117+
->end()
115118
->end()
116119
->arrayNode('hosts')
117120
->prototype('array')
@@ -164,6 +167,64 @@ function ($v) {
164167
return $node;
165168
}
166169

170+
/**
171+
* @return ArrayNodeDefinition
172+
*/
173+
private function getMemcachedOptions()
174+
{
175+
$treeBuilder = new TreeBuilder();
176+
$node = $treeBuilder->root('memcached');
177+
178+
if (class_exists('\Memcached')) {
179+
$node
180+
->children()
181+
->booleanNode('compression')
182+
->end()
183+
->enumNode('serializer')
184+
->values(array('php', 'igbinary', 'json'))
185+
->end()
186+
->scalarNode('prefix_key')
187+
->end()
188+
->enumNode('hash')
189+
->values(array('default', 'md5', 'crc', 'fnv1_64', 'fnv1a_64', 'fnv1_32', 'fnv1a_32', 'hsieh', 'murmur'))
190+
->end()
191+
->enumNode('distribution')
192+
->values(array('modula', 'consistent'))
193+
->end()
194+
->booleanNode('libketama_compatible')
195+
->end()
196+
->booleanNode('uffer_writes')
197+
->end()
198+
->booleanNode('binary_protocol')
199+
->end()
200+
->booleanNode('no_block')
201+
->end()
202+
->booleanNode('tcp_nodelay')
203+
->end()
204+
->integerNode('socket_send_size')
205+
->end()
206+
->integerNode('socket_recv_size')
207+
->end()
208+
->integerNode('connect_timeout')
209+
->end()
210+
->integerNode('retry_timeout')
211+
->end()
212+
->integerNode('send_timeout')
213+
->end()
214+
->integerNode('recv_timeout')
215+
->end()
216+
->integerNode('poll_timeout')
217+
->end()
218+
->booleanNode('cache_lookups')
219+
->end()
220+
->integerNode('server_failure_limit')
221+
->end()
222+
->end();
223+
}
224+
225+
return $node;
226+
}
227+
167228
/**
168229
* Configure the "aequasi_cache.session" section
169230
*

0 commit comments

Comments
 (0)