Skip to content

Commit

Permalink
Making MemcacheEngine understand IPv6 addresses. Fixes #1143
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Sep 26, 2010
1 parent 6d7f2d3 commit 995a14c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
32 changes: 26 additions & 6 deletions cake/libs/cache/memcache.php
Expand Up @@ -81,12 +81,7 @@ function init($settings = array()) {
$return = false;
$this->__Memcache =& new Memcache();
foreach ($this->settings['servers'] as $server) {
$parts = explode(':', $server);
$host = $parts[0];
$port = 11211;
if (isset($parts[1])) {
$port = $parts[1];
}
list($host, $port) = $this->_parseServerString($server);
if ($this->__Memcache->addServer($host, $port)) {
$return = true;
}
Expand All @@ -96,6 +91,31 @@ function init($settings = array()) {
return true;
}

/**
* Parses the server address into the host/port. Handles both IPv6 and IPv4
* addresses
*
* @param string $server The server address string.
* @return array Array containing host, port
*/
function _parseServerString($server) {
if (substr($server, 0, 1) == '[') {
$position = strpos($server, ']:');
if ($position !== false) {
$position++;
}
} else {
$position = strpos($server, ':');
}
$port = 11211;
$host = $server;
if ($position !== false) {
$host = substr($server, 0, $position);
$port = substr($server, $position + 1);
}
return array($host, $port);
}

/**
* Write data for key into cache. When using memcache as your cache engine
* remember that the Memcache pecl extension does not support cache expiry times greater
Expand Down
18 changes: 18 additions & 0 deletions cake/tests/cases/libs/cache/memcache.test.php
Expand Up @@ -134,6 +134,24 @@ function testConnect() {
$this->assertTrue($result);
}

/**
* test connecting to an ipv6 server.
*
* @return void
*/
function testConnectIpv6() {
$Memcache =& new MemcacheEngine();
$result = $Memcache->init(array(
'prefix' => 'cake_',
'duration' => 200,
'engine' => 'Memcache',
'servers' => array(
'[::1]:11211'
)
));
$this->assertTrue($result);
}

/**
* testReadAndWriteCache method
*
Expand Down

0 comments on commit 995a14c

Please sign in to comment.