Memcached::addServer

(PECL memcached >= 0.1.0)

Memcached::addServerAdd a server to the server pool

Description

public bool Memcached::addServer ( string $host , int $port [, int $weight = 0 ] )

Memcached::addServer() adds the specified server to the server pool. No connection is established to the server at this time, but if you are using consistent key distribution option (via Memcached::DISTRIBUTION_CONSISTENT or Memcached::OPT_LIBKETAMA_COMPATIBLE), some of the internal data structures will have to be updated. Thus, if you need to add multiple servers, it is better to use Memcached::addServers() as the update then happens only once.

The same server may appear multiple times in the server pool, because no duplication checks are made. This is not advisable; instead, use the weight option to increase the selection weighting of this server.

Parameters

host

The hostname of the memcache server. If the hostname is invalid, data-related operations will set Memcached::RES_HOST_LOOKUP_FAILURE result code.

port

The port on which memcache is running. Usually, this is 11211.

weight

The weight of the server relative to the total weight of all the servers in the pool. This controls the probability of the server being selected for operations. This is used only with consistent distribution option and usually corresponds to the amount of memory available to memcache on that server.

Return Values

Returns TRUE on success or FALSE on failure.

Examples

Example #1 Memcached::addServer() example

<?php
$m 
= new Memcached();

/* Add 2 servers, so that the second one
   is twice as likely to be selected. */
$m->addServer('mem1.domain.com'1121133);
$m->addServer('mem2.domain.com'1121167);
?>

See Also

Коментарии

Автор:
On my Debian Squeeze system I was getting WRITE FAILURE errors. After debugging and finally tcpdump it seems that the problem was me adding the server 'localhost', which resolved to '::1' (ipv6) while the default memcached server on debian only listens to '127.0.0.1' (ipv4). DNS automatically prefers ipv6 over ipv4. 

I added the server '127.0.0.1' instead and everything worked. You could also disable ipv6 or have memcached listen on ::1
2011-08-23 01:08:44
http://php5.kiev.ua/manual/ru/memcached.addserver.html
Important to not call ->addServers() every run -- only call it if no servers exist (check getServerList() ); otherwise, since addServers() does not check for dups, it will let you add the same server again and again and again, resultings in hundreds if not thousands of connections to the MC daemon. Specially when using FastCGI.

Example:

<?php
class Cache {
        private 
$id;
        private 
$obj;

        function 
__construct($id){
               
$this->id $id;
               
$this->obj = new Memcached($id);
        }

        public function 
connect($host $port){
               
$servers $this->obj->getServerList();
                if(
is_array($servers)) {
                        foreach (
$servers as $server)
                                if(
$server['host'] == $host and $server['port'] == $port)
                                        return 
true;
                }
                return 
$this->obj->addServer($host $port);
        }

}
?>
2012-09-10 21:55:08
http://php5.kiev.ua/manual/ru/memcached.addserver.html
Автор:
As of version 2.0.0b1 you can use Unix socket.

<?php
$m 
= new Memcached();
$m->addServer('/path/to/socket',0);
?>

Not to be confused with Memcache that use 'unix:///path/to/socket'
2012-11-20 12:42:27
http://php5.kiev.ua/manual/ru/memcached.addserver.html

    Поддержать сайт на родительском проекте КГБ