memcached is a high-performance memory object caching system intended to speed up dynamic web applications by alleviating database load.

memcached is meant to work in concert with something like the MySQL query cache, not replace it. The two implementations excel at vastly different things: memcached is an object cache, while MySQL provides a query cache.

memcached is extremely fast. It uses libevent, which provides a mechanism to execute a callback function when a specific event occurs on a file descriptor, to scale to any number of open connections. On a modern Linux system memcached utilizes epoll, is completely non-blocking for network I/O, ensures memory never gets fragmented, and uses its own slab allocator and hash table to achieve 0(1) virtual memory allocation.

How it install

curl -O http://www.monkey.org/~provos/libevent-1.1a.tar.gz
tar zxf libevent-1.1a.tar.gz
cd libevent-1.1a
./configure
make
make install
cd ..
curl -O http://www.danga.com/memcached/dist/memcached-1.1.12.tar.gz
tar zxf memcached-1.1.12.tar.gz
cd memcached-1.1.12
./configure
make
make install


Then add /usr/local/lib to LD_LIBRARY_PATH in your .bash_profile


LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib

export LD_LIBRARY_PATH

How it Works

First, you start up the memcached daemon on as many spare machines as you have. The daemon has no configuration file, just a few command line options, only 3 or 4 of which you’ll likely use:

# /usr/local/bin/memcached –d –l x.x.x.x –u nobody –m 32 –p 11000

This starts memcached as a daemon (–d) on the IP address and port specified with –l and –p, respectively, running as the user nobody (–u), allocating 32 MB for object storage (–m). You should adjust the amount of storage to suit your needs; many memcached installs run with 4 GB. Once you’re comfortable with your startup options, add the appropriate command to your startup scripts.

With memcached installed and running, it’s time to get PHP talking to the object cache. While multiple PHP API’s exists, the one in the PECL repository is recommended. If you’re running a newer version of PHP, installation is as simple as:

# pecl install memcache