Memcache::addServer
(PECL memcache >= 2.0.0)
Memcache::addServer — Add a memcached server to connection pool
Description
$host
[, int $port
= 11211
[, bool $persistent
[, int $weight
[, int $timeout
[, int $retry_interval
[, bool $status
[, callable $failure_callback
[, int $timeoutms
]]]]]]]] ) : boolMemcache::addServer() adds a server to the connection pool. 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. Set this parameter to 0 when using UNIX domain sockets.
Please note:
port
defaults to memcache.default_port if not specified. For this reason it is wise to specify the port explicitly in this method call. -
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 everyretry_interval
seconds when serving a page. -
status
-
Controls if the server should be flagged as online. Setting this parameter to
FALSE
andretry_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 thememcache.allow_failover
setting. Default toTRUE
, 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
-
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.
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::connect() - Open memcached server connection
- Memcache::pconnect() - Open memcached server persistent connection
- Memcache::close() - Close memcached server connection
- Memcache::setServerParams() - Changes server parameters and status at runtime
- Memcache::getServerStatus() - Returns server status
English translation
You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.
Thank you in advance.
Document created the 30/01/2003, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/php-rf-memcache.addserver.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
References
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.