Spec-Zone .ru
спецификации, руководства, описания, API

15.6.3.6. Using MySQL and memcached with PHP

PHP provides support for the Memcache functions through a PECL extension. To enable the PHP memcache extensions, build PHP using the --enable-memcache option to configure when building from source.

If you are installing on a Red Hat-based server, you can install the php-pecl-memcache RPM:

root-shell> yum --install php-pecl-memcache

On Debian-based distributions, use the php-memcache package.

To set global runtime configuration options, specify the configuration option values within your php.ini file. The following table provides the name, default value, and a description for each global runtime configuration option.

Configuration option Default Description
memcache.allow_failover 1 Specifies whether another server in the list should be queried if the first server selected fails.
memcache.max_failover_attempts 20 Specifies the number of servers to try before returning a failure.
memcache.chunk_size 8192 Defines the size of network chunks used to exchange data with the memcached server.
memcache.default_port 11211 Defines the default port to use when communicating with the memcached servers.
memcache.hash_strategy standard Specifies which hash strategy to use. Set to consistent to enable servers to be added or removed from the pool without causing the keys to be remapped to other servers. When set to standard, an older (modula) strategy is used that potentially uses different servers for storage.
memcache.hash_function crc32 Specifies which function to use when mapping keys to servers. crc32 uses the standard CRC32 hash. fnv uses the FNV-1a hashingalgorithm.

To create a connection to a memcached server, create a new Memcache object and then specify the connection options. For example:

<?php$cache = new Memcache;$cache->connect('localhost',11211);?>

This opens an immediate connection to the specified server.

To use multiple memcached servers, you need to add servers to the memcache object using addServer():

bool Memcache::addServer ( string $host [, int $port [, bool $persistent                 [, int $weight [, int $timeout [, int $retry_interval                 [, bool $status [, callback $failure_callback                 ]]]]]]] )

The server management mechanism within the php-memcache module is a critical part of the interface as it controls the main interface to the memcached instances and how the different instances are selected through the hashing mechanism.

To create a simple connection to two memcached instances:

<?php$cache = new Memcache;$cache->addServer('192.168.0.100',11211);$cache->addServer('192.168.0.101',11211);?>

In this scenario, the instance connection is not explicitly opened, but only opened when you try to store or retrieve a value. To enable persistent connections to memcached instances, set the $persistent argument to true. This is the default setting, and causes the connections to remain open.

To help control the distribution of keys to different instances, use the global memcache.hash_strategy setting. This sets the hashing mechanism used to select. You can also add another weight to each server, which effectively increases the number of times the instance entry appears in the instance list, therefore increasing the likelihood of the instance being chosen over other instances. To set the weight, set the value of the $weight argument to more than one.

The functions for setting and retrieving information are identical to the generic functional interface offered by memcached, as shown in this table:

PECL memcache Function Generic Function
get() Generic get().
set() Generic set().
add() Generic add().
replace() Generic replace().
delete() Generic delete().
increment() Generic incr().
decrement() Generic decr().

A full example of the PECL memcache interface is provided below. The code loads film data from the Sakila database when the user provides a film name. The data stored into the memcached instance is recorded as a mysqli result row, and the API automatically serializes the information for you.

<?php$memc = new Memcache;$memc->addServer('localhost','11211');if(empty($_POST['film'])) {?>  <html  xml:lang="en" lang="en">    <head>      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />      <title>Simple Memcache Lookup</title>    </head>    <body>      <form method="post">        <p><b>Film</b>: <input type="text" size="20" name="film"></p>        <input type="submit">      </form>      <hr/><?php} else {        echo "Loading data...\n";        $film   = htmlspecialchars($_POST['film'], ENT_QUOTES, 'UTF-8');    $mfilms = $memc->get($film);    if ($mfilms) {        printf("<p>Film data for %s loaded from memcache</p>", $mfilms['title']);        foreach (array_keys($mfilms) as $key) {            printf("<p><b>%s</b>: %s</p>", $key, $mfilms[$key]);        }    } else {        $mysqli = mysqli('localhost','sakila','password','sakila');            if (mysqli_connect_error()) {            sprintf("Database error: (%d) %s", mysqli_connect_errno(), mysqli_connect_error());            exit;        }            $sql = sprintf('SELECT * FROM film WHERE title="%s"', $mysqli->real_escape_string($film));        $result = $mysqli->query($sql);        if (!$result) {            sprintf("Database error: (%d) %s", $mysqli->errno, $mysqli->error);            exit;        }        $row = $result->fetch_assoc();        $memc->set($row['title'], $row);        printf("<p>Loaded (%s) from MySQL</p>", htmlspecialchars($row['title'], ENT_QUOTES, 'UTF-8');    }}?>  </body></html>

With PHP, the connections to the memcached instances are kept open as long as the PHP and associated Apache instance remain running. When adding or removing servers from the list in a running instance (for example, when starting another script that mentions additional servers), the connections are shared, but the script only selects among the instances explicitly configured within the script.

To ensure that changes to the server list within a script do not cause problems, make sure to use the consistent hashing mechanism.