Setting up Memcached on a Mac for Local Drupal Development

When developing Drupal applications on my Mac, I like having access to the same tools I use on servers. This allows me to test for possible conflicts and issues, while evaluating performance impact. I recently decided to look at getting Memcached going on my Mac since we use it on servers at Classic Graphics. Memcached is a memory-based caching system for speeding up web applications by storing chunks of data. Drupal and PHP can leverage Memcached to assist with performance.

Below is a bit about what I learned through the process of getting this going in my local development environment.

I am using homebrew and homebrew-php as the base for my local development setup. If you want to checkout the details of my setup, see my brewStack docs.

Setup the memcached service

  • Installation
    $ brew install memcached

    • The following command will show all memcached options.
      $ memcached -h
  • There are several options to run memcached after the initial installation.

    1. Run memcached from a terminal session.
      $ memcached
    2. Run memcached as a daemon.
      $ memcached -d
    3. Use launchctl to start memcached at login and keep it running.
      • Copy the provided launchctl plist file to ~/Library/LaunchAgents. I like keeping all of my launchctl plist files together.
        $ cp /usr/local/Cellar/memcached/1.4.15/homebrew.mxcl.memcached.plist ~/Library/LaunchAgents/
      • Load the memcached plist file.
        $ launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.memcached.plist
      • You can stop the memcached service with the following command.
        $ launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.memcached.plist
      • If you drop the “-w” from the previous two commands, memcache will not load automatically when you restart your Mac. This may be the way you want to run memcached so it doesn’t take up system resources when you don’t need it.
      • The following command will show you if the memcached service is loaded in the launchd system. If you get a line returned, it is running.
        $ launchctl list | grep memcached

Setup the php-memcache extension

  • Install php-memcache so PHP can communicate with your memcached service.
    • The installation command below requires homebrew-php.
      $ brew install php53-memcache
      Note: If you are running PHP 5.4, you can change “php53-memcache” to “php54-memcache” in the command line above.

Setup Drupal to use memcached for caching

  • Install the Memcache API and Integration module. Your Drupal instances will need this module to save its cache data to memcached.
    • Install and enable the module.
    • Details on usage and installation can be found here and here.
    • I have been using the following settings in my setings.php file with success.
      $conf['cache_backends'][] = ’sites/all/modules/memcache/';
      $conf['cache_default_class'] = 'MemCacheDrupal';
      $conf['memcache_key_prefix'] = 'something_unique';
    • You can optionally enable the memcache_admin module to view memcache statistics at /admin/reports/memcache.

Note: If you have any issues with the memcached cache clearing, you can restart the memcached service.

Additional Resources

Blog Category: