Oracle 5.0 Reference Manual page 2687

Table of Contents

Advertisement

Mysqlnd query result cache plugin (mysqlnd_qc)
users to make a quick estimation about the maximum performance gains from caching. An application
designed to use a cache may be able to prefix selected statements with the appropriate SQL hints.
However, altering an applications source code may not always be possible or desired, for example, to
avoid problems with software updates. Therefore, PECL/mysqlnd_qc allows setting a callback which
decides if a query is to be cached.
The callback is installed with the
statement string of every statement inspected by the plugin. Then, the callback can decide whether to
cache the function. The callback is supposed to return
return value of
makes the plugin try to add the statement into the cache. The cache entry will be
TRUE
given the default TTL ( mysqlnd_qc.ttl). If the callback returns a numerical value it is used as the
TTL instead of the global default.
Example 20.311. Setting a callback with
mysqlnd_qc.enable_qc=1
mysqlnd_qc.collect_statistics=1
<?php
/* callback which decides if query is cached */
function is_select($query) {
static $patterns = array(
/* true - use default from mysqlnd_qc.ttl */
"@SELECT\s+.*\s+FROM\s+test@ismU" => true,
/* 3 - use TTL = 3 seconds */
"@SELECT\s+.*\s+FROM\s+news@ismU" => 3
);
/* check if query does match pattern */
foreach ($patterns as $pattern => $ttl) {
if (preg_match($pattern, $query)) {
printf("is_select(%45s): cache\n", $query);
return $ttl;
}
}
printf("is_select(%45s): do not cache\n", $query);
return false;
}
/* install callback */
mysqlnd_qc_set_is_select("is_select");
/* Connect, create and populate test table */
$mysqli = new mysqli("host", "user", "password", "schema", "port", "socket");
$mysqli->query("DROP TABLE IF EXISTS test");
$mysqli->query("CREATE TABLE test(id INT)");
$mysqli->query("INSERT INTO test(id) VALUES (1), (2), (3)");
/* cache put */
$mysqli->query("SELECT id FROM test WHERE id = 1");
/* cache hit */
$mysqli->query("SELECT id FROM test WHERE id = 1");
/* cache put */
$mysqli->query("SELECT * FROM test");
$stats = mysqlnd_qc_get_core_stats();
printf("Cache put: %d\n", $stats['cache_put']);
printf("Cache hit: %d\n", $stats['cache_hit']);
?>
The above examples will output something similar to:
is_select(
mysqlnd_qc_set_is_select
FALSE
mysqlnd_qc_set_is_select
DROP TABLE IF EXISTS test): do not cache
2667
function. The callback is given the
if the statement shall not be cached. A

Advertisement

Table of Contents
loading

This manual is also suitable for:

Mysql 5.0

Table of Contents