Skip to content

Commit

Permalink
Added option redundant_servers (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nyholm authored Aug 17, 2016
1 parent 775dabc commit e81e97a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
17 changes: 15 additions & 2 deletions src/Factory/MemcacheFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ public function getAdapter(array $config)
$client = new Memcache();
$client->connect($config['host'], $config['port']);

foreach ($config['redundant_servers'] as $server) {
if (!isset($server['host'])) {
continue;
}
$port = $config['port'];
if (isset($server['port'])) {
$port = $server['port'];
}
$client->addserver($server['host'], $port);
}

return new MemcacheCachePool($client);
}

Expand All @@ -41,11 +52,13 @@ public function getAdapter(array $config)
protected static function configureOptionResolver(OptionsResolver $resolver)
{
$resolver->setDefaults([
'host' => '127.0.0.1',
'port' => 11211,
'host' => '127.0.0.1',
'port' => 11211,
'redundant_servers' => [],
]);

$resolver->setAllowedTypes('host', ['string']);
$resolver->setAllowedTypes('port', ['string', 'int']);
$resolver->setAllowedTypes('redundant_servers', ['array']);
}
}
21 changes: 17 additions & 4 deletions src/Factory/MemcachedFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ public function getAdapter(array $config)
$client = new Memcached($config['persistent_id']);
$client->addServer($config['host'], $config['port']);

foreach ($config['redundant_servers'] as $server) {
if (!isset($server['host'])) {
continue;
}
$port = $config['port'];
if (isset($server['port'])) {
$port = $server['port'];
}
$client->addServer($server['host'], $port);
}

$pool = new MemcachedCachePool($client);

if (null !== $config['pool_namespace']) {
Expand All @@ -48,15 +59,17 @@ public function getAdapter(array $config)
protected static function configureOptionResolver(OptionsResolver $resolver)
{
$resolver->setDefaults([
'persistent_id' => null,
'host' => '127.0.0.1',
'port' => 11211,
'pool_namespace' => null,
'persistent_id' => null,
'host' => '127.0.0.1',
'port' => 11211,
'pool_namespace' => null,
'redundant_servers' => [],
]);

$resolver->setAllowedTypes('persistent_id', ['string', 'null']);
$resolver->setAllowedTypes('host', ['string']);
$resolver->setAllowedTypes('port', ['string', 'int']);
$resolver->setAllowedTypes('pool_namespace', ['string', 'null']);
$resolver->setAllowedTypes('redundant_servers', ['array']);
}
}

0 comments on commit e81e97a

Please sign in to comment.