Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Making MemcacheEngine understand IPv6 addresses. Fixes #1143

  • Loading branch information...
commit 995a14c14039f3d76cabe1b6a53d7b0b0486b155 1 parent 6d7f2d3
@markstory markstory authored
View
32 cake/libs/cache/memcache.php
@@ -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;
}
@@ -97,6 +92,31 @@ function init($settings = array()) {
}
/**
+ * 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
* than 30 days in the future. If you wish to create cache entries that do not expire, set the duration
View
18 cake/tests/cases/libs/cache/memcache.test.php
@@ -135,6 +135,24 @@ function testConnect() {
}
/**
+ * 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
*
* @access public
Please sign in to comment.
Something went wrong with that request. Please try again.