Source of: /manual/en/function.memcache-addserver.php
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.memcache.inc";
$setup = array (
'home' =>
array (
0 => 'index.php',
1 => 'PHP Manual',
),
'head' =>
array (
0 => 'UTF-8',
1 => 'en',
),
'this' =>
array (
0 => 'function.memcache-addserver.php',
1 => 'Memcache::addServer',
),
'up' =>
array (
0 => 'ref.memcache.php',
1 => 'Memcache Functions',
),
'prev' =>
array (
0 => 'function.memcache-add.php',
1 => 'Memcache::add',
),
'next' =>
array (
0 => 'function.memcache-close.php',
1 => 'Memcache::close',
),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);
manual_header();
?>
<div id="function.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>
<a name="function.memcache-addserver.description"></a><div class="refsect1 description">
<h3 class="title">Description</h3>
<div class="methodsynopsis dc-description">
<span class="type">bool</span> <span class="methodname"><b>Memcache::addServer</b></span>
( <span class="methodparam"><span class="type">string</span> <tt class="parameter">$host</tt></span>
[, <span class="methodparam"><span class="type">int</span> <tt class="parameter">$port</tt></span>
[, <span class="methodparam"><span class="type">bool</span> <tt class="parameter">$persistent</tt></span>
[, <span class="methodparam"><span class="type">int</span> <tt class="parameter">$weight</tt></span>
[, <span class="methodparam"><span class="type">int</span> <tt class="parameter">$timeout</tt></span>
[, <span class="methodparam"><span class="type">int</span> <tt class="parameter">$retry_interval</tt></span>
[, <span class="methodparam"><span class="type">bool</span> <tt class="parameter">$status</tt></span>
[, <span class="methodparam"><span class="type"><a href="language.pseudo-types.php#language.types.callback" class="type callback">callback</a></span> <tt class="parameter">$failure_callback</tt></span>
[, <span class="methodparam"><span class="type">int</span> <tt class="parameter">$timeoutms</tt></span>
]]]]]]]] )</div>
<p class="para rdfs-comment">
<b>Memcache::addServer()</b> adds a server to the connection
pool. The connection, which was opened using <b>Memcache::addServer()</b>
will be automatically closed at the end of script execution, you can also close it
manually with <a href="function.memcache-close.php" class="function">Memcache::close()</a>.
You can also use the <b>memcache_add_server()</b> function.
</p>
<p class="para">
When using this method (as opposed to <a href="function.memcache-connect.php" class="function">Memcache::connect()</a> and
<a href="function.memcache-pconnect.php" class="function">Memcache::pconnect()</a>) 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><p><b class="note">Note</b>:
This function has been added to Memcache version 2.0.0.
<br />
</p></blockquote>
</div>
<a name="function.memcache-addserver.parameters"></a><div class="refsect1 parameters">
<h3 class="title">Parameters</h3>
<p class="para">
</p><dl>
<dt class="varlistentry">
<span class="term"><i><tt class="parameter">host</tt></i>
</span>
</dt><dd class="listitem">
<p class="para">
Point to the host where memcached is listening for connections. This parameter
may also specify other transports like <i>unix:///path/to/memcached.sock</i>
to use UNIX domain sockets, in this case <i><tt class="parameter">port</tt></i>
must also
be set to <i>0</i>.
</p>
</dd>
<dt class="varlistentry">
<span class="term"><i><tt class="parameter">port</tt></i>
</span>
</dt><dd class="listitem">
<p class="para">
Point to the port where memcached is listening for connections.
This parameter is optional and its default value is 11211. Set this
parameter to <i>0</i> when using UNIX domain sockets.
</p>
</dd>
<dt class="varlistentry">
<span class="term"><i><tt class="parameter">persistent</tt></i>
</span>
</dt><dd class="listitem">
<p class="para">
Controls the use of a persistent connection. Default to <b><tt class="constant">TRUE</tt></b>.
</p>
</dd>
<dt class="varlistentry">
<span class="term"><i><tt class="parameter">weight</tt></i>
</span>
</dt><dd class="listitem">
<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 class="varlistentry">
<span class="term"><i><tt class="parameter">timeout</tt></i>
</span>
</dt><dd class="listitem">
<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 class="varlistentry">
<span class="term"><i><tt class="parameter">retry_interval</tt></i>
</span>
</dt><dd class="listitem">
<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 <i><tt class="parameter">persistent</tt></i>
parameter has any
effect when the extension is loaded dynamically via <a href="function.dl.php" class="function">dl()</a>.
</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 <i><tt class="parameter">retry_interval</tt></i>
seconds. The typical
effect is that each web server child will retry the connection about every
<i><tt class="parameter">retry_interval</tt></i>
seconds when serving a page.
</p>
</dd>
<dt class="varlistentry">
<span class="term"><i><tt class="parameter">status</tt></i>
</span>
</dt><dd class="listitem">
<p class="para">
Controls if the server should be flagged as online. Setting this parameter
to <b><tt class="constant">FALSE</tt></b> and <i><tt class="parameter">retry_interval</tt></i>
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 <i><tt class="parameter">memcache.allow_failover</tt></i>
setting.
Default to <b><tt class="constant">TRUE</tt></b>, meaning the server should be considered online.
</p>
</dd>
<dt class="varlistentry">
<span class="term"><i><tt class="parameter">failure_callback</tt></i>
</span>
</dt><dd class="listitem">
<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 class="varlistentry">
<span class="term"><i><tt class="parameter">timeoutms</tt></i>
</span>
</dt><dd class="listitem">
<p class="para">
</p>
</dd>
</dl>
<p>
</p>
</div>
<a name="function.memcache-addserver.returnvalues"></a><div class="refsect1 returnvalues">
<h3 class="title">Return Values</h3>
<p class="para">
Returns <b><tt class="constant">TRUE</tt></b> on success or <b><tt class="constant">FALSE</tt></b> on failure.
</p>
</div>
<a name="function.memcache-addserver.examples"></a><div class="refsect1 examples">
<h3 class="title">Examples</h3>
<p class="para">
</p><div class="example">
<p><b>Example #1 <b>Memcache::addServer()</b> example</b></p>
<div class="example-contents programlisting">
<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>
</p>
</div>
<a name="function.memcache-addserver.seealso"></a><div class="refsect1 seealso">
<h3 class="title">See Also</h3>
<p class="para">
</p><ul class="simplelist">
<li class="member"><a href="function.memcache-connect.php" class="function" rel="rdfs-seeAlso">Memcache::connect()</a> - Open memcached server connection</li>
<li class="member"><a href="function.memcache-pconnect.php" class="function" rel="rdfs-seeAlso">Memcache::pconnect()</a> - Open memcached server persistent connection</li>
<li class="member"><a href="function.memcache-close.php" class="function" rel="rdfs-seeAlso">Memcache::close()</a> - Close memcached server connection</li>
<li class="member"><a href="function.memcache-setserverparams.php" class="function" rel="rdfs-seeAlso">Memcache::setServerParams()</a> - Changes server parameters and status at runtime</li>
<li class="member"><a href="function.memcache-getserverstatus.php" class="function" rel="rdfs-seeAlso">Memcache::getServerStatus()</a> - Returns server status</li>
</ul><p>
</p>
</div>
</div><?php manual_footer(); ?>