protected static function MemcacheStorageAPI::connectToCluster in Memcache Storage 7
Connect to memcached cluster and return memcached object.
Parameters
$cluster_name: Name of memcached cluster.
Return value
object Connected memcached object or FALSE if no connection.
2 calls to MemcacheStorageAPI::connectToCluster()
- MemcacheStorageAPI::flushCluster in ./
memcache_storage.api.inc - Immediately invalidates all existing items in a selected memcached cluster.
- MemcacheStorageAPI::openConnection in ./
memcache_storage.api.inc - Connect to memcached clustes and returns memcached object on success.
File
- ./
memcache_storage.api.inc, line 53 - Provide class that processes memcached operations.
Class
- MemcacheStorageAPI
- Integrates with memcache API.
Code
protected static function connectToCluster($cluster_name) {
// Load available servers and its cluster names from settings.php.
$server_list = variable_get('memcache_servers', array(
'127.0.0.1:11211' => 'default',
));
// Search for all servers matching bin cluster.
$cluster_servers = array();
foreach ($server_list as $server => $name) {
if ($name == $cluster_name) {
$cluster_servers[] = $server;
}
}
// No servers found for this cache bin.
if (empty($cluster_servers)) {
// Log error.
self::logError('Could not find server(s) for cluster %cluster. Check your settings.php configuration.', array(
'%cluster' => $cluster_name,
));
// Return not initialized value.
return FALSE;
}
$preferred = variable_get('memcache_extension');
if (!empty($preferred) && class_exists($preferred)) {
$extension = $preferred;
}
elseif (class_exists('Memcache')) {
$extension = 'Memcache';
}
elseif (class_exists('Memcached')) {
$extension = 'Memcached';
}
// Check if Memcache extension enabled.
if (empty($extension)) {
// Log error.
self::logError('<a href="@url_memcache">Memcache</a> or <a href="@url_memcached">Memcached</a> extension is not available.', array(
'@url_memcache' => 'http://pecl.php.net/package/memcache',
'@url_memcached' => 'http://pecl.php.net/package/memcache',
));
return FALSE;
}
// Create new memcached connection.
$memcache = new $extension();
// Collect all servers thas is unable to connect.
$failed_connections = array();
// Connect to every server.
foreach ($cluster_servers as $server) {
list($host, $port) = explode(':', $server);
// Support unix sockets in the format 'unix:///path/to/socket'.
if ($host == 'unix') {
// PECL Memcache supports 'unix:///path/to/socket' path in ::addServer function,
// while PECL Memcached use only '/path/to/socket' string for the same purpose.
if ($extension == 'Memcache') {
$host = $server;
}
elseif ($extension == 'Memcached') {
$host = substr($server, 7);
}
// Port is always 0 for unix sockets.
$port = 0;
}
// Adding new server for memcached connection.
$persistent = variable_get('memcache_storage_persistent_connection', FALSE);
$connected = @$memcache
->addServer($host, $port, $persistent);
if (!$connected) {
// Mark connection to this server as 'failed'.
$failed_connections[] = $server;
// Log error if unable to connect to server.
self::logError('Could not connect to server %server (port %port).', array(
'%server' => $host,
'%port' => $port,
));
}
}
// If memcached is unable to connect to all servers it means that
// we have no connections at all, and could not start memcached connection.
if (sizeof($failed_connections) == sizeof($cluster_servers)) {
return FALSE;
}
// Provide compressThreshold support for PECL Memcached library.
if ($extension == 'Memcache') {
// For more information see http://www.php.net/manual/en/memcache.setcompressthreshold.php.
$compress_threshold = variable_get('memcache_storage_compress_threshold', array(
'threshold' => 20000,
'min_savings' => 0.2,
));
if (isset($compress_threshold['threshold']) && isset($compress_threshold['min_savings'])) {
$memcache
->setCompressThreshold($compress_threshold['threshold'], $compress_threshold['min_savings']);
}
}
elseif ($extension == 'Memcached') {
$default_options = array(
Memcached::OPT_COMPRESSION => FALSE,
Memcached::OPT_DISTRIBUTION => Memcached::DISTRIBUTION_CONSISTENT,
);
// For more info about memcached constants see http://www.php.net/manual/en/memcached.constants.php.
$memcached_options = variable_get('memcache_options', array());
$memcached_options += $default_options;
// Add SASL support.
$sasl_auth = variable_get('memcache_sasl_auth', array());
if (!empty($sasl_auth['user']) && !empty($sasl_auth['password'])) {
// SASL auth works only with binary protocol.
// Protocol has to be set before we call setSaslAuthData(), so we set
// it here specially.
$memcache
->setOption(Memcached::OPT_BINARY_PROTOCOL, TRUE);
$memcache
->setSaslAuthData($sasl_auth['user'], $sasl_auth['password']);
}
// Set memcached options.
// See http://www.php.net/manual/en/memcached.setoption.php.
foreach ($memcached_options as $key => $value) {
$memcache
->setOption($key, $value);
}
}
return $memcache;
}