MemcachedConnector.php
TLDR
The MemcachedConnector.php
file in the Illuminate\Cache
namespace contains the MemcachedConnector
class. This class is responsible for creating and configuring a new Memcached connection.
Methods
connect
This method creates a new Memcached connection by taking an array of server configurations, a connection ID (optional), an array of options (optional), and an array of credentials (optional). It iterates over the server configurations and adds each server to the Memcached connection. It then returns the Memcached connection.
getMemcached
This protected method creates a new instance of the Memcached class by taking a connection ID, an array of credentials, and an array of options as parameters. It creates the Memcached instance, sets the credentials (if provided), and sets the options (if provided). It returns the created Memcached instance.
createMemcachedInstance
This protected method creates a new instance of the Memcached class by taking a connection ID as a parameter. If the connection ID is empty, it creates a new Memcached instance without any arguments. Otherwise, it creates a new Memcached instance with the connection ID as an argument. It returns the created Memcached instance.
setCredentials
This protected method sets the SASL credentials on the Memcached connection by taking a Memcached instance and an array of credentials as parameters. It sets the binary protocol option on the Memcached instance and sets the SASL authentication data using the provided credentials.
Classes
Class: MemcachedConnector
The MemcachedConnector
class is responsible for creating and configuring a new Memcached connection. It contains the connect
, getMemcached
, createMemcachedInstance
, and setCredentials
methods.
<?php
namespace Illuminate\Cache;
use Memcached;
class MemcachedConnector
{
/**
* Create a new Memcached connection.
*
* @param array $servers
* @param string|null $connectionId
* @param array $options
* @param array $credentials
* @return \Memcached
*/
public function connect(array $servers, $connectionId = null, array $options = [], array $credentials = [])
{
$memcached = $this->getMemcached(
$connectionId, $credentials, $options
);
if (! $memcached->getServerList()) {
// For each server in the array, we'll just extract the configuration and add
// the server to the Memcached connection. Once we have added all of these
// servers we'll verify the connection is successful and return it back.
foreach ($servers as $server) {
$memcached->addServer(
$server['host'], $server['port'], $server['weight']
);
}
}
return $memcached;
}
/**
* Get a new Memcached instance.
*
* @param string|null $connectionId
* @param array $credentials
* @param array $options
* @return \Memcached
*/
protected function getMemcached($connectionId, array $credentials, array $options)
{
$memcached = $this->createMemcachedInstance($connectionId);
if (count($credentials) === 2) {
$this->setCredentials($memcached, $credentials);
}
if (count($options)) {
$memcached->setOptions($options);
}
return $memcached;
}
/**
* Create the Memcached instance.
*
* @param string|null $connectionId
* @return \Memcached
*/
protected function createMemcachedInstance($connectionId)
{
return empty($connectionId) ? new Memcached : new Memcached($connectionId);
}
/**
* Set the SASL credentials on the Memcached connection.
*
* @param \Memcached $memcached
* @param array $credentials
* @return void
*/
protected function setCredentials($memcached, $credentials)
{
[$username, $password] = $credentials;
$memcached->setOption(Memcached::OPT_BINARY_PROTOCOL, true);
$memcached->setSaslAuthData($username, $password);
}
}