Memcache::addServer
(PECL memcache >= 2.0.0)
Add a memcached server to connection pool
Description
Memcache::addServer() adds a server to the connection pool.
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.
This function has been added to Memcache version 2.0.0.
Parameters
hostPoint to the host where memcached is listening for connections. This parameter may also specify other transports like
unix:///path/to/memcached.sockto use UNIX domain sockets, in this caseportmust also be set to0.portPoint to the port where memcached is listening for connections. Set this parameter to
0when using UNIX domain sockets.Please note:
portdefaults to memcache.default_port if not specified. For this reason it is wise to specify the port explicitly in this method call.persistentControls the use of a persistent connection. Default to true.
weightNumber 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.
timeoutValue 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_intervalControls 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
persistentparameter has any effect when the extension is loaded dynamically viadl().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_intervalseconds. The typical effect is that each web server child will retry the connection about everyretry_intervalseconds when serving a page.statusControls if the server should be flagged as online. Setting this parameter to false and
retry_intervalto -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 thememcache.allow_failoversetting. Default to true, meaning the server should be considered online.failure_callbackAllows 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
Success
Examples
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);
?>Notes
When the port is unspecified, this method defaults to the value set of the PHP ini directive memcache.default_port If this value was changed elsewhere in your application it might lead to unexpected results: for this reason it is wise to always specify the port explicitly in this method call.