Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge branch 'hotfix/zend-cache-memcached-connection' of https://gith…
Browse files Browse the repository at this point in the history
…ub.com/mwillbanks/zf2 into hotfix/cache-memcached-multiserver
  • Loading branch information
weierophinney committed Feb 24, 2012
11 parents 926e71f + 1f29b3e + a75171a + 9a8e72f + 814716e + 88d9371 + 678232d + a0560e8 + 4ff045c + ac805a6 + 5185be6 commit 41f360c
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 27 deletions.
9 changes: 7 additions & 2 deletions src/Storage/Adapter/Memcached.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,14 @@ public function __construct($options = null)

// It's ok to add server as soon as possible because
// ext/memcached auto-connects to the server on first use
// TODO: Handle multiple servers
$options = $this->getOptions();
$this->memcached->addServer($options->getServer(), $options->getPort());

$servers = $options->getServers();
if (!$servers) {
$options->addServer('localhost', 11211);
$servers = $options->getServers();
}
$this->memcached->addServers($servers);
}

/* options */
Expand Down
75 changes: 52 additions & 23 deletions src/Storage/Adapter/MemcachedOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
namespace Zend\Cache\Storage\Adapter;

use Memcached as MemcachedResource,
Zend\Cache\Exception;
Zend\Cache\Exception,
Zend\Validator\Hostname;

/**
* These are options specific to the APC adapter
Expand All @@ -35,6 +36,7 @@
*/
class MemcachedOptions extends AdapterOptions
{

/**
* Map of option keys to \Memcached options
*
Expand Down Expand Up @@ -69,14 +71,7 @@ class MemcachedOptions extends AdapterOptions
*
* @var string
*/
protected $server = 'localhost';

/**
* Memcached port
*
* @var integer
*/
protected $port = 11211;
protected $servers = array();

/**
* Whether or not to enable binary protocol for communication with server
Expand Down Expand Up @@ -227,19 +222,25 @@ public function setNamespace($namespace)
return parent::setNamespace($namespace);
}

public function setServer($server)
{
$this->server= $server;
return $this;
}

public function getServer()
/**
* Add Server
*
* @param string $host
* @param int $port
* @return MemcachedOptions
* @throws Exception\InvalidArgumentException
*/
public function addServer($host, $port = 11211)
{
return $this->server;
}
$hostNameValidator = new Hostname(array('allow' => Hostname::ALLOW_ALL));
if (!$hostNameValidator->isValid($host)) {
throw new Exception\InvalidArgumentException(sprintf(
'%s expects a valid hostname: %s',
__METHOD__,
implode("\n", $hostNameValidator->getMessages())
));
}

public function setPort($port)
{
if ((!is_int($port) && !is_numeric($port))
|| 0 > $port
) {
Expand All @@ -249,13 +250,41 @@ public function setPort($port)
));
}

$this->port= $port;
$this->servers[] = array($host, $port);
return $this;
}

public function getPort()
/**
* Set Servers
*
* @param array $servers list of servers in [] = array($host, $port)
* @return MemcachedOptions
* @throws Exception\InvalidArgumentException
*/
public function setServers(array $servers)
{
foreach ($servers as $server) {
if (!isset($server[0])) {
throw new Exception\InvalidArgumentException('The servers array must contain a host value.');
}

if (!isset($server[1])) {
$this->addServer($server[0]);
} else {
$this->addServer($server[0], $server[1]);
}
}
return $this;
}

/**
* Get Servers
*
* @return array
*/
public function getServers()
{
return $this->port;
return $this->servers;
}

/**
Expand Down
45 changes: 43 additions & 2 deletions test/Storage/Adapter/MemcachedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,53 @@ public function setUp()
}

$this->_options = new Cache\Storage\Adapter\MemcachedOptions();
$this->_storage = new Cache\Storage\Adapter\Memcached();
$this->_storage->setOptions($this->_options);
$this->_storage = new Cache\Storage\Adapter\Memcached($this->_options);

parent::setUp();
}

public function testOptionsAddServer()
{
$options = new Cache\Storage\Adapter\MemcachedOptions();
$options->addServer('127.0.0.1', 11211);
$options->addServer('localhost');
$options->addServer('domain.com', 11215);

$servers = array(
array('127.0.0.1', 11211),
array('localhost', 11211),
array('domain.com', 11215),
);

$this->assertEquals($options->getServers(), $servers);
$memcached = new Cache\Storage\Adapter\Memcached($options);
$this->assertEquals($memcached->getOptions()->getServers(), $servers);
}

public function testOptionsSetServers()
{
$options = new Cache\Storage\Adapter\MemcachedOptions();
$servers = array(
array('127.0.0.1', 12345),
array('localhost', 54321),
array('domain.com')
);

$options->setServers($servers);
$servers[2][1] = 11211;
$this->assertEquals($options->getServers(), $servers);

$memcached = new Cache\Storage\Adapter\Memcached($options);
$this->assertEquals($memcached->getOptions()->getServers(), $servers);
}

public function testNoOptionsSetsDefaultServer()
{
$memcached = new Cache\Storage\Adapter\Memcached();

$this->assertEquals($memcached->getOptions()->getServers(), array(array('localhost', 11211)));
}

public function tearDown()
{
if (!empty($this->_storage)) {
Expand Down

0 comments on commit 41f360c

Please sign in to comment.