After associating a MySQL connection with a Memcache connection using
mysqnd_memcache_set() the plugin attempts to transparently
replace SQL SELECT
statements by a memcache access. For that purpose the plugin monitors
all SQL statements executed and tries to match the statement string
against MYSQLND_MEMCACHE_DEFAULT_REGEXP
.
In case of a match, the mysqlnd memcache plugin checks whether the
SELECT
is accessing only columns of a mapped table and
the WHERE
clause is limited to a single key lookup.
In case of the example SQL table, the plugin will use the Memcache interface
of the MySQL server to fetch results for a SQL query like
SELECT f1, f2, f3 WHERE id = n
.
Example #1 Basic example.
<?php
$mysqli = new mysqli("host", "user", "passwd", "database");
$memc = new Memcached();
$memc->addServer("host", 11211);
mysqlnd_memcache_set($mysqli, $memc);
/*
This is a query which queries table test using id as key in the WHERE part
and is accessing fields f1, f2 and f3. Therefore, mysqlnd_memcache
will intercept it and route it via memcache.
*/
$result = $mysqli->query("SELECT f1, f2, f3 FROM test WHERE id = 1");
while ($row = $result->fetch_row()) {
print_r($row);
}
/*
This is a query which queries table test but using f1 in the WHERE clause.
Therefore, mysqlnd_memcache can't intercept it. This will be executed
using the MySQL protocol
*/
$mysqli->query("SELECT id FROM test WHERE f1 = 'Lady'");
while ($row = $result->fetch_row()) {
print_r($row);
}
?>
以上例程会输出:
array( [f1] => Hello [f2] => World [f3] => ! ) array( [id] => 2 )