<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Add a memcached server to connection pool</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="memcache.add.html">Memcache::add</a></div> <div class="next" style="text-align: right; float: right;"><a href="memcache.close.html">Memcache::close</a></div> <div class="up"><a href="class.memcache.html">Memcache</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="memcache.addserver" class="refentry"> <div class="refnamediv"> <h1 class="refname">Memcache::addServer</h1> <p class="verinfo">(PECL memcache >= 2.0.0)</p><p class="refpurpose"><span class="refname">Memcache::addServer</span> — <span class="dc-title">Add a memcached server to connection pool</span></p> </div> <div class="refsect1 description" id="refsect1-memcache.addserver-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">bool</span> <span class="methodname"><strong>Memcache::addServer</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$host</code></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$port</code><span class="initializer"> = 11211</span></span> [, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$persistent</code></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$weight</code></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$timeout</code></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$retry_interval</code></span> [, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$status</code></span> [, <span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$failure_callback</code></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$timeoutms</code></span> ]]]]]]]] )</div> <p class="para rdfs-comment"> <span class="function"><strong>Memcache::addServer()</strong></span> adds a server to the connection pool. You can also use the <span class="function"><strong>memcache_add_server()</strong></span> function. </p> <p class="para"> When using this method (as opposed to <span class="function"><a href="memcache.connect.html" class="function">Memcache::connect()</a></span> and <span class="function"><a href="memcache.pconnect.html" class="function">Memcache::pconnect()</a></span>) 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. </p> <p class="para"> 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. </p> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> This function has been added to Memcache version 2.0.0. </p> </p></blockquote> </div> <div class="refsect1 parameters" id="refsect1-memcache.addserver-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">host</code></em></span> <dd> <p class="para"> Point to the host where memcached is listening for connections. This parameter may also specify other transports like <em>unix:///path/to/memcached.sock</em> to use UNIX domain sockets, in this case <em><code class="parameter">port</code></em> must also be set to <em>0</em>. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">port</code></em></span> <dd> <p class="para"> Point to the port where memcached is listening for connections. Set this parameter to <em>0</em> when using UNIX domain sockets. </p> <p class="para"> Please note: <em><code class="parameter">port</code></em> defaults to <a href="memcache.ini.html#ini.memcache.default-port" class="link">memcache.default_port</a> if not specified. For this reason it is wise to specify the port explicitly in this method call. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">persistent</code></em></span> <dd> <p class="para"> Controls the use of a persistent connection. Default to <strong><code>TRUE</code></strong>. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">weight</code></em></span> <dd> <p class="para"> 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. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">timeout</code></em></span> <dd> <p class="para"> 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. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">retry_interval</code></em></span> <dd> <p class="para"> 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 <em><code class="parameter">persistent</code></em> parameter has any effect when the extension is loaded dynamically via <span class="function"><a href="function.dl.html" class="function">dl()</a></span>. </p> <p class="para"> 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 <em><code class="parameter">retry_interval</code></em> seconds. The typical effect is that each web server child will retry the connection about every <em><code class="parameter">retry_interval</code></em> seconds when serving a page. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">status</code></em></span> <dd> <p class="para"> Controls if the server should be flagged as online. Setting this parameter to <strong><code>FALSE</code></strong> and <em><code class="parameter">retry_interval</code></em> 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 <em><code class="parameter">memcache.allow_failover</code></em> setting. Default to <strong><code>TRUE</code></strong>, meaning the server should be considered online. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">failure_callback</code></em></span> <dd> <p class="para"> 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. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">timeoutms</code></em></span> <dd> <p class="para"> </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 notes" id="refsect1-memcache.addserver-notes"> <h3 class="title">Notes</h3> <div class="warning"><strong class="warning">Warning</strong> <p class="para"> When the <em><code class="parameter">port</code></em> is unspecified, this method defaults to the value set of the PHP ini directive <a href="memcache.ini.html#ini.memcache.default-port" class="link">memcache.default_port</a> 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. </p> </div> </div> <div class="refsect1 returnvalues" id="refsect1-memcache.addserver-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns <strong><code>TRUE</code></strong> on success or <strong><code>FALSE</code></strong> on failure. </p> </div> <div class="refsect1 examples" id="refsect1-memcache.addserver-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-4409"> <p><strong>Example #1 <span class="function"><strong>Memcache::addServer()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br /></span><span style="color: #FF8000">/* OO API */<br /><br /></span><span style="color: #0000BB">$memcache </span><span style="color: #007700">= new </span><span style="color: #0000BB">Memcache</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$memcache</span><span style="color: #007700">-></span><span style="color: #0000BB">addServer</span><span style="color: #007700">(</span><span style="color: #DD0000">'memcache_host'</span><span style="color: #007700">, </span><span style="color: #0000BB">11211</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$memcache</span><span style="color: #007700">-></span><span style="color: #0000BB">addServer</span><span style="color: #007700">(</span><span style="color: #DD0000">'memcache_host2'</span><span style="color: #007700">, </span><span style="color: #0000BB">11211</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/* procedural API */<br /><br /></span><span style="color: #0000BB">$memcache_obj </span><span style="color: #007700">= </span><span style="color: #0000BB">memcache_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">'memcache_host'</span><span style="color: #007700">, </span><span style="color: #0000BB">11211</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">memcache_add_server</span><span style="color: #007700">(</span><span style="color: #0000BB">$memcache_obj</span><span style="color: #007700">, </span><span style="color: #DD0000">'memcache_host2'</span><span style="color: #007700">, </span><span style="color: #0000BB">11211</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 seealso" id="refsect1-memcache.addserver-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="memcache.connect.html" class="function" rel="rdfs-seeAlso">Memcache::connect()</a> - Open memcached server connection</span></li> <li class="member"> <span class="function"><a href="memcache.pconnect.html" class="function" rel="rdfs-seeAlso">Memcache::pconnect()</a> - Open memcached server persistent connection</span></li> <li class="member"> <span class="function"><a href="memcache.close.html" class="function" rel="rdfs-seeAlso">Memcache::close()</a> - Close memcached server connection</span></li> <li class="member"> <span class="function"><a href="memcache.setserverparams.html" class="function" rel="rdfs-seeAlso">Memcache::setServerParams()</a> - Changes server parameters and status at runtime</span></li> <li class="member"> <span class="function"><a href="memcache.getserverstatus.html" class="function" rel="rdfs-seeAlso">Memcache::getServerStatus()</a> - Returns server status</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="memcache.add.html">Memcache::add</a></div> <div class="next" style="text-align: right; float: right;"><a href="memcache.close.html">Memcache::close</a></div> <div class="up"><a href="class.memcache.html">Memcache</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>