Connects to Memcached service.

@throws ServiceException If any error occurs during
Memcache initialization

@return Memcached A self instance for chaining method calls.
497295
public function connect()
    {
        $this->_service = new SplMemcached();
        $this->_service->addServer($this->_host, $this->_port);
        $stats = $this->_service->getStats();
        if (isset($stats[$this->_host.":".$this->_port])) {
            $this->_connected = true;
        } else {
            throw new ServiceException(
                "Unable to connect to Memcached service"
            );
        }

        return $this;
    }
119434
private function newMemcachedServers(Memcached $memcached, array $servers)
    {
        $serverIndex = [];
        $existingServers = $memcached->getServerList();

        foreach ($existingServers as $server) {
            $serverIndex[$server['host'] . ':' . $server['port']] = true;
        }

        foreach ($servers as $configServer) {
            $server = [
                'host' => array_get($configServer, 'host', ''),
                'port' => array_get($configServer, 'port', 11211),
                'weight' => array_get($configServer, 'weight', 0),
            ];

            if (!isset($serverIndex[$server['host'] . ':' . $server['port']])) {
                yield $server;
            }
        }
    }
109723
private Map<ServiceInstanceToken, InstanceHealthPair> getCacheServiceInstances() {
        if (instanceCache == null) {
            synchronized (this) {
                if (instanceCache == null) {
                    instanceCache = new ConcurrentHashMap<ServiceInstanceToken, InstanceHealthPair>();
                    initJobTasks();
                }
            }
        }
        return instanceCache;
    }
413392
protected function connect()
    {
        if (!is_object(self::$instance)) {
            self::$instance = new \Memcached();

            if (!empty($this->options['option'])) {
                self::$instance->setOptions($this->options['option']);
            }

            // Set connection timeout ms
            if ($this->options['timeout'] > 0) {
                self::$instance->setOption(\Memcached::OPT_CONNECT_TIMEOUT, $this->options['timeout']);
            }

            // Cluster connections
            $hosts = explode(',', $this->options['host']);
            $ports = explode(',', $this->options['port']);
            if (empty($ports[0])) {
                $ports[0] = 11211;
            }

            // connection establishment
            $servers = [];
            foreach ((array) $hosts as $i => $host) {
                $servers[] = [$host, (isset($ports[$i]) ? $ports[$i] : $ports[0]), 1];
            }

            self::$instance->addServers($servers);

            if ('' != $this->options['username']) {
                self::$instance->setOption(\Memcached::OPT_BINARY_PROTOCOL, true);
                self::$instance->setSaslAuthData($this->options['username'], $this->options['password']);
            }
        }
    }
922121
protected function registerMemcachedBucketCacheDriver(): void
    {
        $this->app['cache']->extend('couchbase-memcached', function ($app, $config) {
            $prefix = $app['config']['cache.prefix'];
            $credential = $config['sasl'] ?? [];
            $memcachedBucket = $this->app['couchbase.memcached.connector']
                ->connect($config['servers']);

            return new Repository(
                new MemcachedBucketStore(
                    $memcachedBucket,
                    strval($prefix),
                    $config['servers'],
                    $credential
                )
            );
        });
    }
438097
public function get_memcache()
    {
        if (!isset($this->memcache)) {
            // no memcache support in PHP
            if (!class_exists('Memcache')) {
                $this->memcache = false;
                return false;
            }

            $this->memcache     = new Memcache;
            $this->memcache_init();

            // test connection and failover (will result in $this->mc_available == 0 on complete failure)
            $this->memcache->increment('__CONNECTIONTEST__', 1);  // NOP if key doesn't exist

            if (!$this->mc_available) {
                $this->memcache = false;
            }
        }

        return $this->memcache;
    }
1132131
public function unserialize($serialized){

		list($this->_host, $this->_port) = explode(':', unserialize($serialized));

		$this->_Memcache = new \Memcache();

		if(!Memcache::connect($this->_Memcache, $this->_host, $this->_port)){

			throw new CacheException('Failed to connect to Memcache');
		}
	}
742463
private function connect()
    {
        $memcache = new Memcached();
        $memcache->addServer($this->host, $this->port);

        if (null !== $this->prefix) {
            $memcache->setOption(Memcached::OPT_PREFIX_KEY, $this->prefix);
        }

        $this->memcached = $memcache;
    }
888376
private function connect()
    {

        if ($this->config['host'] && $this->config['port']) {
            // establish the memcache connection
            $this->memcache = new \Memcache;
            $this->memcache->pconnect(
                $this->config['host'],
                $this->config['port']
            );

        } else {
            throw new \Exception('Insufficient connection parameters');
        }
    }
570776
protected function connect()
    {
        if (!is_object(self::$instance)) {
            self::$instance = new \Memcache();

            // Cluster connections
            $hosts = explode(',', $this->options['host']);
            $ports = explode(',', $this->options['port']);
            if (empty($ports[0])) {
                $ports[0] = 11211;
            }

            // connection establishment
            foreach ((array) $hosts as $i => $host) {
                $port = isset($ports[$i]) ? $ports[$i] : $ports[0];
                $this->options['timeout'] > 0 ?
                    self::$instance->addServer($host, $port, $this->options['persistent'], 1, $this->options['timeout']) :
                    self::$instance->addServer($host, $port, $this->options['persistent'], 1);
            }
        }
    }
659191
public function getCache()
    {
        if ($this->cache) {
            return $this->cache;
        }

        if (property_exists($this, 'app') && $this->app instanceof ServiceContainer
            && isset($this->app['cache']) && $this->app['cache'] instanceof CacheInterface) {
            return $this->cache = $this->app['cache'];
        }

        return $this->cache = $this->createDefaultCache();
    }
698855
public function register()
    {
        Zilf::$container->register(
            'cache', function () {
                return new CacheManager();
            }
        );

        Zilf::$container->register(
            'cache.store', function ($app) {
                return Zilf::$container->getShare('cache')->driver();
            }
        );

        Zilf::$container->register(
            'memcached.connector', function () {
                return new MemcachedConnector;
            }
        );
    }
1155216
public function connect() {
		if (!$this->_connect()) {
			throw new RedisSourceException(__d('redis', 'Could not connect.'));
		}

		if (!$this->_authenticate()) {
			throw new RedisSourceException(__d('redis', 'Could not authenticate.'));
		}

		if (!$this->_select()) {
			throw new RedisSourceException(__d('redis', 'Could not select.'));
		}

		if (!$this->_setPrefix()) {
			throw new RedisSourceException(__d('redis', 'Could not set prefix.'));
		}

		$this->connected = true;

		return $this->connected;
	}
773938
public function connect()
    {
        if ($this->EnableAPCu && extension_loaded('apcu') && ini_get('apc.enabled')) {
            $this->Using = 'APCu';
            return true;
        }
        if ($this->EnableMemcached && extension_loaded('memcached')) {
            $this->WorkingData = new \Memcached();
            if ($this->WorkingData->addServer($this->MemcachedHost, $this->MemcachedPort)) {
                $this->Using = 'Memcached';
                return true;
            }
            $this->WorkingData = null;
        }
        if ($this->EnableRedis && extension_loaded('redis')) {
            $this->WorkingData = new \Redis();
            if ($this->WorkingData->connect($this->RedisHost, $this->RedisPort, $this->RedisTimeout)) {
                $this->Using = 'Redis';
                return true;
            }
            $this->WorkingData = null;
        }
        if ($this->EnablePDO && extension_loaded('pdo')) {
            try {
                $PDO = new \PDO($this->PDOdsn, $this->PDOusername, $this->PDOpassword);
            } catch (\PDOException $Exception) {
                $this->Exceptions[] = $Exception->getMessage();
                return false;
            }
            if (is_object($PDO)) {
                $this->WorkingData = $PDO;
                $this->Using = 'PDO';
                return true;
            }
            unset($PDO);
        }
        if ($this->FFDefault) {
            if (is_file($this->FFDefault)) {
                if (is_readable($this->FFDefault) && is_writable($this->FFDefault)) {
                    $this->Using = 'FF';
                    if (!$Filesize = filesize($this->FFDefault)) {
                        $this->WorkingData = [];
                        return $this->Modified = true;
                    }
                    $Data = '';
                    $Handle = false;
                    $Start = time();
                    while (true) {
                        $Handle = fopen($this->FFDefault, 'rb');
                        if ($Handle !== false || (time() - $Start) > self::FLOCK_TIMEOUT) {
                            break;
                        }
                    }
                    if ($Handle === false) {
                        return false;
                    }
                    $Locked = false;
                    while (true) {
                        if ($Locked = flock($Handle, LOCK_EX | LOCK_NB) || (time() - $Start) > self::FLOCK_TIMEOUT) {
                            break;
                        }
                    }
                    if (!$Locked) {
                        fclose($Handle);
                        return false;
                    }
                    $Size = ($Filesize && self::BLOCKSIZE) ? ceil($Filesize / self::BLOCKSIZE) : 0;
                    $Step = 0;
                    while ($Step < $Size) {
                        $Data .= fread($Handle, self::BLOCKSIZE);
                        $Step++;
                    }
                    flock($Handle, LOCK_UN);
                    fclose($Handle);
                    $Data = $Data ? unserialize($Data) : [];
                    $this->WorkingData = is_array($Data) ? $Data : [];
                    return true;
                }
            } else {
                $Parent = dirname($this->FFDefault);
                if (is_dir($Parent) && is_readable($Parent) && is_writable($Parent)) {
                    $this->WorkingData = [];
                    $this->Using = 'FF';
                    return $this->Modified = true;
                }
            }
            return false;
        }
        return is_array($this->WorkingData);
    }
97108
private function initializeMemCached()
    {
        $this->memcached = new \Memcached();

        // support multiple hosts using semicolon to separate hosts
        $hosts = explode(';', $this->host);

        // different ports for each hosts the same way
        $ports = explode(';', $this->port);

        if (count($ports) < 1) {
            $ports = array_fill(0, count($hosts), self::DEFAULT_PORT);
        } elseif (count($ports) === 1) {
            // if we have just one port, use it for all hosts
            $usedPort = $ports[0];
            $ports    = array_fill(0, count($hosts), $usedPort);
        }

        foreach ($hosts as $i => $host) {
            if (!isset($ports[$i])) {
                /*
                 * if we have a difference between the count of hosts and
                 * the count of ports, use the default port to fill the gap
                 */
                $ports[$i] = self::DEFAULT_PORT;
            }

            $this->memcached->addServer($host, $ports[$i]);
        }
    }
794932
public function connect()
	{
		//check if there is a valid service instance
		if ( ! $this->validService()) 
		{

			//put this in try...catch block for better error handling
			try{
				
				//make a connection attempt
				$this->service = new \MySQLi($this->host,$this->username,$this->password,$this->database,$this->port);

				//check is the connection attempt returned an error
				if ($this->service->connect_error) 
				{
					//throw exception
					throw new MySQLException("Unable to connect to Database service with the provided settings");

				}

				//set the connection variable to true
				$this->connected = true;

			}

			//diplay error message
			catch(MySQLException $MySQLExceptionObject){

				//display error message
				$MySQLExceptionObject->errorShow();

			}
			
		}

		//return this connection instance
		return $this;

	}
437470
public function get($id)
    {
        if (function_exists('memcache_get')) {
            return $this->memcache->get($id);
        }

        throw new MemcacheException(
            sprintf('key id memcache "%s" not found, are you sure memcache_get function already loaded ?', $id)
        );
    }