PHP Doku:: Add a memcached server to connection pool - memcache.addserver.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzSonstige DiensteMemcacheThe Memcache classMemcache::addServer

Ein Service von Reinhard Neidl - Webprogrammierung.

The Memcache class

<<Memcache::add

Memcache::close>>

Memcache::addServer

(PECL memcache >= 2.0.0)

Memcache::addServerAdd a memcached server to connection pool

Beschreibung

bool Memcache::addServer ( string $host [, int $port = 11211 [, 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.

Hinweis:

This function has been added to Memcache version 2.0.0.

Parameter-Liste

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.

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

Rückgabewerte

Gibt bei Erfolg TRUE zurück. Im Fehlerfall wird FALSE zurückgegeben.

Beispiele

Beispiel #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);

?>

Siehe auch


4 BenutzerBeiträge:
- Beiträge aktualisieren...
anthony at iclasspro dot com
2.12.2010 0:40
An important thing to note about this is that if you have 2 memcache servers and one of those servers is your localhost that you will want to specify the external IP addresses of both of those.

<?php
$m
= memcache_connect('127.0.0.1', 11211);
memcache_add_server($m, '10.5.50.20');
?>
This will NEVER talk to 10.5.50.20.

So what you need to do is to specify the external IP address of all your servers:
<?php
$m
= memcache_connect('10.5.50.10', 11211);
memcache_add_server($m, '10.5.50.20');
?>

For some reason when the PHP memcache module does a connection to localhost it assumes that this is the ONLY memcache server and does not attempt to use others.
iwind dot liu at gmail dot com
8.02.2010 9:14
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 message "[notice] child pid 18725 exit signal Segmentation fault (11)" in error_log file.
rich at drivecurrent dot com
23.08.2008 0: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.
Jean-Baptiste Quenot
5.05.2008 10:08
The default value for the "weight" argument is 1



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)