本文整理汇总了PHP中Memcached::addServer方法的典型用法代码示例。如果您正苦于以下问题:PHP Memcached::addServer方法的具体用法?PHP Memcached::addServer怎么用?PHP Memcached::addServer使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Memcached
的用法示例。
在下文中一共展示了Memcached::addServer方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: __construct
/**
* Initialise memcached storage cache.
*
* @param array $options optional config array, valid keys are: host, port
*/
public function __construct(array $options = array())
{
$this->options = $options + $this->options;
$this->memcached = new \Memcached();
$this->memcached->addServer($this->options['host'], $this->options['port'], 0);
$this->flags = $this->options['compressed'] ? MEMCACHED_COMPRESSED : 0;
}
示例2: __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);
}
}
}
示例3: setUpBeforeClass
public static function setUpBeforeClass()
{
static::$Memcached = new \Memcached();
// MEMCACHED_TEST_SERVER defined in phpunit.xml
$server = explode(':', MEMCACHED_TEST_SERVER);
static::$Memcached->addServer($server[0], $server[1]);
}
示例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: __construct
public function __construct(array $aConfig = [])
{
// Parent construct
parent::__construct($aConfig);
// Extend config
$aConfig = ExtendedArray::extendWithDefaultValues($aConfig, ['connection_timeout' => 10, 'server_failure_limit' => 5, 'remove_failed_servers' => true, 'retry_timeout' => 1]);
// Check Memcached is loaded
if (!extension_loaded('memcached')) {
throw new RuntimeException('Memcached extension is not loaded');
}
// Create client
$this->oClient = new Memcached();
$this->oClient->setOption(Memcached::OPT_CONNECT_TIMEOUT, $aConfig['connection_timeout']);
$this->oClient->setOption(Memcached::OPT_DISTRIBUTION, Memcached::DISTRIBUTION_CONSISTENT);
$this->oClient->setOption(Memcached::OPT_SERVER_FAILURE_LIMIT, $aConfig['server_failure_limit']);
$this->oClient->setOption(Memcached::OPT_REMOVE_FAILED_SERVERS, $aConfig['remove_failed_servers']);
$this->oClient->setOption(Memcached::OPT_RETRY_TIMEOUT, $aConfig['retry_timeout']);
// Add servers
if (array_key_exists('servers', $aConfig)) {
$aServers = array_filter(explode(',', $aConfig['servers']));
foreach ($aServers as $sAddress) {
// Parse address
list($sHost, $sPort) = array_pad(explode(':', $sAddress), 2, '');
// Add server
$this->oClient->addServer($sHost, intval($sPort));
}
}
}
示例6: __construct
public function __construct()
{
$this->mem = new Memcached(static::$servers_id);
if (count($this->mem->getServerList()) == 0) {
$this->mem->addServer("", 11211);
}
}
示例7: setupMemcache
private function setupMemcache()
{
if (is_null($this->memcache) && class_exists('Memcached', false)) {
$this->memcache = new \Memcached();
$this->memcache->addServer($this->host, $this->port);
}
}
示例8: addServer
public function addServer($host = 'localhost', $port = 11211)
{
if ($this->memcached->addServer($host, $port, 1) === FALSE) {
$error = error_get_last();
throw new Nette\InvalidStateException("Memcached::addServer(): {$error['message']}.");
}
}
示例9: __construct
/**
* Constructor
*
* @param string $uniqId to be used to separate objects in addition to their key identifiers, for instance in a
* multi-user scenario.
*
* @return \iveeCrest\MemcachedWrapper
*/
public function __construct($uniqId)
{
$this->memcached = new \Memcached();
$this->memcached->addServer(Config::getCacheHost(), Config::getCachePort());
$this->memcached->setOption(\Memcached::OPT_PREFIX_KEY, Config::getCachePrefix());
$this->uniqId = $uniqId;
}
示例10: __construct
/**
* Creamos y configuramos el driver para la cache.
* @param string $hostname Nombre del host para conectar al servidor memcached.
* @param int $port Puerto para conectar al servidor de memcached.
* @param int $weight Importancia del servidor frente al resto.
*/
public function __construct($hostname = '127.0.0.1', $port = 11211, $weight = 1)
{
// Instanciamos memcached.
$this->_memcached = new Memcached();
// Configuramos el servidor.
$this->_memcached->addServer($hostname, $port, $weight);
}
示例11: init
protected function init()
{
$this->config['host'] = $this->config['host'] ?: 'localhost';
$this->config['port'] = $this->config['port'] ?: 11211;
$this->store = new Memcached();
$this->store->addServer($this->config['host'], $this->config['port'], 100);
}
示例12: connectServer
/**
* @return bool
*/
public function connectServer()
{
if ($this->checkdriver() == false) {
return false;
}
$s = $this->config['memcache'];
if (count($s) < 1) {
$s = array(array('127.0.0.1', 11211, 100));
}
foreach ($s as $server) {
$name = isset($server[0]) ? $server[0] : '127.0.0.1';
$port = isset($server[1]) ? $server[1] : 11211;
$sharing = isset($server[2]) ? $server[2] : 0;
$checked = $name . '_' . $port;
if (!isset($this->checked[$checked])) {
try {
if ($sharing > 0) {
if (!$this->instant->addServer($name, $port, $sharing)) {
$this->fallback = true;
}
} else {
if (!$this->instant->addServer($name, $port)) {
$this->fallback = true;
}
}
$this->checked[$checked] = 1;
} catch (\Exception $e) {
$this->fallback = true;
}
}
}
}
示例13: connect
/**
* Connect and initialize this handler.
*
* @return boolean True if successful, false on failure
*/
function connect()
{
global $mybb, $error_handler;
$this->memcached = new Memcached();
if ($mybb->config['memcache']['host']) {
$mybb->config['memcache'][0] = $mybb->config['memcache'];
unset($mybb->config['memcache']['host']);
unset($mybb->config['memcache']['port']);
}
foreach ($mybb->config['memcache'] as $memcached) {
if (!$memcached['host']) {
$message = "Please configure the memcache settings in inc/config.php before attempting to use this cache handler";
$error_handler->trigger($message, MYBB_CACHEHANDLER_LOAD_ERROR);
die;
}
if (!isset($memcached['port'])) {
$memcached['port'] = "11211";
}
$this->memcached->addServer($memcached['host'], $memcached['port']);
if (!$this->memcached) {
$message = "Unable to connect to the memcached server on {$memcached['memcache_host']}:{$memcached['memcache_port']}. Are you sure it is running?";
$error_handler->trigger($message, MYBB_CACHEHANDLER_LOAD_ERROR);
die;
}
}
// Set a unique identifier for all queries in case other forums are using the same memcache server
$this->unique_id = md5(MYBB_ROOT);
return true;
}
示例14: connect
/**
* Only connect at the last possible moment. This enables you to get the cache provider, but not connect
* until you have business with the cache.
*/
private function connect()
{
if ($this->isConnected) {
return;
}
$this->memcache->addServer($this->configuration->getHost(), $this->configuration->getPort());
}
示例15: __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;
}