<!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>Associate a MySQL connection with a Memcache connection</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.mysqlnd-memcache-get-config.html">mysqlnd_memcache_get_config</a></div> <div class="next" style="text-align: right; float: right;"><a href="mysqlnd-memcache.changes.html">Change History</a></div> <div class="up"><a href="ref.mysqlnd-memcache.html">Mysqlnd_memcache Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.mysqlnd-memcache-set" class="refentry"> <div class="refnamediv"> <h1 class="refname">mysqlnd_memcache_set</h1> <p class="verinfo">(PECL mysqlnd_memcache >= 1.0.0)</p><p class="refpurpose"><span class="refname">mysqlnd_memcache_set</span> — <span class="dc-title">Associate a MySQL connection with a Memcache connection</span></p> </div> <div class="refsect1 description" id="refsect1-function.mysqlnd-memcache-set-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">bool</span> <span class="methodname"><strong>mysqlnd_memcache_set</strong></span> ( <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$mysql_connection</code></span> [, <span class="methodparam"><span class="type"><a href="class.memcached.html" class="type Memcached">Memcached</a></span> <code class="parameter">$memcache_connection</code></span> [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$pattern</code></span> [, <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.callback" class="type callback">callback</a></span> <code class="parameter">$callback</code></span> ]]] )</div> <p class="para rdfs-comment"> Associate <em><code class="parameter">mysql_connection</code></em> with <em><code class="parameter">memcache_connection</code></em> using <em><code class="parameter">pattern</code></em> as a PCRE regular expression, and <em><code class="parameter">callback</code></em> as a notification callback or to unset the association of <em><code class="parameter">mysql_connection</code></em>. </p> <p class="para"> While associating a MySQL connection with a Memcache connection, this function will query the MySQL Server for its configuration. It will automatically detect whether the server is configured to use the InnoDB Memcache Daemon Plugin or MySQL Cluster NDB Memcache support. It will also query the server to automatically identify exported tables and other configuration options. The results of this automatic configuration can be retrieved using <span class="function"><a href="function.mysqlnd-memcache-get-config.html" class="function">mysqlnd_memcache_get_config()</a></span>. </p> </div> <div class="refsect1 parameters" id="refsect1-function.mysqlnd-memcache-set-parameters"> <h3 class="title">Parameters</h3> <dl> <dt> <span class="term"><em><code class="parameter">mysql_connection</code></em></span> <dd> <p class="para"> A handle to a MySQL Server using one of the MySQL API extensions for PHP, which are <a href="ref.pdo-mysql.html" class="link">PDO_MYSQL</a>, <a href="book.mysqli.html" class="link">mysqli</a> or <a href="book.mysql.html" class="link">ext/mysql</a>. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">memcache_connection</code></em></span> <dd> <p class="para"> A <a href="book.memcached.html" class="link">Memcached</a> instance with a connection to the MySQL Memcache Daemon plugin. If this parameter is omitted, then <em><code class="parameter">mysql_connection</code></em> will be unassociated from any memcache connection. And if a previous association exists, then it will be replaced. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">pattern</code></em></span> <dd> <p class="para"> A regular expression in <a href="book.pcre.html" class="link">Perl Compatible Regular Expression</a> syntax used to identify potential Memcache-queries. The query should have three sub patterns. The first subpattern contains the requested field list, the second the name of the ID column from the query and the third the requested value. If this parameter is omitted or os set to <strong><code>NULL</code></strong>, then a default pattern will be used. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">callback</code></em></span> <dd> <p class="para"> A callback which will be used whenever a query is being sent to MySQL. The callback will receive a single <span class="type"><a href="language.types.boolean.html" class="type boolean">boolean</a></span> parameter telling if a query was sent via Memcache. </p> </dd> </dt> </dl> </div> <div class="refsect1 returnvalues" id="refsect1-function.mysqlnd-memcache-set-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> <strong><code>TRUE</code></strong> if the association or disassociation is successful, otherwise <strong><code>FALSE</code></strong> if there is an error. </p> </div> <div class="refsect1 examples" id="refsect1-function.mysqlnd-memcache-set-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-1935"> <p><strong>Example #1 <span class="function"><strong>mysqlnd_memcache_set()</strong></span> example with <span class="function"><a href="function.var-dump.html" class="function">var_dump()</a></span> as a simple debugging callback.</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$mysqli </span><span style="color: #007700">= new </span><span style="color: #0000BB">mysqli</span><span style="color: #007700">(</span><span style="color: #DD0000">"host"</span><span style="color: #007700">, </span><span style="color: #DD0000">"user"</span><span style="color: #007700">, </span><span style="color: #DD0000">"passwd"</span><span style="color: #007700">, </span><span style="color: #DD0000">"database"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$memc </span><span style="color: #007700">= new </span><span style="color: #0000BB">Memcached</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$memc</span><span style="color: #007700">-></span><span style="color: #0000BB">addServer</span><span style="color: #007700">(</span><span style="color: #DD0000">"host"</span><span style="color: #007700">, </span><span style="color: #0000BB">11211</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">mysqlnd_memcache_set</span><span style="color: #007700">(</span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">, </span><span style="color: #0000BB">$memc</span><span style="color: #007700">, </span><span style="color: #0000BB">NULL</span><span style="color: #007700">, </span><span style="color: #DD0000">'var_dump'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/* This query will be intercepted and executed via Memcache protocol */<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"Sending query for id via Memcache: "</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">-></span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #DD0000">"SELECT f1, f2, f3 FROM test WHERE id = 1"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/* f1 is not configured as valid key field, this won't be sent via Memcache */<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"Sending query for f1 via Memcache: "</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">-></span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #DD0000">"SELECT id FROM test WHERE f1 = 1"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">mysqlnd_memcache_set</span><span style="color: #007700">(</span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/* Now the regular MySQL protocol will be used */<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"var_dump won't be invoked: "</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">-></span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #DD0000">"SELECT f1, f2, f3 WHERE id = 1"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> Sending query for id via Memcache: bool(true) Sending query for f1 via Memcache: bool(false) var_dump won't be invoked: </pre></div> </div> </div> </p> </div> <div class="refsect1 seealso" id="refsect1-function.mysqlnd-memcache-set-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.mysqlnd-memcache-get-config.html" class="function" rel="rdfs-seeAlso">mysqlnd_memcache_get_config()</a> - Returns information about the plugin configuration</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="function.mysqlnd-memcache-get-config.html">mysqlnd_memcache_get_config</a></div> <div class="next" style="text-align: right; float: right;"><a href="mysqlnd-memcache.changes.html">Change History</a></div> <div class="up"><a href="ref.mysqlnd-memcache.html">Mysqlnd_memcache Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>