Skip to content

Commit 0634fc4

Browse files
JeremyJeremy Andrews
Jeremy
authored and
Jeremy Andrews
committed
Issue #1977452 by markpavlitski, Jeremy: Add persistent connection support for the PECL Memcached extension
1 parent 62f8c49 commit 0634fc4

File tree

3 files changed

+47
-15
lines changed

3 files changed

+47
-15
lines changed

README.txt

+8
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,14 @@ When enabled, the Memcache module will add its own 'Drupal-PageCache-Memcache'
220220
header. When cached pages are served out of the cache the header will include an
221221
'age=' value indicating how many seconds ago the page was stored in the cache.
222222

223+
## PERSISTENT CONNECTIONS ##
224+
225+
As of 7.x-1.5, the memcache module uses peristent connections by default. If
226+
this causes you problems you can disable persistent connections by adding the
227+
following to your settings.php:
228+
229+
$conf['memcache_persistent'] = FALSE;
230+
223231
## EXAMPLES ##
224232

225233
Example 1:

dmemcache.inc

+31-11
Original file line numberDiff line numberDiff line change
@@ -726,14 +726,19 @@ function dmemcache_unserialize() {
726726
/**
727727
* Return a new memcache instance.
728728
*/
729-
function dmemcache_instance() {
729+
function dmemcache_instance($bin = 'cache') {
730730
static $error = FALSE;
731731
$extension = dmemcache_extension();
732732
if ($extension == 'Memcache') {
733733
return new Memcache();
734734
}
735735
elseif ($extension == 'Memcached') {
736-
$memcache = new Memcached();
736+
if (variable_get('memcache_persistent', TRUE)) {
737+
$memcache = new Memcached($bin);
738+
}
739+
else {
740+
$memcache = new Memcached;
741+
}
737742
$default_opts = array(
738743
Memcached::OPT_COMPRESSION => FALSE,
739744
Memcached::OPT_DISTRIBUTION => Memcached::DISTRIBUTION_CONSISTENT,
@@ -786,14 +791,11 @@ function dmemcache_connect($memcache, $server, $connection) {
786791
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);
787792
}
788793

794+
if (!isset($memcache_persistent)) {
795+
$memcache_persistent = variable_get('memcache_persistent', TRUE);
796+
}
797+
789798
if ($extension == 'Memcache') {
790-
// Allow persistent connection via Memcache extension -- note that this
791-
// module currently doesn't support persistent connections with the
792-
// Memcached extension. See http://drupal.org/node/822316#comment-4427676
793-
// for details.
794-
if (!isset($memcache_persistent)) {
795-
$memcache_persistent = variable_get('memcache_persistent', FALSE);
796-
}
797799

798800
// Support unix sockets of the format 'unix:///path/to/socket'.
799801
if ($host == 'unix') {
@@ -839,7 +841,25 @@ function dmemcache_connect($memcache, $server, $connection) {
839841
else if (!isset($port)) {
840842
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);
841843
}
842-
$rc = $memcache->addServer($host, $port);
844+
if ($memcache_persistent) {
845+
$servers = $memcache->getServerList();
846+
$match = FALSE;
847+
foreach ($servers as $s) {
848+
if ($s['host'] == $host && $s['port'] == $port) {
849+
$match = TRUE;
850+
break;
851+
}
852+
}
853+
if (!$match) {
854+
$rc = $memcache->addServer($host, $port);
855+
}
856+
else {
857+
$rc = TRUE;
858+
}
859+
}
860+
else {
861+
$rc = $memcache->addServer($host, $port);
862+
}
843863
}
844864
else {
845865
$rc = FALSE;
@@ -919,7 +939,7 @@ function dmemcache_object($bin = NULL, $flush = FALSE) {
919939
}
920940
else {
921941
// Create a new memcache object for each cluster.
922-
$memcache = dmemcache_instance();
942+
$memcache = dmemcache_instance($bin);
923943

924944
// Track whether or not we've opened any memcache connections.
925945
$connection = FALSE;

memcache.install

+8-4
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@ function memcache_enable() {
3030
}
3131
// Make a test connection to all configured memcache servers.
3232
$memcache_servers = variable_get('memcache_servers', array('127.0.0.1:11211' => 'default'));
33-
$memcache = dmemcache_instance();
3433
foreach ($memcache_servers as $server => $bin) {
34+
$memcache = dmemcache_instance($bin);
3535
if (dmemcache_connect($memcache, $server, FALSE) === FALSE) {
3636
$error = TRUE;
3737
}
3838
else {
39-
dmemcache_close($memcache);
39+
if (!variable_get('memcache_persistent', TRUE)) {
40+
dmemcache_close($memcache);
41+
}
4042
}
4143
}
4244
}
@@ -111,13 +113,15 @@ function memcache_requirements($phase) {
111113

112114
// Make a test connection to all configured memcache servers.
113115
$memcache_servers = variable_get('memcache_servers', array('127.0.0.1:11211' => 'default'));
114-
$memcache = dmemcache_instance();
115116
foreach ($memcache_servers as $server => $bin) {
117+
$memcache = dmemcache_instance($bin);
116118
if (dmemcache_connect($memcache, $server, FALSE) === FALSE) {
117119
$errors[] = $t('Failed to connect to memcached server instance at %server.', array('%server' => $server));
118120
}
119121
else {
120-
dmemcache_close($memcache);
122+
if (!variable_get('memcache_persistent', TRUE)) {
123+
dmemcache_close($memcache);
124+
}
121125
}
122126
}
123127

0 commit comments

Comments
 (0)