Quickstart and Examples
The mysqlnd memcache plugin is easy to use.
This quickstart will demo typical use-cases, and provide practical advice on getting
started.
It is strongly recommended to read the reference sections in addition to the
quickstart. The quickstart tries to avoid discussing theoretical concepts
and limitations. Instead, it will link to the reference sections. It is safe
to begin with the quickstart. However, before using the plugin in mission critical
environments we urge you to read additionally the background information from the
reference sections.
Setup
The plugin is implemented as a PHP extension. See also the
installation instructions to
install this extension.
Compile or configure the PHP MySQL extension (API) (mysqli,
PDO_MYSQL, mysql).
That extension must use the mysqlnd library as
because mysqlnd_memcache is a plugin for the mysqlnd library. For additional information,
refer to the mysqlnd_memcache installation instructions.
Then, load this extension into PHP and activate the plugin in the PHP configuration
file using the PHP configuration directive named
mysqlnd_memcache.enable.
Enabling the plugin (php.ini)
Follow the instructions given in the MySQL
Reference Manual on installing the Memcache plugins for the MySQL server. Activate
the plugins and configure Memcache access for SQL tables.
The examples in this quickguide assume that the following table exists, and that
Memcache is configured with access to it.
SQL table used for the QuickstartUsage
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.
Basic example.
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);
}
?>
]]>
&example.outputs;
Hello
[f2] => World
[f3] => !
)
array(
[id] => 2
)
]]>