Memcache PHP Extensions for Memcached Caching Daemon

Memcached (Memcache Daemon) is a caching daemon designed especially for dynamic web applications to decrease database load by storing objects in memory. It is commonly used to speed up dynamic database-driven websites by caching data and objects in server memory to reduce the number of times the data source must be read. Memcached is free and open-source software, licensed under the Revised BSD license. This article will cover PHP extensions which allow you to work with memcached. There are two PHP memcache extensions available from the PHP Extension Community LibraryPHP memcached and PHP memcache.

PHP Memcached vs PHP memcache

These two PHP extensions are not identical. PHP Memcache is older, very stable but has a few limitations. The PHP memcache module utilizes the daemon directly while the PHP memcached module uses the libMemcached client library and also contains some added features. You can compare features and differences between them here.

Installing Memcache Daemon + PHP memcache or PHP memcached

Before selecting a PHP extension be sure to install the memcache daemon:

Centos / Red Hat:

yum install memcached

Ubuntu/ Debian:

apt-get update
apt-get install memcached

After installing Memcached, open the configuration file for Memcached and make any changes:

Centos / Red Hat:

vim /etc/sysconfig/memcached

Ubuntu / Debian:

vim /etc/memcached.conf

Exit and save the configuration file, and then restart Memcached

service memcached restart

Remember to set the memcache daemon to start on server boot.

Centos / Red Hat:

chkconfig memcached on

Ubuntu / Debian:

update-rc.d memcached enable

Next lets install a PHP memcache extension:

PHP memcache:

wget https://pecl.php.net/get/memcache-2.2.7.tgz
tar xf memcache-2.2.7.tgz
cd memcache-2.2.7
phpize
./configure
make && make install

Then add memcache.so to your php.ini file:

extension="memcache.so"

PHP memcached:

Remember to install libmemcached dependancy (Ubuntu/Debian):

yum install cyrus-sasl-devel zlib-devel gcc-c++
wget https://launchpad.net/libmemcached/1.0/1.0.16/+download/libmemcached-1.0.16.tar.gz
tar -xvf libmemcached-1.0.16.tar.gz
cd libmemcached-1.0.16
./configure --disable-memcached-sasl
make
make install

Then install PHP Memcached:

wget https://pecl.php.net/get/memcached-2.1.0.tgz
tar xf memcached-2.1.0.tgz
cd memcached-2.1.0
phpize
./configure
make && make install

Then add memcached.so to your php.ini file:

extension="memcached.so"

You will need to connect your PHP application to memcached. For example, using W3 Total Cache with WordPress, memcached module with Drupal, Magento config, etc.

Finally restart memcached, httpd (or apache, apache2 for Ubuntu/Debian)

If you would like to view stats of hit rate etc you can download memcachephp stats. Which will look something like this…

MEMCACHE STATS

Tags: , , , ,