本文整理汇总了PHP中Memcached::getServerList方法的典型用法代码示例。如果您正苦于以下问题:PHP Memcached::getServerList方法的具体用法?PHP Memcached::getServerList怎么用?PHP Memcached::getServerList使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Memcached
的用法示例。
在下文中一共展示了Memcached::getServerList方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: __construct
public function __construct()
{
$this->mem = new Memcached(static::$servers_id);
if (count($this->mem->getServerList()) == 0) {
$this->mem->addServer("", 11211);
}
}
示例2: __construct
/**
* @param array $servers List of servers
* @param int $expire Expiration time, defaults to 3600 seconds
* @throws \Exception
*/
public function __construct($servers = [], $expire = 3600)
{
// Set expiration time
$this->expire = $expire;
// Create memcached object
$this->cache = new \Memcached();
// Check if there already are servers added, according to the manual
// http://php.net/manual/en/memcached.addservers.php no duplication checks
// are made. Since we have at least one connection we don't need to add
// more servers and maybe add duplicates.
if (count($this->cache->getServerList()) === 0) {
// Add servers
$this->cache->addServers($servers);
}
// Get server stats
$stats = $this->cache->getStats();
// Loop through servers
foreach ($stats as $stat) {
// Check if pid is more than 0, if pid is -1 connection isn't working
if ($stat['pid'] > 0) {
// Return true to avoid the exception below
return true;
}
}
// If we end up here we don't have a working connection. Throw an exception that
// will be handled by the method calling this connect method. A working cache is
// NOT a requirement for the application to run so it's important to handle the
// exception and let the application run. Suggestion: if the exception below is
// thrown a new NullCache should be created
throw new \Exception('Unable to connect to Memcache(d) backend');
}
示例3: __construct
public function __construct($persistent = true)
{
/*
* Caching can be disabled in the config file if you aren't able to run
* a memcached instance. In that case this class will still get used,
* but we will pretend that we have a perpetually empty cache.
*/
if (defined("USE_MEMCACHED") && USE_MEMCACHED != false) {
$servers = null;
if ($persistent === true) {
/*
* Since prefix should be unique across RTK installations, we can
* use the server hostname + port + prefix as a persistent ID.
* Hostname + port are included because otherwise changing the
* memcached server would NOT change the persistent connection,
* and you'd end up using the old server.
*/
$persistentID = MEMCACHED_HOST . ":" . MEMCACHED_PORT . "_" . MEMCACHED_PREFIX;
$this->cache = new Memcached($persistentID);
$serverList = $this->cache->getServerList();
foreach ($serverList as $entry) {
$servers[] = $entry['host'] . ":" . $entry['port'];
}
} else {
//Not running Memcached in persistent mode.
$this->cache = new Memcached();
}
if ($persistent !== true || !is_array($servers) || !in_array(MEMCACHED_HOST . ":" . MEMCACHED_PORT, $servers)) {
$this->cache->addServer(MEMCACHED_HOST, MEMCACHED_PORT);
}
}
}
示例4: getConnection
/**
* 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');
}
}
示例5: __construct
/**
* Construct the driver instance.
*
* @param Config $config The instance config
*/
public function __construct(Config $config, Instance $instance)
{
$this->instance = $instance;
$this->client = new \Memcached($config->name);
if (count($this->client->getServerList()) === 0) {
$this->client->addServers($config->servers->toArray());
}
}
示例6: getMemcacheKeys
/**
* @return array
*/
protected function getMemcacheKeys()
{
$keys = [];
foreach ($this->memcache->getServerList() as $server) {
$keys = array_merge($keys, $this->getMemcacheKeysForHost($server[MemcacheConfigConstants::SERVER_HOST], $server[MemcacheConfigConstants::SERVER_PORT]));
}
return $keys;
}
示例7: setUpBeforeClass
public static function setUpBeforeClass()
{
self::$memcached = new \Memcached();
self::$memcached->setOptions(array(\Memcached::OPT_TCP_NODELAY => true, \Memcached::OPT_NO_BLOCK => true, \Memcached::OPT_CONNECT_TIMEOUT => 1000));
if (count(self::$memcached->getServerList()) == 0) {
self::$memcached->addServers(array(array(MEMCACHED_HOST, MEMCACHED_PORT)));
}
}
示例8: setConnection
/**
* Set connection
*
* @param \Memcached $memcached
*
* @return $this
*
*/
public function setConnection(\Memcached $memcached)
{
$this->connection = $memcached;
if (!count($this->connection->getServerList())) {
$this->configureServers();
}
$this->setOptions();
return $this;
}
示例9: _addServer
protected function _addServer($server, $port, $persist, $weight, $timeout)
{
$list = $this->_connection->getServerList();
foreach ($list as $srv) {
if ($srv['host'] === $server && (int) $srv['port'] === (int) $port && (!isset($srv['weight']) || (int) $srv['weight'] === (int) $weight)) {
return;
}
}
$this->_connection->addServer($server, $port, $weight);
}
示例10: __construct
/**
* @param $server
* @param $port
* @param $userName
* @param $password
* @param $bucket
*/
public function __construct($server, $port, $userName, $password, $bucket)
{
$this->_instance = new \Memcached();
$this->_instance->setOption(\Memcached::SERIALIZER_JSON, TRUE);
$this->_instance->setOption(\Memcached::OPT_COMPRESSION, FALSE);
$this->_instance->setOption(\Memcached::OPT_CONNECT_TIMEOUT, 500);
$this->_instance->setOption(\Memcached::OPT_POLL_TIMEOUT, 500);
$this->_instance->setOption(\Memcached::OPT_TCP_NODELAY, TRUE);
$this->_instance->setOption(\Memcached::OPT_NO_BLOCK, TRUE);
if (!count($this->_instance->getServerList())) {
$this->_instance->addServer($server, $port);
}
}
示例11: __construct
/**
* @param ConfigurationInterface $configuration
*/
protected function __construct(ConfigurationInterface $configuration)
{
$this->configuration = $configuration;
$this->cache = new \Memcached();
$serverList = $this->cache->getServerList();
if (empty($serverList)) {
$this->cache->addServer($this->configuration->getHost(), $this->configuration->getPort());
}
$this->cache->setOption(\Memcached::OPT_BINARY_PROTOCOL, true);
if ($this->configuration->shouldCheckConnection()) {
$this->checkConnection();
}
}
示例12: getHandler
/**
* Get Mamcached Handler
*
* @return \Memcached
*/
public function getHandler()
{
if (!$this->handler) {
$persistentId = isset($this->settings['persistent']) ? $this->settings['persistent'] : null;
$this->handler = new \Memcached($persistentId);
if (!$this->handler->getServerList()) {
$this->handler->addServers($this->settings['servers']);
}
if (isset($this->settings['options'])) {
$this->handler->setOptions($this->settings['options']);
}
}
return $this->handler;
}
示例13: init
/**
* Initialize the Cache Engine
*
* Called automatically by the cache frontend
*
* @param array $config array of setting for the engine
* @return bool True if the engine has been successfully initialized, false if not
* @throws \InvalidArgumentException When you try use authentication without
* Memcached compiled with SASL support
*/
public function init(array $config = [])
{
if (!extension_loaded('memcached')) {
return false;
}
$this->_serializers = ['igbinary' => Memcached::SERIALIZER_IGBINARY, 'json' => Memcached::SERIALIZER_JSON, 'php' => Memcached::SERIALIZER_PHP];
if (defined('Memcached::HAVE_MSGPACK') && Memcached::HAVE_MSGPACK) {
$this->_serializers['msgpack'] = Memcached::SERIALIZER_MSGPACK;
}
parent::init($config);
if (!empty($config['host'])) {
if (empty($config['port'])) {
$config['servers'] = [$config['host']];
} else {
$config['servers'] = [sprintf('%s:%d', $config['host'], $config['port'])];
}
}
if (isset($config['servers'])) {
$this->config('servers', $config['servers'], false);
}
if (!is_array($this->_config['servers'])) {
$this->_config['servers'] = [$this->_config['servers']];
}
if (isset($this->_Memcached)) {
return true;
}
if ($this->_config['persistent']) {
$this->_Memcached = new Memcached((string) $this->_config['persistent']);
} else {
$this->_Memcached = new Memcached();
}
$this->_setOptions();
if (count($this->_Memcached->getServerList())) {
return true;
}
$servers = [];
foreach ($this->_config['servers'] as $server) {
$servers[] = $this->_parseServerString($server);
}
if (!$this->_Memcached->addServers($servers)) {
return false;
}
if (is_array($this->_config['options'])) {
foreach ($this->_config['options'] as $opt => $value) {
$this->_Memcached->setOption($opt, $value);
}
}
if (empty($this->_config['username']) && !empty($this->_config['login'])) {
throw new InvalidArgumentException('Please pass "username" instead of "login" for connecting to Memcached');
}
if ($this->_config['username'] !== null && $this->_config['password'] !== null) {
$sasl = method_exists($this->_Memcached, 'setSaslAuthData') && ini_get('memcached.use_sasl');
if (!$sasl) {
throw new InvalidArgumentException('Memcached extension is not build with SASL support');
}
$this->_Memcached->setOption(Memcached::OPT_BINARY_PROTOCOL, true);
$this->_Memcached->setSaslAuthData($this->_config['username'], $this->_config['password']);
}
return true;
}
示例14: __construct
private function __construct()
{
if (defined('MEMCACHE_SERVERS')) {
try {
// create a new persistent client
$m = new Memcached("memcached_pool");
$m->setOption(Memcached::OPT_BINARY_PROTOCOL, TRUE);
// some nicer default options
$m->setOption(Memcached::OPT_NO_BLOCK, TRUE);
$m->setOption(Memcached::OPT_AUTO_EJECT_HOSTS, TRUE);
$m->setOption(Memcached::OPT_CONNECT_TIMEOUT, 2000);
$m->setOption(Memcached::OPT_POLL_TIMEOUT, 2000);
$m->setOption(Memcached::OPT_RETRY_TIMEOUT, 2);
// setup authentication
if (defined('MEMCACHE_USERNAME') && defined('MEMCACHE_PASSWORD')) {
$m->setSaslAuthData(MEMCACHE_USERNAME, MEMCACHE_PASSWORD);
}
// We use a consistent connection to memcached, so only add in the
// servers first time through otherwise we end up duplicating our
// connections to the server.
if (!$m->getServerList()) {
// parse server config
$servers = explode(",", MEMCACHE_SERVERS);
foreach ($servers as $s) {
$parts = explode(":", $s);
$m->addServer($parts[0], $parts[1]);
}
}
} catch (Exception $e) {
$this->objCache = false;
}
} else {
$this->objCache = false;
}
}
示例15: serversDiffer
/**
* Servers differ? i.e., current vs. active.
*
* @since 151216 Memcached utilities.
*
* @return bool True if servers differ.
*/
protected function serversDiffer() : bool
{
if (!$this->Pool) {
return false;
// Not possible.
}
$active_servers = [];
// Initialize array.
foreach ($this->Pool->getServerList() as $_server) {
$active_servers[$_server['host'] . ':' . $_server['port']] = $_server;
}
// unset($_server); // Housekeeping.
if (count($this->servers) !== count($active_servers)) {
return true;
// They definitely differ.
}
foreach ($this->servers as $_key => $_server) {
if (!isset($active_servers[$_key])) {
return true;
}
// unset($_key, $_server);
}
foreach ($active_servers as $_key => $_server) {
if (!isset($this->servers[$_key])) {
return true;
}
// unset($_key, $_server);
}
return false;
}