summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeremy2017-01-24 03:31:54 (GMT)
committerJeremy Andrews2017-01-24 03:31:54 (GMT)
commit0634fc47eb8de8845933423a3c8a2f8640ab5238 (patch)
tree1dc029f364cbcc8b056e85e873e6e47a4776d451
parent62f8c49063588fcaf7a6c893711ed450d6d616a3 (diff)
Issue #1977452 by markpavlitski, Jeremy: Add persistent connection support for the PECL Memcached extension7.x-1.6-rc11.6-rc1
-rw-r--r--README.txt8
-rw-r--r--dmemcache.inc42
-rw-r--r--memcache.install12
3 files changed, 47 insertions, 15 deletions
diff --git a/README.txt b/README.txt
index 685345b..d6b2f4b 100644
--- a/README.txt
+++ b/README.txt
@@ -220,6 +220,14 @@ When enabled, the Memcache module will add its own 'Drupal-PageCache-Memcache'
header. When cached pages are served out of the cache the header will include an
'age=' value indicating how many seconds ago the page was stored in the cache.
+## PERSISTENT CONNECTIONS ##
+
+As of 7.x-1.5, the memcache module uses peristent connections by default. If
+this causes you problems you can disable persistent connections by adding the
+following to your settings.php:
+
+ $conf['memcache_persistent'] = FALSE;
+
## EXAMPLES ##
Example 1:
diff --git a/dmemcache.inc b/dmemcache.inc
index 5f0b441..d55cb6f 100644
--- a/dmemcache.inc
+++ b/dmemcache.inc
@@ -726,14 +726,19 @@ function dmemcache_unserialize() {
/**
* Return a new memcache instance.
*/
-function dmemcache_instance() {
+function dmemcache_instance($bin = 'cache') {
static $error = FALSE;
$extension = dmemcache_extension();
if ($extension == 'Memcache') {
return new Memcache();
}
elseif ($extension == 'Memcached') {
- $memcache = new Memcached();
+ if (variable_get('memcache_persistent', TRUE)) {
+ $memcache = new Memcached($bin);
+ }
+ else {
+ $memcache = new Memcached;
+ }
$default_opts = array(
Memcached::OPT_COMPRESSION => FALSE,
Memcached::OPT_DISTRIBUTION => Memcached::DISTRIBUTION_CONSISTENT,
@@ -786,14 +791,11 @@ function dmemcache_connect($memcache, $server, $connection) {
register_shutdown_function('watchdog', 'memcache', 'You have specified an invalid address of "!server" in settings.php. Please review README.txt for proper configuration.', array('!server' => $server, '!ip' => t('127.0.0.1:11211'), '!host' => t('localhost:11211'), '!socket' => t('unix:///path/to/socket')), WATCHDOG_WARNING);
}
+ if (!isset($memcache_persistent)) {
+ $memcache_persistent = variable_get('memcache_persistent', TRUE);
+ }
+
if ($extension == 'Memcache') {
- // Allow persistent connection via Memcache extension -- note that this
- // module currently doesn't support persistent connections with the
- // Memcached extension. See http://drupal.org/node/822316#comment-4427676
- // for details.
- if (!isset($memcache_persistent)) {
- $memcache_persistent = variable_get('memcache_persistent', FALSE);
- }
// Support unix sockets of the format 'unix:///path/to/socket'.
if ($host == 'unix') {
@@ -839,7 +841,25 @@ function dmemcache_connect($memcache, $server, $connection) {
else if (!isset($port)) {
register_shutdown_function('watchdog', 'memcache', 'You have specified an invalid address of "!server" in settings.php which does not include a port. Please review README.txt for proper configuration. You must specify both a server address and port such as "!ip" or "!host", or a unix socket such as "!socket".', array('!server' => $server, '!ip' => t('127.0.0.1:11211'), '!host' => t('localhost:11211'), '!socket' => t('unix:///path/to/socket')), WATCHDOG_WARNING);
}
- $rc = $memcache->addServer($host, $port);
+ if ($memcache_persistent) {
+ $servers = $memcache->getServerList();
+ $match = FALSE;
+ foreach ($servers as $s) {
+ if ($s['host'] == $host && $s['port'] == $port) {
+ $match = TRUE;
+ break;
+ }
+ }
+ if (!$match) {
+ $rc = $memcache->addServer($host, $port);
+ }
+ else {
+ $rc = TRUE;
+ }
+ }
+ else {
+ $rc = $memcache->addServer($host, $port);
+ }
}
else {
$rc = FALSE;
@@ -919,7 +939,7 @@ function dmemcache_object($bin = NULL, $flush = FALSE) {
}
else {
// Create a new memcache object for each cluster.
- $memcache = dmemcache_instance();
+ $memcache = dmemcache_instance($bin);
// Track whether or not we've opened any memcache connections.
$connection = FALSE;
diff --git a/memcache.install b/memcache.install
index f57ace7..91b7a68 100644
--- a/memcache.install
+++ b/memcache.install
@@ -30,13 +30,15 @@ function memcache_enable() {
}
// Make a test connection to all configured memcache servers.
$memcache_servers = variable_get('memcache_servers', array('127.0.0.1:11211' => 'default'));
- $memcache = dmemcache_instance();
foreach ($memcache_servers as $server => $bin) {
+ $memcache = dmemcache_instance($bin);
if (dmemcache_connect($memcache, $server, FALSE) === FALSE) {
$error = TRUE;
}
else {
- dmemcache_close($memcache);
+ if (!variable_get('memcache_persistent', TRUE)) {
+ dmemcache_close($memcache);
+ }
}
}
}
@@ -111,13 +113,15 @@ function memcache_requirements($phase) {
// Make a test connection to all configured memcache servers.
$memcache_servers = variable_get('memcache_servers', array('127.0.0.1:11211' => 'default'));
- $memcache = dmemcache_instance();
foreach ($memcache_servers as $server => $bin) {
+ $memcache = dmemcache_instance($bin);
if (dmemcache_connect($memcache, $server, FALSE) === FALSE) {
$errors[] = $t('Failed to connect to memcached server instance at %server.', array('%server' => $server));
}
else {
- dmemcache_close($memcache);
+ if (!variable_get('memcache_persistent', TRUE)) {
+ dmemcache_close($memcache);
+ }
}
}