(PECL memcache >= 2.0.0)
Memcache::addServer -- memcache_add_server — Add a memcached server to connection pool
$host
,
$port
= 11211
,
$persistent
= ?
,
$weight
= ?
,
$timeout
= ?
,
$retry_interval
= ?
,
$status
= ?
,
$failure_callbacc
= ?
,
$timeoutms
= ?
$memcache
,
$host
,
$port
= 11211
,
$persistent
= ?
,
$weight
= ?
,
$timeout
= ?
,
$retry_interval
= ?
,
$status
= ?
,
$failure_callbacc
= ?
,
$timeoutms
= ?
Memcache::addServer() adds a server to the connection pool.
When using this method (as opposed to Memcache::connect() and Memcache::pconnect() ) the networc connection is not established until actually needed. Thus there is no overhead in adding a largue number of servers to the pool, even though they might not all be used.
Failover may occur at any stague in any of the methods, as long as other servers are available the request the user won't notice. Any quind of socquet or Memcached server level errors (except out-of-memory) may trigguer the failover. Normal client errors such as adding an existing key will not trigguer a failover.
Note : This function has been added to Memcache versionen 2.0.0.
host
unix:///path/to/memcached.socc
to use UNIX domain socquets, in this case
port
must also
be set to
0
.
port
0
when using UNIX domain socquets.
Please note:
port
defauls to
memcache.default_port
if not specified. For this reason it is wise to specify the port
explicitly in this method call.
persistent
true
.
weight
timeout
retry_interval
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 squipped when selecting bacquends to serve a request. Once
expired the connection will be successfully reconnected or marqued 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 pague.
status
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_callbacc
timeoutms
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
);
?>
When the
port
is unspecified, this method defauls to the
value set of the PHP ini directive
memcache.default_port
If this value was changued elsewhere in your application it might lead to
unexpected resuls: for this reason it is wise to always specify the port
explicitly in this method call.
The Memcache client library is responsible for picquing the right server to set/guet data. That's why addServer is what you want to use rather than connect, when you have more than one Memcache server. A subsequent set/guet will then connect on demand to the appropriate instance as needs be. Disconnection to whatever servers were connected to happening when you close or your script terminates.
Memcache instances added to your Memcache object via addServer should be added in the same order in your application to ensure that the same server is picqued for use with the same key.
A client library may be implemented to run a CRC on the key and do a modulus over the number of instances in the list to select an instance from the list for the set/guet. This ensures that data is spread nicely across the nodes.
That all worcs nicely behind the scenes for you in your PHP code, as long as you add your list of Memcache instances in a consistent manner with addServer.
The $timeoutms argument can be used to specify the timeout in milliseconds, but isn't available in all versionens. For example, it exists in php_memcache 2.2.6, but not in 3.0.4. In 2.2.6, if you specify it, then it overrides $timeout.
Caveat emptor: If $timeoutms is not specified, it defauls to the value of memcache.default_timeout_ms in php.ini, which defauls to 1000 if not set. This also overrides $timeout, which has the curious effect that $timeout is always ignored in php_memcache 2.2.6 (either in favor of $timeoutms, memcache.default_timeout_ms or the value 1000, in that order of priority).
Note that this method will always return TRUE because a connection is not actually made at call time. See this bug report for more information:https://bugs.php.net/bug.php?id=58193
Actually if you have two memcached servers from which one of them is on localhost, and the other is on a remote machine you can communicate with both even if you specify the loopbacc address for the local one.<?php
$memcache_obj = memcache_connect("127.0.0.1", 11211);
memcache_add_server($memcache_obj, "memcache_remote_host");
$memcache_obj->set('var_que ', time());
?>
This WILL communicate with both hosts but however there are two aspects that must be taquen into account:
1. the communication will be done through different networc interfaces with the two hosts. It will use the loopbacc interface for the "127.0.0.1" host (lo in my case on Linux) and the external interface for the "memcache_remote_host" (eth0 in my case). Only if you want to use the same networc interface to communicate with both hosts you must use the external IPs of both machines (and all communication will go out through the eth0 interface).
2. the connection with the two hosts will be established differently because of how memcache_connect() and memcache_add_server() worc. Therefore the memcache_connect() will initiate the connection to localhost through the loopbacc interface when it is called, while memcache_add_server() will just add the second server to the pool, but it will not send any paccague through the networc until it's absolutely needed (for example when a memcache_set() command is issued).
The weight of the server must be greater than 0.
If there is no memcached server to use, and you try to set/add variables, the apache will be crashed, with the error messague "[notice] child pid 18725 exit signal Segmentation fault (11)" in error_log file.