Caching Application Data Using Redis Labs Memcache

You can use Redis Labs Memcached Cloud to cache application data. Memcached Cloud is a fully-managed service offering a variety of options for running Memcached.

Pricing

Redis Labs offers a free tier that provides 30mb of cache storage.

Setting up

To use Redis Labs Memcached Cloud:

  1. Sign up for a Redis Labs account.
  2. Create a Memcached Bucket using Memcached Cloud. Be sure to specify GCE/us-central1 as the Cloud for the lowest latency.
  3. Edit the bucket and set a username and password.
  4. Note the bucket's Endpoint.

Installing a Memcached client library

Because Redis Labs uses SASL authentication, you need to use a Memcached client that supports the binary protocol and SASL authentication.

We recommended you use pylibmc.

libmemcached is required to install pylibmc. The runtime includes this library. If you want to run and test your application locally, you need to install libmemcached in your OS:

Linux

For Debian and Ubuntu derivatives:

sudo apt-get install libmemcached-dev libsasl2-dev libsasl2-modules sasl2-bin

For Red Hat, CentOS, and Fedora, use yum:

yum install libmemcached-devel libsasl2-devel

For Linux Arch:

pacman -S libmemcached

Mac OS X

  1. Install Homebrew.

  2. Install libmemcached:

    brew install libmemcached
    

Windows

pylibmc presently does not work on Windows. You can try using another Python memcached client.

Include pylibmc in your requirements.txt file:

Flask==0.11.1
gunicorn==19.6.0
pylibmc==1.5.1

If running locally, install your dependencies using pip:

pip install -r requirements.txt

Connecting to Redis Labs Memcache

Configure environment variables in your app.yaml with the Memcached server endpoint, username, and password for your Redis Labs Memcached bucket:

env_variables:
  # If you are using the App Engine Memcache service (currently in alpha),
  # uncomment this section and comment out the other Memcache variables.
  # USE_GAE_MEMCACHE: 1
  MEMCACHE_SERVER: your-memcache-server
  # If you are using a Memcached server with SASL authentiation enabled,
  # fill in these values with your username and password.
  MEMCACHE_USERNAME: your-memcache-username
  MEMCACHE_PASSWORD: your-memcache-password

Use the environment variables in your application code to configure a Memcached client:

# Environment variables are defined in app.yaml.
if os.environ.get('USE_GAE_MEMCACHE'):
    MEMCACHE_SERVER = ':'.join(
        os.environ.get('GAE_MEMCACHE_HOST', 'localhost'),
        os.environ.get('GAE_MEMCACHE_PORT', 11211))
else:
    MEMCACHE_SERVER = os.environ.get('MEMCACHE_SERVER', 'localhost:11211')

MEMCACHE_USERNAME = os.environ.get('MEMCACHE_USERNAME')
MEMCACHE_PASSWORD = os.environ.get('MEMCACHE_PASSWORD')

memcache_client = pylibmc.Client(
    [MEMCACHE_SERVER], binary=True,
    username=MEMCACHE_USERNAME, password=MEMCACHE_PASSWORD)

Testing Memcached locally

If you are testing your app locally, you can still connect to your the Redis Labs Memcached bucket. However, you might want to run a local instance of Memcached to avoid interacting with production data.

To run a local Memcached instance, install a Memcached daemon in your OS:

Linux

  1. Use the memcached package.

    For Debian and Ubuntu derivatives:

    apt-get install memcached
    

    For Red Hat, CentOS, and Fedora, use yum:

    yum install memcached
    

    For Linux Arch:

    pacman -S memcached
    
  2. Run it as a daemon (or check systemd for an automatic startup):

    memcached -d
    

Mac OS X

  1. Install Homebrew.

  2. Install memcached:

    brew install memcached
    
  3. Run it as a daemon:

    memcached -d
    

Windows

  1. Build your own memcached. Alternatively, you can download freeware binaries.

  2. Run it as a daemon:

    memcached -d
    

Send feedback about...

App Engine flexible environment for Python docs