54 lines
1.3 KiB
PHP
Executable File
54 lines
1.3 KiB
PHP
Executable File
<?php
|
|
|
|
namespace Illuminate\Cache;
|
|
|
|
use Memcached;
|
|
use RuntimeException;
|
|
|
|
class MemcachedConnector
|
|
{
|
|
/**
|
|
* Create a new Memcached connection.
|
|
*
|
|
* @param array $servers
|
|
* @return \Memcached
|
|
*
|
|
* @throws \RuntimeException
|
|
*/
|
|
public function connect(array $servers)
|
|
{
|
|
$memcached = $this->getMemcached();
|
|
|
|
// 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']
|
|
);
|
|
}
|
|
|
|
$memcachedStatus = $memcached->getVersion();
|
|
|
|
if (! is_array($memcachedStatus)) {
|
|
throw new RuntimeException('No Memcached servers added.');
|
|
}
|
|
|
|
if (in_array('255.255.255', $memcachedStatus) && count(array_unique($memcachedStatus)) === 1) {
|
|
throw new RuntimeException('Could not establish Memcached connection.');
|
|
}
|
|
|
|
return $memcached;
|
|
}
|
|
|
|
/**
|
|
* Get a new Memcached instance.
|
|
*
|
|
* @return \Memcached
|
|
*/
|
|
protected function getMemcached()
|
|
{
|
|
return new Memcached;
|
|
}
|
|
}
|