<!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>Installs a callback which decides whether a statement is cached</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.mysqlnd-qc-set-cache-condition.html">mysqlnd_qc_set_cache_condition</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.mysqlnd-qc-set-storage-handler.html">mysqlnd_qc_set_storage_handler</a></div> <div class="up"><a href="ref.mysqlnd-qc.html">mysqlnd_qc Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.mysqlnd-qc-set-is-select" class="refentry"> <div class="refnamediv"> <h1 class="refname">mysqlnd_qc_set_is_select</h1> <p class="verinfo">(PECL mysqlnd_qc >= 1.0.0)</p><p class="refpurpose"><span class="refname">mysqlnd_qc_set_is_select</span> — <span class="dc-title">Installs a callback which decides whether a statement is cached</span></p> </div> <div class="refsect1 description" id="refsect1-function.mysqlnd-qc-set-is-select-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <span class="methodname"><strong>mysqlnd_qc_set_is_select</strong></span> ( <span class="methodparam"> <span class="type">string</span> <code class="parameter">$callback</code> </span> )</div> <p class="para rdfs-comment"> Installs a callback which decides whether a statement is cached. </p> <p class="para"> There are several ways of hinting PELC/mysqlnd_qc to cache a query. By default, PECL/mysqlnd_qc attempts to cache a if caching of all statements is enabled or the query string begins with a certain SQL hint. The plugin internally calls a function named <em>is_select()</em> to find out. This internal function can be replaced with a user-defined callback. Then, the user-defined callback is responsible to decide whether the plugin attempts to cache a statement. Because the internal function is replaced with the callback, the callback gains full control. The callback is free to ignore the configuration setting <em>mysqlnd_qc.cache_by_default</em> and SQL hints. </p> <p class="para"> The callback is invoked for every statement inspected by the plugin. It is given the statements string as a parameter. The callback returns <strong><code>FALSE</code></strong> if the statement shall not be cached. It returns <strong><code>TRUE</code></strong> to make the plugin attempt to cache the statements result set, if any. A so-created cache entry is given the default TTL set with the PHP configuration directive <em>mysqlnd_qc.ttl</em>. If a different TTL shall be used, the callback returns a numeric value to be used as the TTL. </p> <p class="para"> The internal <em>is_select</em> function is part of the internal cache storage handler interface. Thus, a user-defined storage handler offers the same capabilities. </p> </div> <div class="refsect1 parameters" id="refsect1-function.mysqlnd-qc-set-is-select-parameters"> <h3 class="title">Parameters</h3> <p class="para">This function has no parameters.</p> </div> <div class="refsect1 returnvalues" id="refsect1-function.mysqlnd-qc-set-is-select-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-function.mysqlnd-qc-set-is-select-examples"> <h3 class="title">Examples</h3> <div class="example" id="example-1868"> <p><strong>Example #1 <span class="function"><strong>mysqlnd_qc_set_is_select()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">/* callback which decides if query is cached */<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">is_select</span><span style="color: #007700">(</span><span style="color: #0000BB">$query</span><span style="color: #007700">) {<br /> static </span><span style="color: #0000BB">$patterns </span><span style="color: #007700">= array(<br /> </span><span style="color: #FF8000">/* true - use default from mysqlnd_qc.ttl */<br /> </span><span style="color: #DD0000">"@SELECT\s+.*\s+FROM\s+test@ismU" </span><span style="color: #007700">=> </span><span style="color: #0000BB">true</span><span style="color: #007700">,<br /> </span><span style="color: #FF8000">/* 3 - use TTL = 3 seconds */<br /> </span><span style="color: #DD0000">"@SELECT\s+.*\s+FROM\s+news@ismU" </span><span style="color: #007700">=> </span><span style="color: #0000BB">3<br /> </span><span style="color: #007700">);<br /> </span><span style="color: #FF8000">/* check if query does match pattern */<br /> </span><span style="color: #007700">foreach (</span><span style="color: #0000BB">$patterns </span><span style="color: #007700">as </span><span style="color: #0000BB">$pattern </span><span style="color: #007700">=> </span><span style="color: #0000BB">$ttl</span><span style="color: #007700">) {<br /> if (</span><span style="color: #0000BB">preg_match</span><span style="color: #007700">(</span><span style="color: #0000BB">$pattern</span><span style="color: #007700">, </span><span style="color: #0000BB">$query</span><span style="color: #007700">)) {<br /> </span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"is_select(%45s): cache\n"</span><span style="color: #007700">, </span><span style="color: #0000BB">$query</span><span style="color: #007700">);<br /> return </span><span style="color: #0000BB">$ttl</span><span style="color: #007700">;<br /> }<br /> }<br /> </span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"is_select(%45s): do not cache\n"</span><span style="color: #007700">, </span><span style="color: #0000BB">$query</span><span style="color: #007700">);<br /> return </span><span style="color: #0000BB">false</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">mysqlnd_qc_set_is_select</span><span style="color: #007700">(</span><span style="color: #DD0000">"is_select"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/* Connect, create and populate test table */<br /></span><span style="color: #0000BB">$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">"password"</span><span style="color: #007700">, </span><span style="color: #DD0000">"schema"</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">"DROP TABLE IF EXISTS test"</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">"CREATE TABLE test(id INT)"</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">"INSERT INTO test(id) VALUES (1), (2), (3)"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/* cache put */<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 id = 1"</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">/* cache hit */<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 id = 1"</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">/* cache put */<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 * FROM test"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above examples will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> is_select( DROP TABLE IF EXISTS test): do not cache is_select( CREATE TABLE test(id INT)): do not cache is_select( INSERT INTO test(id) VALUES (1), (2), (3)): do not cache is_select( SELECT id FROM test WHERE id = 1): cache is_select( SELECT id FROM test WHERE id = 1): cache is_select( SELECT * FROM test): cache </pre></div> </div> </div> </div> <div class="refsect1 seealso" id="refsect1-function.mysqlnd-qc-set-is-select-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <a href="mysqlnd-qc.configuration.html" class="link">Runtime configuration</a> </li> <li class="member"> <a href="mysqlnd-qc.configuration.html#ini.mysqlnd-qc.ttl" class="link">mysqlnd_qc.ttl</a> </li> <li class="member"> <a href="mysqlnd-qc.configuration.html#ini.mysqlnd-qc.cache-by-default" class="link">mysqlnd_qc.cache_by_default</a> </li> <li class="member"> <span class="function"><a href="function.mysqlnd-qc-set-user-handlers.html" class="function" rel="rdfs-seeAlso">mysqlnd_qc_set_user_handlers()</a> - Sets the callback functions for a user-defined procedural storage handler</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-qc-set-cache-condition.html">mysqlnd_qc_set_cache_condition</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.mysqlnd-qc-set-storage-handler.html">mysqlnd_qc_set_storage_handler</a></div> <div class="up"><a href="ref.mysqlnd-qc.html">mysqlnd_qc Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>