Memcache PHP Extension for the Memcached Caching Daemon

Memcached (Memcache Daemon) is a caching daemon specially designed for dynamic web applications to reduce 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 reads to the data source. Memcached is free and open source software licensed under the modified BSD license.

This article will cover PHP extensions that allow you to work with Memcached. There are two PHP Memcache extensions available from PHP Extension Community Library, PHP Memcached And PHP Memcached,

PHP Memcached vs PHP Memcached

These two PHP extensions are not the same. PHP Memcached It is old, very stable but has some limitations. The PHP Memcache module uses the daemon directly, whereas PHP Memcached uses the module libMemcached Client library and includes some additional features.

You can compare features and differences between them Here,

Installing Memcache Daemon + PHP Memcache or PHP Memcached

Before selecting the PHP extension, be sure to install the Memcache daemon:

CentOS/Red Hat:

dnf install memcached

Ubuntu/Debian:

apt update
apt install memcached

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

CentOS/Red Hat:

vi /etc/sysconfig/memcached

Ubuntu / Debian:

vi /etc/memcached.conf

Exit and save the configuration file, and then restart Memcached. Remember to set the Memcache daemon to start on server boot.

CentOS/Red Hat 6:

chkconfig memcached on

CentOS / Red Hat 7+:

systemctl start memcached
​systemctl enable memcached ​
systemctl status memcached

Ubuntu / Debian:

Memcached is already started and enabled upon installation.

note: You can search for your Linux distribution’s package manager and install it from there.

Next, let’s install a PHP Memcache extension.

PHP Memcached

wget 
tar xf memcache-x.x.tgz
cd memcache-x.x
phpize
./configure
make && make install

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

extension="memcache.so"

PHP Memcached

to remember install libmemcached dependency (or for ubuntu/debian,

yum install cyrus-sasl-devel zlib-devel gcc-c++
wget 
tar -xvf libmemcached-x.x.tar.gz
cd libmemcached-x.x
./configure --disable-memcached-sasl
make
make install

Then install PHP Memcached:

wget 
tar xf memcached-x.x.tgz
cd memcached-x.x
phpize
./configure
make && make install

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

extension="memcached.so"

You need to connect your PHP application to Memcached. For example, using W3 Total Cache with wordpress, memcached module with drupal, magento configurationEtcetera.

Finally, restart Memcached and, if necessary, Apache (and/or Nginx, etc.).

If you want to see statistics of hit rate etc., you can download Memcache PHP Stats, something that would look like this…

memcached statistics

Published: August 29, 2015 | Last Updated: August 9, 2021

Leave a Comment