/** * * @throws Exception */ protected function __construct() { if (!class_exists('Memcache', false)) { throw new Exception('Class Memcache not Found. php5-memcache may not be installed.'); } self::$_Connection = new Memcache(); foreach (self::$_MemCachePool as $_host => $weight) { list($host, $port) = explode(":", $_host); self::$_Connection->addServer($host, $port, true, $weight); } $results = @self::$_Connection->getExtendedStats(); foreach ($results as $__host => $_stats) { if (empty($_stats)) { list($host, $port) = explode(":", $__host); self::$_Connection->setServerParams($host, $port, 1, -1, false); Log::doLog("Server {$host}:{$port} down, not available."); unset(self::$_MemCachePool[$__host]); } } if (empty(self::$_MemCachePool)) { throw new Exception('No Memcached servers available. All down.'); } register_shutdown_function('MemcacheHandler::close'); }
/** * Callback method for Memcache::failure_callback to use if any Memcache call * on a particular server fails. This method switches off that instance of the * server if the configuration setting `instant_death` is set to `TRUE`. * * @param string hostname * @param integer port * @return void|boolean * @since 3.0.8 */ function _failed_request($hostname, $port) { if (!$this->conf['instant_death']) { return; } // Setup non-existent host $host = FALSE; // Get host settings from configuration foreach ($this->conf['servers'] as $server) { if ($hostname == $server['host'] and $port == $server['port']) { $host = $server; continue; } } if (!$host) { return; } else { return $this->memcache->setServerParams($host['host'], $host['port'], $host['timeout'], $host['retry_interval'], FALSE, array($this, '_failed_request')); } }
/** * Callback method for Memcache::failure_callback to use if any Memcache call * on a particular server fails. This method switches off that instance of the * server if the configuration setting `instant_death` is set to `TRUE`. * @param string $hostname * @param integer $port * @return void|boolean * @since 3.0.8 */ public function _failed_request($hostname, $port) { if (!$this->_config['instant_death']) { return; } // Setup non-existent host $host = false; // Get host settings from configuration foreach ($this->_config['servers'] as $server) { // Merge the defaults, since they won't always be set $server += $this->_default_config; // We're looking at the failed server if ($hostname == $server['host'] and $port == $server['port']) { // Server to disable, since it failed $host = $server; continue; } } if (!$host) { return; } else { return $this->_memcache->setServerParams($host['host'], $host['port'], $host['timeout'], $host['retry_interval'], false, [$this, '_failed_request']); } }
protected function registerMemcacheHandler(Application $app) { $app['session.handler_factory.backing_memcache'] = $app->protect(function ($connections) { $memcache = new \Memcache(); foreach ($connections as $conn) { $args = [$conn['host'] ?: 'localhost', $conn['port'] ?: 11211, $conn['persistent'] ?: false]; if ($conn['weight'] > 0) { $args[] = $conn['weight']; if ($conn['timeout'] > 1) { $args[] = $conn['timeout']; } } call_user_func_array([$memcache, 'addServer'], $args); if ($conn['weight'] <= 0 && $conn['timeout'] > 1) { $memcache->setServerParams($args[0], $args[1], $conn['timeout']); } } return $memcache; }); $app['session.handler_factory.backing_memcached'] = $app->protect(function ($connections) { $memcache = new \Memcached(); foreach ($connections as $conn) { $memcache->addServer($conn['host'] ?: 'localhost', $conn['port'] ?: 11211, $conn['weight'] ?: 0); } return $memcache; }); $app['session.handler_factory.memcache'] = $app->protect(function ($options, $key = 'memcache') use($app) { $connections = $this->parseConnections($options, 'localhost', 11211); $memcache = $app['session.handler_factory.backing_' . $key]($connections); $handlerOptions = []; if (isset($options['expiretime'])) { $handlerOptions['expiretime'] = $options['expiretime']; } if (isset($options['prefix'])) { $handlerOptions['prefix'] = $options['prefix']; } if ($key === 'memcache') { return new MemcacheHandler($memcache, $handlerOptions); } else { return new MemcachedHandler($memcache, $handlerOptions); } }); $app['session.handler_factory.memcached'] = $app->protect(function ($options) use($app) { return $app['session.handler_factory.memcache']($options, 'memcached'); }); }
$memcache_save_path = ''; #addServer foreach $memcache_settings['servers'] foreach ($memcache_settings['servers'] as $server) { #bool Memcache::addServer ( string $host [, int $port = 11211 [, bool $persistent [, int $weight [, int $timeout [, int $retry_interval [, bool $status [, callback $failure_callback [, int $timeoutms ]]]]]]]] ) ### from php.net if ($_GET['debug']) { echo "\n", '$memcache->addServer : ', var_export($server, true), "\n"; } $addServer_status = NULL; $memcache_host = $server['host']; $memcache_port = $server['port']; if ($using_MemcachePool) { $addServer_status = $memcache->addServer($server['host'], $server['port'], $server['udp_port'], $server['persistent'], $server['timeout'], $server['retry_interval']); if (array_key_exists('status', $server) && !$server['status']) { $server['retry_interval'] = -1; #it does not make sense to have any other value for retry_interval if status is false if (!$memcache->setServerParams($server['host'], $server['port'], $server['timeout'], $server['retry_interval'], $server['status']) && $_GET['debug']) { echo "\nFailed to set {$memcache_host} status to offline\n"; } else { if ($_GET['debug']) { echo "\nSet {$memcache_host} status to offline\n"; } } } } else { $addServer_status = $memcache->addServer($server['host'], $server['port'], $server['persistent'], $server['weight'], $server['timeout'], $server['retry_interval'], $server['status'], $server['callback_failure']); } if ($_GET['debug']) { if ($addServer_status) { echo "\nSuccessfully added {$memcache_host}\n"; } else { echo "\nFailed to add {$memcache_host}\n";