Recently I started retro-fitting the MeLikeDrinks.com drink website to cache frequently used data to improve performance, as such I wrote a light, custom cache API which sits on top of PHP’s Memcached API. Because I follow TDD principles, I wrote out the tests first, which helped me write out the API calls needed to support the application, but when it came time to actually get the memcached service on my CentOS box, I ran into all sorts of confusion, which motivated me to write this article. It is my hope that this article will help alleviate any confusion others may face when they decide to dive into the cache pool.

First, I need to clarify one of the more confusing issues regarding PHP and Memcache, which is, there are two different PHP Apis. Depending on which PHP Memcache API you select, will determine the steps necessary for PHP to gain access to the underlying memcached server. The differences between PHP Memcache and PHP Memcached are outlined here. Regardless of the differences between the two PHP APIs we have to choose from, they both access the same underlying Memcached Service. The only REAL difference, as far as configuration and installation steps are concerned, is that PHP Memcached requires an external library known as libmemcached. In short, the stacks look like this:

PHP (PECL) Memcache

PHP (PECL) Memcached

  •  libevent (dependency)
  • memcached (service)
  • libmemcached
  • zlib (PHP dependency)
  • PHP (PECL) Memcached

As you can see, the stacks are nearly identical, except for the fact that PHP Memcached requires an extra layer; libmemcached. If you opt to use PHP Memcache, and because this article assumes you are using CentOS,  you can simply have YUM install the entire stack for your via `yum install php-memcache`. If your environment requires you to compile PHP, then you can issue a `yum install memcached` command, and YUM will install libevent and memcached, then you can compile PHP (and PHP Memcache module).

If you are still reading, then you want to install and use PHP Memcached, which unfortunately will require a little more work on your end. I will not go over how to install PHP Memcached using PECL, as I do not believe in these types of automated processes. In the past I have had bad experiences with PECL and rather not introduce another layer of complexity, so the following steps will allow you to compile and install the PHP Memcached stack without PECL.

Steps required:

  • libevent (dependency)
    • yum install libevent libevent-devel
  • memcached (service)
    • yum install memcached
  • libmemcached
    • First check which version of PHP Memcached you wish to use, which will determine which version of libmemcached you need. For example; according to PHP PECL Memcached changelog, the latest version of libmemcached you can use is 1.0.4, otherwise if you try to use a newer version of PHP PECL Memcached you may run into unforeseen issues, in other words, you should ALWAYS assume that PHP PECL Memcached is a few versions behind libmemcached.
    • Based on which version of libmemcached you need from the previous step, you can download from libmemcached download page.
    • Extract file and CD into dir
    • $ -> ./configure –with-libevent-prefix=/usr
    • $ -> make
    • $ -> make install
  • PHP PECL Memcached
    • Download the correct version based on which version of libmemcached you compiled and installed via changelog (which links to download) page.
    • Extract file and CD into dir
    • $ -> phpize
    • $ -> ./configure –with-libmemcached-dir=/path/to/where/memcached.h/is/located
    • make
    • make install
  • PHP ini config
    • vi /path/to/php.ini
    • Add: extension=memcached.so
  • Test module installation
    • $ -> php -m
      • You should memcached listed among other modules
    • $ -> php -i | fgrep -irs cache
      • You should see various memcached config settings
  • Finishing touches
    • Restart apache
    • Start memcached
    • Write a test script to test the setting and getting of a value from your cache server via PHP Memcached API.

And there you have it, a memcached stack without using PECL. All things considered it should not have been too painful an installation, however I must make one disclaimer; I customized my memcached stack a bit more than I eluded to in this article, so if you run into an issue, just post a comment and I will try to help you resolve the issue.

Now that you have a memcached service, and an API to use, you should start focusing on the code points of your application with the most overhead, this will give you the most bang for your buck when you start caching data. Good luck, and happy caching.