本文整理汇总了PHP中Memcached::addServers方法的典型用法代码示例。如果您正苦于以下问题:PHP Memcached::addServers方法的具体用法?PHP Memcached::addServers怎么用?PHP Memcached::addServers使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Memcached
的用法示例。
在下文中一共展示了Memcached::addServers方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: __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');
}
示例2: __construct
/**
* Constructor
*
* @param array[] $servers server array
*/
public function __construct($servers)
{
if (!$servers || !is_array($servers) || count($servers) < 1) {
throw new GitPHP_MessageException('No Memcache servers defined', true, 500);
}
if (class_exists('Memcached')) {
$this->memcacheObj = new Memcached();
$this->memcacheType = GitPHP_CacheResource_Memcache::Memcached;
$this->memcacheObj->addServers($servers);
} else {
if (class_exists('Memcache')) {
$this->memcacheObj = new Memcache();
$this->memcacheType = GitPHP_CacheResource_Memcache::Memcache;
foreach ($servers as $server) {
if (is_array($server)) {
$host = $server[0];
$port = 11211;
if (isset($server[1])) {
$port = $server[1];
}
$weight = 1;
if (isset($server[2])) {
$weight = $server[2];
}
$this->memcacheObj->addServer($host, $port, true, $weight);
}
}
} else {
throw new GitPHP_MissingMemcacheException();
}
}
$this->servers = $servers;
}
示例3: __construct
public function __construct($prefix = '')
{
parent::__construct($prefix);
if (is_null(self::$cache)) {
self::$cache = new \Memcached();
$servers = \OC::$server->getSystemConfig()->getValue('memcached_servers');
if (!$servers) {
$server = \OC::$server->getSystemConfig()->getValue('memcached_server');
if ($server) {
$servers = array($server);
} else {
$servers = array(array('localhost', 11211));
}
}
self::$cache->addServers($servers);
$defaultOptions = [\Memcached::OPT_CONNECT_TIMEOUT => 50, \Memcached::OPT_RETRY_TIMEOUT => 50, \Memcached::OPT_SEND_TIMEOUT => 50, \Memcached::OPT_RECV_TIMEOUT => 50, \Memcached::OPT_POLL_TIMEOUT => 50, \Memcached::OPT_COMPRESSION => true, \Memcached::OPT_LIBKETAMA_COMPATIBLE => true, \Memcached::OPT_BINARY_PROTOCOL => true];
// by default enable igbinary serializer if available
if (\Memcached::HAVE_IGBINARY) {
$defaultOptions[\Memcached::OPT_SERIALIZER] = \Memcached::SERIALIZER_IGBINARY;
}
$options = \OC::$server->getConfig()->getSystemValue('memcached_options', []);
if (is_array($options)) {
$options = $options + $defaultOptions;
self::$cache->setOptions($options);
} else {
throw new HintException("Expected 'memcached_options' config to be an array, got {$options}");
}
}
}
示例4: __construct
/**
* Constructor
*
* @access public
*
* @param array $config config array
*
* @result void
* @throws Exception
*/
public function __construct(array $config = [])
{
parent::__construct($config);
if (empty($config['type']) || !$this->check()) {
throw new Exception('Memcache(d) not installed or not select type');
}
switch (strtolower($config['type'])) {
case 'memcached':
$this->driver = new \Memcached();
break;
case 'memcache':
$this->driver = new \Memcache();
break;
default:
throw new Exception('Selected type not valid in the driver');
}
if (!empty($config['servers'])) {
$this->driver->addServers($config['servers']);
} elseif ($config['server']) {
$conf = $config['server'];
$server = ['hostname' => !empty($conf['hostname']) ? $conf['hostname'] : '127.0.0.1', 'port' => !empty($conf['port']) ? $conf['port'] : 11211, 'weight' => !empty($conf['weight']) ? $conf['weight'] : 1];
if (get_class($this->driver) === 'Memcached') {
$this->driver->addServer($server['hostname'], $server['port'], $server['weight']);
} else {
$this->driver->addServer($server['hostname'], $server['port'], true, $server['weight']);
}
} else {
throw new Exception('Server(s) not configured');
}
}
示例5: getMemcached
/**
* @return \Memcached
*/
public function getMemcached()
{
if (!$this->Memcached) {
$this->Memcached = new \Memcached();
$this->Memcached->addServers($this->options['servers']);
}
return $this->Memcached;
}
示例6: addServers
/**
* {@inheritDoc}
*/
public function addServers(array $servers)
{
$configs = [];
foreach ($servers as $server) {
$configs[] = [$server['host'], $server['port']];
}
$this->_connection->addServers($configs);
}
示例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: __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());
}
}
示例9: __construct
/**
* Constructor
* @param array $options cache store storage option
*/
public function __construct($options = array())
{
$this->connect = new \Memcached();
$this->prefix = $options['prefix'];
$this->default_ttl = $options['default_ttl'];
$this->connect->setOption(\Memcached::OPT_PREFIX_KEY, $this->prefix);
$this->connect->addServers($options['servers']);
}
示例10: __construct
/**
* Creates a new instance of memcached.
*/
public function __construct() {
if (!class_exists('Memcached')) {
throw new SystemException('memcached support is not enabled.');
}
// init memcached
$this->memcached = new \Memcached();
// add servers
$tmp = explode("\n", StringUtil::unifyNewlines(CACHE_SOURCE_MEMCACHED_HOST));
$servers = array();
$defaultWeight = floor(100 / count($tmp));
$regex = new Regex('^\[([a-z0-9\:\.]+)\](?::([0-9]{1,5}))?(?::([0-9]{1,3}))?$', Regex::CASE_INSENSITIVE);
foreach ($tmp as $server) {
$server = StringUtil::trim($server);
if (!empty($server)) {
$host = $server;
$port = 11211; // default memcached port
$weight = $defaultWeight;
// check for IPv6
if ($regex->match($host)) {
$matches = $regex->getMatches();
$host = $matches[1];
if (isset($matches[2])) {
$port = $matches[2];
}
if (isset($matches[3])) {
$weight = $matches[3];
}
}
else {
// IPv4, try to get port and weight
if (strpos($host, ':')) {
$parsedHost = explode(':', $host);
$host = $parsedHost[0];
$port = $parsedHost[1];
if (isset($parsedHost[2])) {
$weight = $parsedHost[2];
}
}
}
$servers[] = array($host, $port, $weight);
}
}
$this->memcached->addServers($servers);
// test connection
$this->memcached->get('testing');
// set variable prefix to prevent collision
$this->prefix = substr(sha1(WCF_DIR), 0, 8) . '_';
}
示例11: __construct
/**
* @param $persistentId
* @param array $connections
*/
public function __construct($persistentId, array $connections)
{
$this->isMemcachedExtensionAvailable();
$this->memcached = new MemcachedDriver($persistentId);
$this->memcached->addServers($connections);
$this->memcached->setOption(MemcachedDriver::OPT_SERIALIZER, \defined(MemcachedDriver::HAVE_IGBINARY) && MemcachedDriver::HAVE_IGBINARY ? MemcachedDriver::SERIALIZER_IGBINARY : MemcachedDriver::SERIALIZER_PHP);
$this->memcached->setOption(MemcachedDriver::OPT_DISTRIBUTION, MemcachedDriver::DISTRIBUTION_CONSISTENT);
$this->memcached->setOption(MemcachedDriver::OPT_LIBKETAMA_COMPATIBLE, true);
$this->memcached->setOption(MemcachedDriver::OPT_BINARY_PROTOCOL, true);
}
示例12: __construct
/**
* Constructor
*
* @param array[] $servers server array
*/
public function __construct($servers)
{
if (!class_exists('Memcached')) {
throw new Exception('Memcached extension not found');
}
if (!$servers || !is_array($servers) || count($servers) < 1) {
throw new GitPHP_MessageException('No Memcache servers defined', true, 500);
}
$this->memcache = new Memcached();
$this->memcache->addServers($servers);
}
示例13: configureServers
/**
* Configure servers
*
* @return void
*
* @throws \InvalidArgumentException
*/
public function configureServers()
{
if (!array_key_exists('server', $this->config)) {
throw new \InvalidArgumentException('Configuration key "server" must be set');
}
$servers = $this->config['server'];
// Single server
if (array_key_exists('host', $servers)) {
$servers = array($servers);
}
$this->connection->addServers($servers);
}
示例14: 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;
}
示例15: connect
/**
* Connect to the cache server(s).
*
* @throws CacheException
* @access private
*/
private function connect()
{
$this->connected = false;
switch (NN_CACHE_TYPE) {
case self::TYPE_REDIS:
if ($this->socketFile === false) {
$servers = unserialize(NN_CACHE_HOSTS);
foreach ($servers as $server) {
if ($this->server->connect($server['host'], $server['port'], (double) NN_CACHE_TIMEOUT) === false) {
throw new CacheException('Error connecting to the Redis server!');
} else {
$this->connected = true;
}
}
} else {
if ($this->server->connect(NN_CACHE_SOCKET_FILE) === false) {
throw new CacheException('Error connecting to the Redis server!');
} else {
$this->connected = true;
}
}
break;
case self::TYPE_MEMCACHED:
$params = $this->socketFile === false ? unserialize(NN_CACHE_HOSTS) : [[NN_CACHE_SOCKET_FILE, 'port' => 0]];
if ($this->server->addServers($params) === false) {
throw new CacheException('Error connecting to the Memcached server!');
} else {
$this->connected = true;
}
break;
case self::TYPE_APC:
$this->connected = true;
break;
}
}