PHP provides support for the Memcache functions through a PECL
extension. To enable the PHP memcache
extensions, you must build PHP using the
--enable-memcache
option to
configure when building from source.
If you are installing on a RedHat 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.
You can set global runtime configuration options by specifying
the values in the following table within your
php.ini
file.
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 hashing
algorithm. |
To create a connection to a memcached server,
you need to create a new Memcache
object and
then specifying 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. You can enable persistent connections to
memcached instances by setting 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 | Equivalent to |
---|---|
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'); ?> <html xmlns="http://www.w3.org/1999/xhtml" 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 echo "Loading data...\n"; $value = $memc->get($_REQUEST['film']); if ($value) { printf("<p>Film data for %s loaded from memcache</p>",$value['title']); foreach (array_keys($value) as $key) { printf("<p><b>%s</b>: %s</p>",$key, $value[$key]); } } else { $con = new mysqli('localhost','sakila','password','sakila') or die ("<h1>Database problem</h1>" . mysqli_connect_error()); $result = $con->query(sprintf('select * from film where title ="%s"',$_REQUEST['film'])); $row = $result->fetch_array(MYSQLI_ASSOC); $memc->set($row['title'],$row); printf("<p>Loaded %s from MySQL</p>",$row['title']); } ?>
With PHP, the connections to the memcached instances are kept open as long as the PHP and associated Apache instance remain running. When adding a 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.