Book Image

Getting Started with Memcached

By : Ahmed Soliman
Book Image

Getting Started with Memcached

By: Ahmed Soliman

Overview of this book

<p>Web application performance is no longer a non-functional requirement, but an implicit condition for an engaging user experience. As a result, responsive and highly scalable applications are becoming a necessity. Memcached is a high-performance distributed memory caching system built to speed up dynamic web applications by offloading pressure from your database. <br /><br />Getting Started with Memcached is a hands-on, comprehensive guide to the Memcached service and it’s API in different programming languages. It contains practical recipes to integrate Memcached within your Rails, Django, or even Scala Play! applications.<br /><br />This book will show you everything you need to know to start using Memcached in your existing or new web applications.<br />This book uses real-world recipes to help you learn how to store and retrieve data from your clustered virtual memory cache pool and how to integrate caching into your favourite web development framework.</p> <p><br />You will also learn how to build a Memcached consistent-hashing scalable cluster and how Memcached clients are properly configured to use different servers to scale out your memory cache pool in Ruby, Python, PHP, and Java. With this book, you will see how to cache templates and database queries in the most popular web development framework in use today.</p>
Table of Contents (9 chapters)

Basic installation of memcached on Mac (Simple)


Installation on Mac OS X is quite a straightforward process if you have the right tools installed.

Getting ready

We will be using a package manager for Mac OS X that is really a must-have tool for any Mac user and even more important if you are a developer or a system engineer.

The package manager we will be using is Homebrew, the missing package manager for OS X

You will need to install Homebrew first if you don't have it, installation is straightforward and all instructions are explained in different languages for your comfort at http://brew.sh/.

Or you can simply use this one liner to install Homebrew on your Mac:

ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"

How to do it...

  1. Update the Homebrew local repository:

    brew update
    
  2. Use Homebrew to install the memcached package:

    brew install memcached
    

    Memcached is not started by default after installation, if you manually want to start memcached use, /usr/local/opt/memcached/bin/memcached

  3. If you are planning to start memcached on boot every time, you will need to create a link:

    ln -sfv /usr/local/opt/memcached/*.plist ~/Library/LaunchAgents
    

    Then, you may want to start memcached immediately using launchctl:

    launchctl load ~/Library/LaunchAgents/homebrew.mxcl.memcached.plist
    

How it works...

We started by updating the local copy of the Homebrew repository from the Internet using the brew update, this ensures we are installing the latest version of any package we want to. Then we installed the latest version of memcached.

Homebrew does not start memcached automatically after installation nor during boot time, so we had to do this ourselves.

You can always start memcached manually in the foreground by using the memcached daemon executable. But if you want memcached to start on boot we created a symlink so that launchctl picks it up on boot.

There's more...

If you have configured your memcached daemon to start on boot as previously described, you might be wondering, where is the default configuration? Is it in the same place as Ubuntu? The answer is No!

Because the memcached service will be started by launchctl, the configuration is controlled by it. You will find the configuration file at /usr/local/opt/memcached/homebrew.mxcl.memcached.plist and it's basically an XML file.

See the following section in the file:

 <array>
 <string>/usr/local/opt/memcached/bin/memcached</string>
 <string>-l</string>
 <string>localhost</string>
 </array>

As you may have discovered yourself, this represents a list of parameters passed to the memcached executable at runtime.

You can edit the localhost field, as previously described in the Ubuntu configuration section, but if you want to configure the amount of memory that memcached can use, you will need to insert a couple of directives for that directly after the localhost directive:

<string>-m</string><string>256</string>

This configures memcached to use up to 256 MB of memory for its on-memory storage.

Another option if you are not a Homebrew user, is to use MacPorts (http://www.macports.org/), it works almost the same way and you can use the command port instead of brew.

Another interesting feature of brew, is that you can specify options to control the way memcached is built (compiled), so most of the time you don't really need to compile memcached from source on Mac OS X, instead, you use brew options for that. An example, is to enable SASL support to disable the plain ASCII protocol or to add SASL with password option, as stated in the brew info memcached.

--enable-sasl
      Enable SASL support -- disables ASCII protocol!
--enable-sasl-pwdb
      Enable SASL with memcached's own plain text password db support -- disables ASCII protocol!

So, for example, if you want to enable SASL support during installation, use the following:

brew install memcached --enable-sasl