Skip directly to content

How to install and monitor Memcache on CentOS?

on Mon, 02/01/2010 - 07:09

What is Memcache?

Memcache is free, open source, high performance, distributed memory object caching system. It is intended for speeding up dynamic web-applications by reducing database load. It is in-memory data store that keeps data in the format of key-value pair. Data is strings/objects and are usually the results of database calls, API calls or page rendering.

Its APIs are available in popular languages. So, we find python memcache API integration, php memcache API orJava client for memcache.

How to Install Memcache?

Memcache has a dependency on libevent. So we need to install that first.

First, install the dependency libevent:

cd /usr/local/src
 curl -O http://monkey.org/~provos/libevent-1.4.9-stable.tar.gz
 tar xzvf libevent-1.4.9-stable.tar.gz 
 cd libevent-1.4.9-stable
 ./configure --prefix=/usr/local
 make
 make install

Now, install memcached. Best is to use famous YUM. But, before you go there, you would need to activate the RPMForge custom repository (formely known as Dag Wieers): http://dag.wieers.com/rpm/packages/rpmforge-release/

wget http://dag.wieers.com/rpm/packages/rpmforge-release/rpmforge-release-0.3...
rpm -i rpmforge-release-0.3.4-1.el4.rf.i386.rpm
yum install --enablerepo=rpmforge memcached

Now memcached is installed.

You can test it:

service memcached restart
chkconfig memcached on // start memcache on boot

memcache restart stop

If you want to configure the memory or create multiple instances make change to: /etc/init.d/memcached start daemon.

multiple memcache instances

Remember, we have just installed Memcache on the server. If you are using  PHP, Python, Java or a CMS like Drupal, you would need to install the language based client API for memcache. For example for PHP, we would use PECL memcache.

Install PECl extension for PHP

Assuming you have pecl installed on your CentOS, use the following command:

cd /usr/local/src/
wget http://pecl.php.net/get/memcache-2.1.2.tgz
tar zxvf memcache-2.1.2.tgz
phpize
./configure
make & make install
echo "extension=memcache.so" >> /usr/local/lib/php.ini
service httpd restart

install memcache pecl extension

 

Alternatively, you could try, if pecl is installed on centos.

pecl install memcached

Drupal integration with Memcache

Install Drupal memcache module and configure your settings.php as follows:

$conf = array(
    'cache_inc' => './sites/all/modules/memcache/memcache.db.inc',
 );

And you are ready to go. Complex configurations with memcache can be set.

How to monitor Memcache Hits and Misses?

How lovely? My Drupal/PHP application is up and running.  Don't forget the steps  after installing memcahe would be different for a different language client or API integration of the application.

As of now, I want to monitor my hits and misses. How do I do that? I use Brutis.

./brutis -x 127.0.0.1:11211 -v

It tells me my hits and misses. Check out 100% Hits.

100% memcache hits

 

And there you are with fast web application.

Zlib-devel maybe required

If you're getting an error like
configure: error: memcache support requires ZLIB. Use --with-zlib-dir=

to specify prefix where ZLIB include and library are located
ERROR: `/var/cache/php-pear/memcache-2.2.5/configure --enable-memcache-session=yes' failed

Install:

yum install zlib-devel

Libevent can be installed using YUM

Libevent can be installed using YUM as follows:

yum install libevent

using memcached

We use memcached and apache for http://www.decasasyautos.com
and it's run very good.
http://www.decasasyautos.com use memcaches since 2008.

Javier
http://www.decasasyautos.com