downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Memcache::close> <Memcache::add
Last updated: Fri, 06 Nov 2009

view this page in

Memcache::addServer

(PECL memcache >= 2.0.0)

Memcache::addServerAdd a memcached server to connection pool

Description

bool Memcache::addServer ( string $host [, int $port [, bool $persistent [, int $weight [, int $timeout [, int $retry_interval [, bool $status [, callback $failure_callback [, int $timeoutms ]]]]]]]] )

Memcache::addServer() adds a server to the connection pool. The connection, which was opened using Memcache::addServer() will be automatically closed at the end of script execution, you can also close it manually with Memcache::close(). You can also use the memcache_add_server() function.

When using this method (as opposed to Memcache::connect() and Memcache::pconnect()) the network connection is not established until actually needed. Thus there is no overhead in adding a large number of servers to the pool, even though they might not all be used.

Failover may occur at any stage in any of the methods, as long as other servers are available the request the user won't notice. Any kind of socket or Memcached server level errors (except out-of-memory) may trigger the failover. Normal client errors such as adding an existing key will not trigger a failover.

Note: This function has been added to Memcache version 2.0.0.

Parameters

host

Point to the host where memcached is listening for connections. This parameter may also specify other transports like unix:///path/to/memcached.sock to use UNIX domain sockets, in this case port must also be set to 0.

port

Point to the port where memcached is listening for connections. This parameter is optional and its default value is 11211. Set this parameter to 0 when using UNIX domain sockets.

persistent

Controls the use of a persistent connection. Default to TRUE.

weight

Number of buckets to create for this server which in turn control its probability of it being selected. The probability is relative to the total weight of all servers.

timeout

Value in seconds which will be used for connecting to the daemon. Think twice before changing the default value of 1 second - you can lose all the advantages of caching if your connection is too slow.

retry_interval

Controls how often a failed server will be retried, the default value is 15 seconds. Setting this parameter to -1 disables automatic retry. Neither this nor the persistent parameter has any effect when the extension is loaded dynamically via dl().

Each failed connection struct has its own timeout and before it has expired the struct will be skipped when selecting backends to serve a request. Once expired the connection will be successfully reconnected or marked as failed for another retry_interval seconds. The typical effect is that each web server child will retry the connection about every retry_interval seconds when serving a page.

status

Controls if the server should be flagged as online. Setting this parameter to FALSE and retry_interval to -1 allows a failed server to be kept in the pool so as not to affect the key distribution algorithm. Requests for this server will then failover or fail immediately depending on the memcache.allow_failover setting. Default to TRUE, meaning the server should be considered online.

failure_callback

Allows the user to specify a callback function to run upon encountering an error. The callback is run before failover is attempted. The function takes two parameters, the hostname and port of the failed server.

timeoutms

Return Values

Returns TRUE on success or FALSE on failure.

Examples

Example #1 Memcache::addServer() example

<?php

/* OO API */

$memcache = new Memcache;
$memcache->addServer('memcache_host'11211);
$memcache->addServer('memcache_host2'11211);

/* procedural API */

$memcache_obj memcache_connect('memcache_host'11211);
memcache_add_server($memcache_obj'memcache_host2'11211);

?>

See Also



Memcache::close> <Memcache::add
Last updated: Fri, 06 Nov 2009
 
add a note add a note User Contributed Notes
Memcache::addServer
jay dot paroline at escapemg dot com
20-Feb-2009 11:29
timeoutms does not seem to be supported even as recently as memcache 3.0.3
rich at drivecurrent dot com
22-Aug-2008 10:12
The "version" mentioned for this function is the PECL module. To see the PECL version do phpinfo() and look at Memcache section. To see memcached (server) version: Memcache::getVersion(). You can also telnet into memcached server (eg: localhost:11211) and type 'stats' and hit enter.
puneet dot mathur at hytechpro dot com
07-Aug-2008 05:07
Hello,

I see a note here that this function is available on in version 2.0.0+. When I see my memcache version (memcached -h) I see version 1.1.12 and 1.2.0 on two server networks. I feel that addServer is working OK on memcache version 1.2.0 but not on 1.1.12. I am confused with this numbering of version. Can you please help me

Thanks.
Jean-Baptiste Quenot
05-May-2008 08:08
The default value for the "weight" argument is 1

Memcache::close> <Memcache::add
Last updated: Fri, 06 Nov 2009
 
 
show source | credits | sitemap | contact | advertising | mirror sites