Commit 0634fc47 authored by Jeremy's avatar Jeremy Committed by Jeremy

Issue #1977452 by markpavlitski, Jeremy: Add persistent connection support for...

Issue #1977452 by markpavlitski, Jeremy: Add persistent connection support for the PECL Memcached extension
parent 62f8c490
......@@ -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:
......
......@@ -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;
......
......@@ -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);
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment