Back to MemcachedStorage class

Method getConnection

protected void
getConnection
()
Create the Memcached connection
Returns
  • void
Since
  • 3.0.0
-
  • \RuntimeException

Method getConnection - Source code

/**
 * Create the Memcached connection
 *
 * @return  void
 *
 * @since   3.0.0
 * @throws  \RuntimeException
 */
protected function getConnection()
{
    if (!static::isSupported()) {
        throw new \RuntimeException('Memcached Extension is not available');
    }
    $app = Factory::getApplication();
    $host = $app->get('memcached_server_host', 'localhost');
    $port = $app->get('memcached_server_port', 11211);
    // Create the memcached connection
    if ($app->get('memcached_persist', true)) {
        static::$_db = new \Memcached($this->_hash);
        $servers = static::$_db->getServerList();
        if ($servers && ($servers[0]['host'] != $host || $servers[0]['port'] != $port)) {
            static::$_db->resetServerList();
            $servers = array();
        }
        if (!$servers) {
            static::$_db->addServer($host, $port);
        }
    } else {
        static::$_db = new \Memcached();
        static::$_db->addServer($host, $port);
    }
    static::$_db->setOption(\Memcached::OPT_COMPRESSION, $this->_compress);
    $stats = static::$_db->getStats();
    $result = !empty($stats["{$host}:{$port}"]) && $stats["{$host}:{$port}"]['pid'] > 0;
    if (!$result) {
        // Null out the connection to inform the constructor it will need to attempt to connect if this class is instantiated again
        static::$_db = null;
        throw new CacheConnectingException('Could not connect to memcached server');
    }
}