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 that allow you to work with Memcached. There are two PHP Memcache extensions available from the PHP Extension Community Library: PHP Memcached and PHP Memcache.
Table of Contents
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 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:
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 your Linux distribution’s package manager and install from there.
Next, let’s install a PHP Memcache extension.
PHP Memcache
wget https://pecl.php.net/get/memcache-x.x.tgz 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
Remember to install libmemcached dependency (or for Ubuntu/Debian):
yum install cyrus-sasl-devel zlib-devel gcc-c++ wget https://launchpad.net/libmemcached...+download/libmemcached-x.x.tar.gz tar -xvf libmemcached-x.x.tar.gz cd libmemcached-x.x ./configure --disable-memcached-sasl make make install
Then install PHP Memcached:
wget https://pecl.php.net/get/memcached-x.x.tgz 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 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 and, if necessary, Apache (and/or Nginx, etc.).
If you would like to view stats of hit rate etc., you can download Memcache PHP stats. Which will look something like this…
Published: Aug 29, 2015 | Last updated: August 14th, 2023