/** * Create the Memcached connection * * @return void * * @since 12.1 * @throws RuntimeException */ protected function getConnection() { if (!static::isSupported()) { throw new RuntimeException('Memcached Extension is not available'); } $config = JFactory::getConfig(); $host = $config->get('memcached_server_host', 'localhost'); $port = $config->get('memcached_server_port', 11211); // Create the memcached connection if ($config->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 JCacheExceptionConnecting('Could not connect to memcached server'); } }
public function __destruct() { if ($this->instance) { $this->instance->resetServerList(); unset($this->instance); } }
/** * @param \Memcached|null $memcached */ public function setMemcached($memcached = null) { if ($memcached) { $this->memcached = $memcached; } if ($this->memcached) { $this->memcached->resetServerList(); $this->memcached->addServer($this->server, $this->port); } }
/** * Maybe add server connections. * * @since 151216 Memcached utilities. */ protected function maybeAddServerConnections() { if (!$this->Pool) { return; // Not possible. } if ($this->serversDiffer()) { $this->Pool->quit(); $this->Pool->resetServerList(); $this->Pool->addServers($this->servers); } }
public function testDelete() { $this->storage->get('oldkey'); $this->assertArrayHasKey('oldkey', $this->storage->getCasArray(), 'Cas array has not key "oldkey"!'); $this->assertEquals(0, $this->storage->delete('oldkey'), 'Delete failed!'); $this->assertArrayNotHasKey('oldkey', $this->storage->getCasArray(), 'Cas array has key "oldkey"!'); try { self::$memcached->resetServerList(); $this->storage->delete('oldkey'); } catch (StorageException $e) { if (count(self::$memcached->getServerList()) == 0) { self::$memcached->addServers(array(array(MEMCACHED_HOST, MEMCACHED_PORT))); } return; } $this->fail('Storage delete exception test failed'); }
public function getStats() { $stats = $this->handler->getStats(); $servers = $this->servers; foreach ($stats as $key => $stat) { if ($stat['pid'] === -1) { //移除不可用的server unset($servers[$key]); } } //没有可用的server if (empty($servers)) { $this->isConnected = false; return false; } else { $this->handler->resetServerList(); $status = $this->handler->addServers(array_values($servers)); if ($status) { $this->isConnected = true; } return $status; } }
<?php $mem = new Memcached(); $mem->addServer("localhost", 1234); var_dump(count($mem->getServerList())); $mem->resetServerList(); var_dump($mem->getServerList());