Memcache (Data + Object caching)

Memcache(d) is a object caching system that stores data in RAM to reduce the number of times something has to be read. It is generally used to speed up dynamic database-driven websites.

Quick Links:
Memcache repo CentOS
Installing memcache
Installing memcache php module
Php.ini configuration
Memcache Commands

Repo

First thing to do is make sure you have the correct repository installed with:

rpm -Uvh http://mirrors.kernel.org/fedora-epel/6/x86_64/epel-release-6-8.noarch.rpm

Installation

To install memcache you can use the following:

yum install -y memcached

and then turn it on from server boot:

chkconfig memcached on

Now we need to make some configuration changes by editing /etc/sysconfig/memcached and you should see something similar to:

PORT="11211"

USER="memcached"

MAXCONN="1024"

CACHESIZE="64"

OPTIONS=""

You should then update the options section to:

OPTIONS="-l 127.0.0.1"


Php module

You will now need to install the php module.

yum groupinstall “Development Tools”

yum install zlib-devel libmemcached-devel php-pear php-pecl-memcached

pecl install -f memcached-1.0.0

Php.ini config

Once you have done this you will need to edit your php.ini file found at /etc/php.ini

You should then locate the following section:

[Session]

; Handler used to store/retrieve data.

; http://www.php.net/manual/en/session.configuration.php#ini.session.save-handler

session.save_handler = files

You should change this section to the following:

[Session]

; Handler used to store/retrieve data.

; http://www.php.net/manual/en/session.configuration.php#ini.session.save-handler

session.save_handler = memcached

session.save_path = "127.0.0.1:11211"

 

Restart apacheĀ  and your done!

You may get a message when restarting apache similar to:

Starting httpd: httpd: apr_sockaddr_info_get() failed for memcached

httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName

This can be resolved by editing /etc/httpd/conf/httpd.conf and commenting or adding the following line

ServerName localhost

Restart apache to resolve this issue.

Memcache commands

Is memcache running?

If you log into your server as a user (not root) and run the following command it will tell you what is being cached by memcache

memcached -vv